Here's a little trick that might have helped you to answer the first part of your question yourself, much faster than composing the node that you post here, and waiting for a meaningful answer to come back:

use warnings; use strict; use diagnostics; # <------------------------- Important line here. my $x = "Mmm...donut, thought Homer"; $x =~ /^(Mmm|Yech)\.\.\.(donut|peas)/; # matches foreach my $expr (1..$#-) { print "Match $expr: '${$expr}' at position ($-[$expr],$+ [$expr])\n +"; }

The output you get will be:

Can't use string ("1") as a SCALAR ref while "strict refs" in use at m +ytest.pl line 11 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref. Uncaught exception from user code: Can't use string ("1") as a SCALAR ref while "strict refs" in use +at mytest.pl line 11. at mytest.pl line 11.

Notice how it helpfully guides you to perlref? You can take that suggestion to your own command line by typing, "perldoc perlref, or by visiting perldoc.perl.org and typing in perlref. In that document when you search for "symbolic references" you will find the following:

Symbolic references

We said that references spring into existence as necessary if they are undefined, but we didn't say what happens if a value used as a reference is already defined, but isn't a hard reference. If you use it as a reference, it'll be treated as a symbolic reference. That is, the value of the scalar is taken to be the name of a variable, rather than a direct link to a (possibly) anonymous value.

People frequently expect it to work like this. So it does.

$name = "foo"; $$name = 1; # Sets $foo ${$name} = 2; # Sets $foo ${$name x 2} = 3; # Sets $foofoo $name->[0] = 4; # Sets $foo[0] @$name = (); # Clears @foo &$name(); # Calls &foo() (as in Perl 4) $pack = "THAT"; ${"${pack}::$name"} = 5; # Sets $THAT::foo without eval

This is powerful, and slightly dangerous, in that it's possible to intend (with the utmost sincerity) to use a hard reference, and accidentally use a symbolic reference instead. To protect against that, you can say

use strict 'refs';

and then only hard references will be allowed for the rest of the enclosing block.

Then your question might instead be seeking clarification on what that means. perlref also points you to perlreftut, which is a shorter and somewhat kinder entry point to references. After spending a few minutes with those documents you'll come to a more thorough understanding than you could hope to obtain by reading a few pithy replies here.


Dave


In reply to Re: can't use string refs while "strict refs" by davido
in thread can't use string refs while "strict refs" by artifact

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.