Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: ref eq "REF"

by edan (Curate)
on Oct 18, 2004 at 12:54 UTC ( [id://400129]=note: print w/replies, xml ) Need Help??


in reply to Re^2: ref eq "REF"
in thread ref == "REF"

What's unreliable about it? ref tells you "the type of thing the reference is a reference to." (quote from ref). You're used to using references to hashes and arrays (I assume), so it makes sense to you that ref(\%hash) returns HASH, since \%hash is obviously a reference to a hash. So isn't it perfectly logical (and reliable) that ref(\\$anything) returns REF, since the reference \\$anything is a reference to a reference?

--
edan

Replies are listed 'Best First'.
Re^4: ref eq "REF"
by gaal (Parson) on Oct 18, 2004 at 14:18 UTC
    Hmmm, unreliable was probably the wrong word. But (still fishing for uses for this construct) if you'r using REF as an indicator for "there's something yet to dereference here", you're in for a surprise, because it isn't a comrehensive indicator.

    *r = \"moose"; print ref \*r; # GLOB print ${*r}; # moose

    So the precise meaning of REF seems to be "a reference to something, and nothing else".

      Sorry, I'm lost. Either I'm not understanding you, or you're not understanding me, or most likely both :)

      The function ref is used to determine if its argument is a reference: if ref returns a true value, then its argument is a reference to something, right?

      As an added bonus, it tells you what that reference is referring to. Therefore the following code makes everyone happy, right?

      #!perl -l my $s = "foo"; # a scalar print ref($s); # prints nothing: $s is a scalar, not a ref my $sr = \$s; # a reference to a scalar print ref($sr); # prints SCALAR my %h; # a hash print ref(%h); # prints nothing: %h is a hash, not a ref my $hr = \%h; # a reference to a hash print ref($hr); # prints HASH my $hrr = \$hr; # a reference to a reference to a hash print ref($hrr); # prints REF

      So the precise meaning of REF is "a reference to a reference (to anything - sorry, it only goes one level deep)"

      --
      edan

        Fine; where would you find this useful? (Viz, with added value over SCALAR, which up till today I'd have interpreted as "(reference to) a scalar value, which might be a number, a string, or a reference" (inclusive or here).

        My problem arises from a seeming inconsistency of the concept "scalar" in Perl. In the guts, there are IVs and NVs and PVs and RVs; but the user just wants his $scalar, with all these possible values conflated. I'd venture to say that this conflation is a widely accepted feature by Perl programmers. Note how another, well-known breach of this generalization — you can't use references as hash keys — is often regarded as suprising (and frustrating) behavior the first time someone encounters it (I think!). Certainly from the user perspective, of the Perl language, it isn't a useful feature, but presumably it makes more sense from the implementation, perl, point of view.

        So is ref returning REF on \\"moose" just some leftover hairiness? Or is there a nice hack waiting to be found here?

      I don't now what that example is trying to show, but let's expand:

      perl -l *r = ["goose"]; *r = \"moose"; print ref \*r; print ${*r}; print @{*r}; __END__ GLOB moose goose

      So you see, GLOB is a useful value in this case. In anycase - if ref() returns a true value, THAT means there is something to dereference here.

      Just to make it clear: ref($r) returns "REF" if $r is a reference to a reference.

      On the other hand, I agree that the REF return value just adds unnesessary complexity and returning SCALAR for a ref-to-a-ref would have been more consistent and simpler (just like an arrayref returns ARRAY, and doesn't care about the elements of the array).

        Certainly GLOB is a useful value. The purpose of the example was to show that REF is less useful than it would seem because it is not exhaustive. Other things -- GLOBs -- can also hold references, so when writing code that decides whether to recurse further you need to either match against multiple values or call ref again anyway.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found