Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: hash name as a variable?

by ikegami (Patriarch)
on Sep 15, 2004 at 16:53 UTC ( [id://391241]=note: print w/replies, xml ) Need Help??


in reply to hash name as a variable?

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?

Replies are listed 'Best First'.
Re^2: hash name as a variable?
by Ronnie (Scribe) on Sep 16, 2004 at 09:46 UTC
    Though I don't actually send any, you would most definately have made my Christmas card list for your help. I've implemented the code including your "stylistic soapbox wink" and all is now working perfectly. There was one typo that threw me for a minute - if (!defined($DB_hash)) should be $DB_info and one piece of syntax that I'd not seen before. The @$ in ($DB_hash, $Path_hash) = @$DB_info; is puzzling me I'm guessing this has something to do with the referencing in the %DBhashlookup table?
Re^2: hash name as a variable?
by Ronnie (Scribe) on Sep 16, 2004 at 12:07 UTC
    Sorry to be a pest but I THINK I've worked it out now. The scalar values in the DBhashlookup table are actually anonymous arrays which are later dereferenced via the @$ in the
    ($DB_hash, $Path_hash_ = @$DB_info line?
    Or am I still way off the mark? Cheers,
    Ronnie "must start using references more" Cruickshank
      Bingo! That's exactly it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://391241]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 22:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found