![]() |
![]()
| ![]() |
![]()
NAMEData::Path - Perl extension for XPath like accessing from complex data structsSYNOPSISuse Data::Path; my $hashdata={ result => { msg => [ { text => 'msg0' } , { text => 'msg1' } , { text => 'msg2' } ] }, method => sub {'method text'} } my $hpath=Data::Path->new($hashdata); my $value= $hpath->get('/result/msg[1]/text'); my $value2 = $hpath->get('/method()'); print "OK" if $value2 eq 'method text'; print "OK" if $value eq 'msg1'; my $hpath=Data::Path->new($hashdata,$callback); my $hpath=Data::Path->new ($hashdata, { key_does_not_exist => sub { die index not found } ); DESCRIPTIONXPath like access to get values from a complex data structs.key_does_not_exist / index_does_not_exist are only called if it was not the last part of the path. If the last part of path is not exists undef is returned. CALLBACKsThe default callbacks but you can overwrite this.{ key_does_not_exist => sub { my ($data, $key, $index, $value, $rest )=@_; croak "key $key does not exists\n"; } , index_does_not_exist => sub { my ($data, $key, $index, $value, $rest )=@_; croak "key $key\[$index\] does not exists\n"; } , retrieve_index_from_non_array => sub { my ($data, $key, $index, $value, $rest )=@_; croak "trie to retrieve an index $index from a no array value (in key $key)\n"; } , retrieve_key_from_non_hash => sub { my ($data, $key, $index, $value, $rest )=@_; croak "trie to retrieve a key from a no hash value (in key $key)\n"; } , not_a_coderef_or_method => $callback->{not_a_coderef_or_method} || sub { my ($data, $key, $index, $value, $rest )=@_; croak "tried to retrieve from a non-existant coderef or method"; } } EXMAPLE overwrite callbackmy $hpath=Data::Path->new ($hashdata, { key_does_not_exist => sub { die key not found } { index_does_not_exist => sub { die index not found } ); EXPORTNone by default.SEE ALSOXPath TODOSlices of data through /foo[*]/bar syntax. eg. retrieve all the bar keys from each element of the foo array.allow accessing the results of coderefs eg. /foo()/bar retreive the result of the foo coderef/method of the object AUTHORMarco Schrieck, <marco.schrieck@gmx.de>Jeremy Wall jeremy@marzhillstudios.com COPYRIGHT AND LICENSECopyright (C) 2006 by Marco Schrieck Copyright (C) 2007 by Jeremy WallThis library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.
|