I'm not sure I actually responded to the real question. I still think naming a class 'ARRAY' is begging for trouble.
But here a snippet that knows the difference:
$aref = []; $href = {}; $sref = \"";
my $text = 'some text';
my $ref = \$text;
my $blessed = bless($ref, 'ARRAY');
print prt_typ($_) for ( [ $aref, '$aref'], [ $href, '$href'],
[ $sref, '$sref'], [$blessed, '$blessed'], );
sub prt_typ
{
my ( $rin, $str ) = @{$_[0]};
print "ref($str) = ", ref($rin), "\n";
eval { my $x = ${$rin} };
return "$str is SCALAR\n\n" unless $@;
eval { my %x = %{$rin} };
return "$str is HASH\n\n" unless $@;
eval { my @x = @{$rin} };
return "$str is ARRAY\n\n" unless $@;
}
Results in:
ref($aref) = ARRAY
$aref is ARRAY
ref($href) = HASH
$href is HASH
ref($sref) = SCALAR
$sref is SCALAR
ref($blessed) = ARRAY
$blessed is SCALAR
Bob Niederman, http://bob-n.com |