Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Why do I (sometimes) get a REF ref and not a SCALAR ref? (silly)

by tye (Sage)
on Jun 23, 2016 at 18:39 UTC ( [id://1166403]=note: print w/replies, xml ) Need Help??


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

Yeah, you can explain this behavior. But it is, frankly, stupid. \\$x is, indeed, a reference to a scalar and so ref should return "SCALAR". Having a special case for "reference to a scalar that happens to currently be holding a reference" is silly. It just leads to code having to check for both "SCALAR" and "REF", adding complexity. It makes about as much sense as having ref(\\\$x) return "REFREF" or ref(\\@x) return "ARRAYREF" or ref([]) return "EMPTYARRAY".

- tye        

Replies are listed 'Best First'.
Re^2: Why do I (sometimes) get a REF ref and not a SCALAR ref?
by Anonymous Monk on Jun 23, 2016 at 19:53 UTC

    The "ARRAY" references can be dereferenced via @{ $ref }.
    The "HASH" references can be dereferenced via %{ $ref }.
    The "REF" references can be dereferenced via ${ $ref }.

    Silly or not, but a self-circular reference can be followed ad infinitum; you never arrive at plain "SCALAR" value.

      Circularity has nothing to do with whether "REF" or "SCALAR" is returned. "SCALAR" references can be dereferenced via ${ $ref }, exactly like "REF" references, which argues for not making that distinction. Code that is going to traverse from $sv to $$sv is often also code that will traverse from checking ref $av to checking ref $av->[0] and from checking ref $hv to checking ref $hv->{$key} and so can cycle infinitely for any of those cases.

      - tye        

      Uh, addendum. (Getting confused myself?) I should've said:

      Both "SCALAR" and "REF" can be dereferenced via ${ $ref }, but only the "SCALAR" can be used as a honest scalar via that deref.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found