Q: "my" variables and "no strict 'refs'"

D

Daniel Friedman

The bottom of p.594 in the Camel book (3rd edition) says this:

no strict 'refs';
$name = "variable";
$$name = 7 ; # Sets $variable to 7

....but I don't obtain that if I declare the variables with "my".
Here's my code:

no strict 'refs';
my ($variable, $name);
$variable = 1;
$name = "variable";
$$name = 7; # should set $variable to 7

print "\$variable is $variable\n";

.... and this prints for me:

$variable = 1

I've found the code works fine if I don't declare the variables (with
the "my" line). What am I not understanding here?

(I'm using Perl 5.6.0 on RH7.2, in case it makes a difference.)

Thanks very much,
--daniel
 
E

Eric Schwartz

The bottom of p.594 in the Camel book (3rd edition) says this:

no strict 'refs';
$name = "variable";
$$name = 7 ; # Sets $variable to 7

Don't do that.

See said:
...but I don't obtain that if I declare the variables with "my".

Yes, that's correct.

perldoc -q "How can I use a variable as a variable name?"

-=Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top