I don't think that will help do what the author intended. The problem isn't creating new variables, it's creating new ones with dynamic names (and then being able to use them under
strict). Using lexicals doesn't solve this:
# trying to create a $dynamic_var
use strict;
my $var_name = 'dynamic_var';
my $$var_name = 'foo'; # doesn't compile
my ${$var_name} = 'foo'; # nor does this
eval "my \$$var_name = 'foo';"; # this runs fine, now $dynamic_var
# exists in some scope
print $dynamic_var, "\n"; # but this doesn't work in strict
strict requires that variables be predefined before their use, whether global or lexical. However, when dynamically creating variables of any kind, this can't be the case. You may be able to bend things a bit to get the new variables created, but you can't actually
use those new variables in the rest of your
strict-enabled program (it sounds like
nedals wants to use strict as much as possible in the remainder of the code). Please correct me if I'm missing some clever way of creating dynamic lexicals.
I think a hash is the best way to go.
blokhead
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.