One slight problem, in v5.8.8 (ActiveState build 820), this reports that the array reference is a hash reference: use strict;
use warnings;
my $aref = [qw(1 2 3 4)];
if ( eval { %$aref; 1 } ) {
print "aref is a href\n";
my %hash = %$aref;
}
But it dies with Can't coerce array into hash when you try to dereference it outside of the eval. In 5.6.1, it does not report that the array ref is a hash ref. This seems to fix it:if ( eval { my $foo = %$aref; 1 } ) {
print "aref is a href\n";
my %hash = %$aref;
}
In other code similar to the first instance ( but not that particular code for some reason), I did get the warning:Pseudo-hashes are deprecated
which explains why an array reference could be considered a hash reference.
Update: What I've ended up using is this: sub isa_hash {
no warnings 'void';
my $hsh = shift();
return if eval { @$hsh; 1 };
return 1 if eval { %$hsh; 1 };
return;
}
sub isa_array {
no warnings 'void';
my $arr = shift();
return 1 if eval { @$arr; 1 };
return;
}
UNIVERSAL::isa() is looking simpler all the time (even if wrong in obscure instances).
Another Update: Borrowed from diotalevi's use perl journal: if ( eval { 1 + %$possible_href } ) {
return 1;
}
|