The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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' }
                        ]
        }
  }
  
  my $hpath=Data::Path->new($hashdata);
  my $value= $hpath->get('/result/msg[1]/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 )=@_;
                        die "key $key does not exists\n";
                }

        , index_does_not_exist         =>
                sub {
                        my ($data, $key, $index, $value, $rest )=@_;
                        die "key $key\[$index\] does not exists\n";
                }

        , retrieve_index_from_non_array =>
                sub {
                        my ($data, $key, $index, $value, $rest )=@_;
                        die "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 )=@_;
                        die "trie to retrieve a key from a no hash value (in key $key)\n";
                }
        }

        

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 

AUTHOR

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

COPYRIGHT AND LICENSE

Copyright (C) 2006 by Marco Schrieck

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.