First, something quick and unrelated. In regexps, [] is for single character choices. [ab de] means 'a' or 'b' or 'c' or 'd' or a space. Use vertical bars to seperate longer choices instead of using square brackets:
if ($DB_name =~ /ISWLIVE|ISWTEST|ISWTEACH/) {

Back to the question at hand. That error is... not an error per say; it's a self-inflicted limitation. Under use strict 'refs' (included in use strict), $varname = 'foo'; %$varname; gives an error. The idea behind use strict is to force you to declare your variables to avoid typos. There's no way to check that using the above syntax, so it's disallowed. The quick way to solve it is to temporarily and locally disable strict refs:

my $db_hash = do { no strict 'refs'; \%$DB_name }; while (($key,$value) = each %$db_hash) { print "\tKey :: $key\n" ; print "\tValue :: $value\n" ; }

On to why it's printing nothing. %$varname looks for global variables, not lexicals. (Lexicals are my variables). Lexicals don't have a name at runtime, so they can't be looked up in this or any other fashion. So you're grabbing the global %ISWTEST, which doesn't exist. Isn't that exactly what use strict was trying to protect you from doing? Change 'my' to 'our' for your hashes to fix this problem.

All that being said, I recommend against those changes. I'd also avoid the eval "" someone suggested for being unecessarily costly. There's a nice easy way of fixing your problems that's strict-friendly:

... my %DBHashLookup = ( ISWLIVE => [ \%ISWLIVE, \%ISWLIVE_PATH ], ISWTEST => [ \%ISWTEST, \%ISWTEST_PATH ], ISWTEACH => [ \%ISWTEACH, \%ISWTEACH_PATH ], ); ... $DB_name = $ARGV[0]; $DB_info = $DBHashLookup{$DB_name}; if (defined($DB_info)) { ($DB_hash, $Path_hash) = @$DB_info; print "\n\tProcessing $DB_name Environment.\n\n" ; ... while (($key,$value) = each %$DB_hash) { ...

Simple elegance. Sweet, eh?

While I still have the soapbox *wink*, may I recommend a stylistic change that will make your code easier to read? Instead of if (!error) { do something } else { end it }, why not use if (error) { end it } do something. Here's what your code would look like:

$param_no = scalar @ARGV ; # if ($param_no != 1) # Other than one parameter has been supplied { print "\n\t<***** There MUST be a parameter supplied! *****>" ; print "\n\n\t<***** $script Failed to run. *****>\n\n" ; $msg = "DB name parameter not supplied" ; &end_it($mail_msg,$to,$from,$subject,$msg,$topline) ; } # $DB_name = $ARGV[0]; $DB_info = $DBHashLookup{$DB_name}; # if (!defined($DB_hash)) { print "\n\tThink again muppett!\n" ; $msg = "$DB_name is not a database known to this script!" ; &end_it($mail_msg,$to,$from,$subject,$msg,$topline) ; } # print "\n\tProcessing $DB_name Environment.\n\n" ; # ($DB_hash, $Path_hash) = @$DB_info; while (($key,$value) = each %$DB_hash) { print "\tKey :: $key\n" ; print "\tValue :: $value\n" ; } #

See how nicely it flows?


In reply to Re: hash name as a variable? by ikegami
in thread hash name as a variable? by Ronnie

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.