I should use a hash instead, but humor me, would you ?

Well, just this once :-)

Actually, it works for me on the command line if not using strictures and not declaring the variables so they default to being package variables.

$ perl -le ' > $foo = q{foo}; > $bar = q{f}; > print ${$bar . q{oo}};' foo $

Enabling strictures and using lexical varables throws a "strict refs" error

$ perl -Mstrict -wle ' > my $foo = q{foo}; > my $bar = q{f}; > print ${$bar . q{oo}};' Can't use string ("foo") as a SCALAR ref while "strict refs" in use at + -e line 4. $

Turning the stricture off inside the print statement throws an "uninitialsed value" warning because you can't have soft references with lexical variables.

$ perl -Mstrict -wle ' > my $foo = q{foo}; > my $bar = q{f}; > print do {no strict q{refs}; ${$bar . q{oo}} };' Use of uninitialized value in print at -e line 4. $

Declaring the variables as package variables with our gets things working again.

$ perl -Mstrict -wle ' > our $foo = q{foo}; > our $bar = q{f}; > print do {no strict q{refs}; ${$bar . q{oo}} };' foo $

But having said all of that, you really should be using a hash and should avoid soft references like the plague if at all possible.

Cheers,

JohnGG


In reply to Re: Variable name by johngg
in thread Variable name by nsteiner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.