Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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 ( [id://1166383]=note: print w/replies, xml ) Need Help??


in reply to Re: Why do I (sometimes) get a REF ref and not a SCALAR ref?
in thread Why do I (sometimes) get a REF ref and not a SCALAR ref?

It's worth noting that circularity is not required, nor is it required that you are dealing with a scalar:
perl -E 'say \\$x; say \\@x; say \\%x'
outputs
REF(0x735d48) REF(0x735d48) REF(0x735d48)
Seems like an odd design choice since it makes crawling an arbitrary reference tree harder and I wouldn't think it adds any useful information. Anyone know a justification?

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^3: Why do I (sometimes) get a REF ref and not a SCALAR ref?
by choroba (Cardinal) on Jun 23, 2016 at 17:32 UTC
    Interestingly, using all the expressions in one command:
    perl -E 'say for \\$x, \\@x, \\%x;'

    gives a different output:

    REF(0x1204e78) REF(0x12251f8) REF(0x1225240)

    If you don't store the reference, it's freed and reused later.

    $ perl -E 'say \\$x; say \\@y; say \\%z;' REF(0xcfae78) REF(0xcfae78) REF(0xcfae78)

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      To finish the set:
      perl -E '@arr=(\\$x,\\@x,\\%x);say \\$x;say \\@x;say \\%x'
      outputs
      REF(0x8e8f58) REF(0x8e8f58) REF(0x8e8f58)
      and
      perl -E '@arr=(\\$x,\\@x,\\%x);say join"\n",\\$x,\\@x,\\%x'
      outputs
      REF(0xdc3f58) REF(0xdc3e38) REF(0xde0618)
      and
      perl -E '@arr=(\\$x,\\@x,\\%x);say $arr[0];say $arr[1];say $arr[2]'
      outputs
      REF(0x19b4d48) REF(0x19b4b98) REF(0x19d13e8)
      I think what's going on is that we're getting the address of a temporary variable. When they are all in the same statement, you need three temporary spots; when in different statements, you just reuse your scratch space.

      Still doesn't answer the 'REF' question, though...


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re^3: Why do I (sometimes) get a REF ref and not a SCALAR ref?
by talexb (Chancellor) on Jun 23, 2016 at 15:26 UTC

    My understanding is the three variables you listed are actually stored in the same dictionary entry, but as 'x (as a scalar)', 'x (as an array)' and 'x (as a hash)'. The appropriate value or list is returned based on the context.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      Package vars are stored in an instance of a struct called a typeglob, glob or GV.

      The appropriate variable is fetched when needed.

      $ perl -E' say \*x; # Prints the address of the glob. say \@x; # Prints the address of the array. say *x{ARRAY}; # Prints the address of the same array. say \%x; # Prints the address of the hash. say *x{HASH}; # Prints the address of the same hash. ' GLOB(0x2f78fd8) ARRAY(0x2f79080) ARRAY(0x2f79080) HASH(0x2f79038) HASH(0x2f79038)

      None of this is relevant here. The program is printing the address of three different anonymous scalars that all got allocated at the same address (which is possible since none of the three existed at the same time).

      That's what I thought, but choroba's reply implies it's just really fast garbage collection or persistent scratch space. This gets into deeper waters than I'm used to.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1166383]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found