Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found