in reply to Why do I (sometimes) get a REF ref and not a SCALAR ref?
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: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why do I (sometimes) get a REF ref and not a SCALAR ref?
by kennethk (Abbot) on Jun 23, 2016 at 14:55 UTC | |
by choroba (Cardinal) on Jun 23, 2016 at 17:32 UTC | |
by kennethk (Abbot) on Jun 23, 2016 at 18:49 UTC | |
by talexb (Chancellor) on Jun 23, 2016 at 15:26 UTC | |
by ikegami (Patriarch) on Jun 24, 2016 at 17:07 UTC | |
by kennethk (Abbot) on Jun 23, 2016 at 18:39 UTC | |
|
Re^2: Why do I (sometimes) get a REF ref and not a SCALAR ref?
by Anonymous Monk on Jun 23, 2016 at 12:33 UTC |