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...):

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)
Note that the reference addresses are all the same.

$ 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:

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)
Again, all the reference addresses are the same.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.