Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

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

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.

In reply to Re: loop to allocate values to variables by Codon
in thread loop to allocate values to variables by travisbickle34

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 chanting in the Monastery: (2)
As of 2024-04-26 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found