The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Path - Perl extension for XPath like accessing from complex data structs

SYNOPSIS

  use 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 } 
        );

DESCRIPTION

XPath 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.

CALLBACKs

The 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 callback

  my $hpath=Data::Path->new
        ($hashdata,
        { key_does_not_exist   => sub { die key not found } 
        { index_does_not_exist => sub { die index not found } 
        );

EXPORT

None by default.

SEE ALSO

        XPath
 

TODO

Slices 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

AUTHOR

Marco Schrieck, <marco.schrieck@gmx.de>

Jeremy Wall jeremy@marzhillstudios.com

COPYRIGHT AND LICENSE

Copyright (C) 2006 by Marco Schrieck Copyright (C) 2007 by Jeremy Wall

This 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.