I would explain it slightly differently than BrowserUk.
$ perl -le 'my $a = "foo"; $a = \$a; print $a'
REF(0x7f9b608299c8)
In this example, a reference to $a is assigned to $a and the whole thing becomes circular; $a refers to $a, which refers to $a, ... So $a is a REF to $a (to $a, to ...) If the magical names $a and $b are avoided and everything is made lexical to keep everyone honest and aboveboard (nothing up my sleeve...):
Note that the reference addresses are all the same.c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 'foo'; $x = \$x; print $x; print $$x; print $$$x; print $$$$x; " REF(0x1df2a9c) REF(0x1df2a9c) REF(0x1df2a9c) REF(0x1df2a9c)
$ perl -le 'my $a = "foo"; my $b = \$a; $a = $b; print $a'
REF(0x7fab830299c8)
This example is a bit more roundabout in that it first assigns a reference to $a to another scalar before assigning it back to $a to establish circularity, but the result is the same:
Again, all the reference addresses are the same.c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 'foo'; my $y = \$x; $x = $y; print $x; print $$x; print $$$x; print $$$$x; " REF(0x601cec) REF(0x601cec) REF(0x601cec) REF(0x601cec)
In all these examples, the original value of $x (or $a in the OPed code), 'foo', is lost.
Update: Or entirely without circularity:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $w = 'foo'; my $x = \$w; my $y = \$x; my $z = \$y; ;; print $z; print $$z; print $$$z; print $$$$z; ;; dd $z; " REF(0x54c11c) REF(0x432734) SCALAR(0x4326f4) foo \\\"foo"
Give a man a fish: <%-{-{-{-<
In reply to Re: Why do I (sometimes) get a REF ref and not a SCALAR ref?
by AnomalousMonk
in thread Why do I (sometimes) get a REF ref and not a SCALAR ref?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |