in reply to Truthfulness of references
return_ref, as its name announces, returns a reference to a hash. That reference gets assigned to a scalar variable ($ref) as is fine an proper. A rather bogus test for "truth" is then made to see if $_ is a reference to anything (is that what you thought the test actually was?).
I'm not sure what issue you are trying to illustrate, however the following code may provide a little illumination:
use strict; use warnings; my $scalar = 'a string is a scalar'; print ref ({}), "\n"; print ref ([]), "\n"; print ref (ret_hash ()), "\n"; print ref (ret_array ()), "\n"; print ">", ref ($scalar), "<\n"; print ref (\$scalar), "\n"; sub ret_hash {my %hash; return \%hash;} sub ret_array {my @array; return \@array;}
Prints:
HASH ARRAY HASH ARRAY >< SCALAR
ref tells you something about the type of reference you point it at (but nothing about scalars). ref on it's own tells you about whatever reference $_ contains (which is the test you were making in your if).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Truthfulness of references
by njcodewarrior (Pilgrim) on Mar 13, 2007 at 11:08 UTC | |
by Anno (Deacon) on Mar 13, 2007 at 13:11 UTC |