Re^3: ref eq "REF"
by edan (Curate) on Oct 18, 2004 at 12:54 UTC
|
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?
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
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". | [reply] [Watch: Dir/Any] [d/l] |
|
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)"
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
|
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).
| [reply] [Watch: Dir/Any] [d/l] |
|
Re^3: ref eq "REF"
by Jasper (Chaplain) on Oct 18, 2004 at 12:24 UTC
|
I'm sure Data::Dumper couldn't have been written without ref to a reference returning REF. | [reply] [Watch: Dir/Any] |
|
Why's that? It could have worked just fine if ref had returned SCALAR in this case.
In fact, there's only place in Dumper.pm where REF is mentioned at all. Here's what it says:
if ($realtype eq 'SCALAR' || $realtype eq 'REF')
Clearly, Dumper would have lived just fine with "a reference is just a scalar" semantics.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
So it would seem. I was just thinking that the reference is a reference, a SCALAR is something different would seem to be the easier. As you say, Data::Dumper would have lived just fine (better, even). I should have looked at the source myself before writing that.
| [reply] [Watch: Dir/Any] |