Subject: | type checking for blessed arrayrefs |
I found some cases that Data::Path does not work on blessed arrayrefs.
The following output will be produced when the attached program is
executed:
trie to retrieve an index 0 from a no array value (in key k)
at t.pl line 13
hello
Can't coerce array into hash
at /usr/local/share/perl/5.8.8/Data/Path.pm line 75.
Can't coerce array into hash
at /usr/local/share/perl/5.8.8/Data/Path.pm line 81.
(I ran this script with perl 5.8.8.)
I think the correct output should be:
hello
hello
hello
tried to retrieve a key from a no hash value (in key a)
at t.pl line 16
This seems to be because Path.pm assumes blessed objects are always
hashrefs.
I think it is better to check types of objects before dereferencing
hashrefs, like this:
if not exists $data->{$key} and $rest;
--> if UNIVERSAL::isa($value,'HASH') and
not exists $data->{$key} and $rest;
Type-checking like "ref($value) eq 'ARRAY'" will not work on blessed
arrayrefs. Using UNIVERSAL::isa will be more general.
Will you please consider to modify Data::Path to work on more general
types of objects?
Regards,
Yoshikazu
Subject: | t.pl |
#!/usr/bin/perl
use Data::Path;
my $ary = bless(['hello'], 'myarray');
sub myarray::fst {
shift->[0];
}
# print $ary->fst, "\n";
my $t1 = { k => $ary };
# my $t1 = { k => ['hello'] };
my $dp1 = Data::Path->new($t1);
eval{ print $dp1->get('/k[0]'), "\n"; } || print $@;
eval{ print $dp1->get('/k')->[0], "\n"; } || print $@;
eval{ print $dp1->get('/k/fst()'), "\n"; } || print $@;
eval{ print $dp1->get('/k/a'), "\n"; } || print $@;