You can assign a hash ref to a glob to create a hash in the local namespace...
my $name = 'foo'; *{$name} = { a => 1, b => 2}; print "$foo{b}\n";
or you could even use eval...

I would suggest not doing either and just using a multilevel hash, with the first key being the name you wanted the hash to be, so... $big_hash{$hash_name}{$ref_id} if you can do this your life will be much easier, though I realize there are reason sometimes you want specific veriable names defined. Avoid if possible.

You may need a no strict; around it if you are using strict.

Update I just thought you could actually have the best of both worlds (i.e. not doing namespace mangling AND having different variable names) if you have a finite number of variables. Then you could create the variables, store references to them in a hash, then assign values to the refs, as so...

my(%h1,%h2,%h3); my %lookup = ( 1 => \%h1, foo => \%h2, c => \%h3 ); $which_hash = 'foo'; $lookup->{$which_hash}{test} = 'value'; $h2{test} eq 'value';
much cleaner. Though having a hash of hashes or an array of hashes is in some ways better, giving you the ability to iterate through them all, not to mention just all around cleaner.

                - Ant
                - Some of my best work - (1 2 3)


In reply to Re: multiple files and hashed by suaveant
in thread multiple files and hashed by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.