Oh revered monks:

I know I'm missing something silly, but despite reading the Camel References chapter till I'm blurry, and reading the perlref tutorial I am stumped.

I understand hard references wrt using a variable name as a variable. But, if I want to create the variable name on the fly, I can't seem to get the refernce right:

#!/usr/bin/perl -l use warnings; use strict; my $str1 = qq[A-M o'Foo]; my $str2 = qq[.,rtyu_']; my $str3 = qq[$<%^]; my $str4 = qq[this works]; my $ref4 = \$str4; print $$ref4; # works as expected for (1..3) { my $ref = \${'str'.$_}; print "The string is $$ref"; }
outputs:
this works Error:17: Can't use string ("str1") as a SCALAR ref while "strict ref +s" in use

I've tried multiple levels of ref/deref (i.e.  \\\\$var ...  $$$$$ref) but still have the same problem.

What would be the proper way to reference these variables?


In reply to Variable Names and References by chakram88

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.