Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: can't use string refs while "strict refs"

by davido (Cardinal)
on Jul 30, 2012 at 05:15 UTC ( [id://984347]=note: print w/replies, xml ) Need Help??


in reply to can't use string refs while "strict refs"

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://984347]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found