symbolic reference

S

slurper

i don't understand following snippet from "programming perl".

$name = "bam";
$$name = 1; # Sets $bam
${$name} = 2; # Sets $bam
${$name x 2} = 3; # Sets $bambam
$name->[0] = 4; # Sets $bam[0]
i don't understand above. $name is a string and it gets dereferenced.
impossible no?
can anyone shed some light on this
tx very much

@$name = (); # Clears @bam
&$name(); # Calls &bam() (as in prior versions of Perl)
$pkg = "THAT"; # (Don't use "package" or "pack"!)
${"${$pkg}::$name"} = 5; # Sets $THAT::bam without eval
 
N

nobull

slurper said:
i don't understand following snippet from "programming perl".

$name = "bam";
$$name = 1; # Sets $bam
${$name} = 2; # Sets $bam
${$name x 2} = 3; # Sets $bambam
$name->[0] = 4; # Sets $bam[0]
i don't understand above.

How so?
$name is a string and it gets dereferenced.

Yep that's correct. That is what symbolic reference means. It means
using a string as a reference.
impossible no?

No.

When Perl encounters a string when it expects a reference it will, by
default, take that string and perform lookup in the package symbol
table(s) to get the reference. Note: it only looks in the package
symbol tables (aka "the stashes"), not the lexical symbol table (aka
"the pad").

Although this feature is, for historical reasons, switched on by
default it is rarely needed can be very confusing if you are not
expecting it. It can (and should) be switched off when not needed.
For this reason it is best practice to put "use strict" at the top of
all your scripts and modules. Doing so switches the symbolic
reference feature off (except in a handfull of special cases). It
also switches off two other unrelated but equally potentially
confusing legacy features.

If you have a good reason to use symbolic references you can enable
them just for the current BLOCK with "no strict 'refs'".
can anyone shed some light on this

perldoc perlref

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 
A

Anthony

$name = "bam";
$$name = 1; # this also means ${bam} = 1. This is because of
interpolation. This could be used in interesting applications, but
will make debugging seriously complicated.
 
J

Joe Smith

slurper said:
i don't understand following snippet from "programming perl".

First thing to understand is that $variable_name and
${variable_name} are the same thing. Second, ${"variable_name"}
and $_="variable_name";${$_} also reference $variable_name.
$name = "bam";
$$name = 1; # Sets $bam
${$name} = 2; # Sets $bam
${$name x 2} = 3; # Sets $bambam
$name->[0] = 4; # Sets $bam[0]
i don't understand above. $name is a string and it gets dereferenced.

$name is a string. It's value is being used as the name of another
variable.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top