Subject: | Better handling of pseudo-hashes |
Yes, I have read the documentation and the note about pseudohashes
being deprecated. However in perl/5.8.3 with "use fields" still
pseudohashes are used, and this little patch greatly enhances the
support of that; here some comments about the individual changes:
1. $ref = ref($ref)
it seems that isObject is not really in use, but the code in
sub isObject choked on objects with overloaded "" (stringify)
operator. The additional line fixes this - altough the entire
code seems to be removable
2. =~ /^(pseudohash|HASH)$/
I don't know which perl version introduced that - but the ref
command returns 'pseudohash' here, and not HASH. This is the key
change of the patch
3. The index check must consider the upper limit defined in the
$item->[0] hash, not the number of items in @$item - as it seems
that the array is extended dynamically
4. some minor code compaction - no change in functionality (I hope)
Thanks for the great code and keep up the good work!
Cheers,
Marek
--- ObjScanner.pm.orig Fri Nov 28 18:16:19 2003
+++ ObjScanner.pm Wed Feb 4 11:46:52 2004
@@ -254,6 +254,7 @@
{
my $ref = shift ; # not a method !
return 0 unless $ref ;
+ $ref = ref($ref);
return
scalar grep ($ref eq $_, qw/REF SCALAR CODE GLOB ARRAY HASH/) ?
0 : 1;
}
@@ -269,19 +270,17 @@
$cw->{viewpseudohash} &&
isa($item,'ARRAY') &&
scalar @$item &&
- ref $item->[0] eq 'HASH');
+ ref($item->[0]) =~ /^(HASH|pseudohash)$/);
+
my @indexes = values %{ $item->[0] } ;
+ my $ret = scalar keys %{ $item->[0] } ;
- return 0 if scalar grep( /\D/, @indexes ) or
- scalar grep( $_ > $#$item,@indexes ) ;
+ # check that all indexes are numbers and within the range
+ return 0 if scalar grep( /\D/ || $_ < 1 || $_ > $ret, @indexes );
- my $ret = scalar keys %{ $item->[0] } ;
+ # check that not more array items than in the range are defined
return 0 unless $ret >= scalar @$item - 1;
- for (values %{ $item->[0] })
- {
- return 0 if ($_ !~ /^\d+$/ || $_ < 1);
- }
return $ret ;
}