Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Do you know where your variables are?
 
PerlMonks  

Re: loop to allocate values to variables

by Codon (Friar)
on Aug 17, 2005 at 12:08 UTC ( [id://484481]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to loop to allocate values to variables

I'm going to have to agree with holli on this and say Why not just use the hash? If you must do something like this, know that it is in violation of some of the tenets of strict. Here's a version with limited un-strict-ness that does not use an eval:

#!/usr/bin/perl -l use strict; use warnings; my %foo = ( _foo => 'FOO', _bar => 'BAR', _baz => 'BAZ', zum => 'ZUM', # do not make this one a "real" variable ); { # limit the scope of the following 'no strict' no strict 'refs'; # There be magic here! This will get the keys from %foo # then attempt to remove the leading '_'. If it works, # the *modified* key gets passed along; otherwise, the # key is stripped from your processing list. We then # dynamically create a variable with the name of the # modified key and assign it the value of the unmodified # key in the hash. $$_ = $foo{"_$_"} for (grep { s/^_(.*)/$1/ } keys %foo); } # this is so we can speak the variable's name without Perl throwing a +fit no strict 'vars'; print for ($foo, $bar, $baz, $zum); # should produce one 'unitialized +' warning
I don't agree that this is good practice. However, do what you must to get the task accomplished. I know too well what it is to maintain ugly code, even when the original author was me.

Ivan Heffner
Sr. Software Engineer, DAS Lead
WhitePages.com, Inc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://484481]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.