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::FetchPath - "eval"able paths to your complex data values

VERSION

Version 0.02

SYNOPSIS

Quick summary of what the module does.

use Data::FetchPath 'path';
use Test::Most;

my $data = {
    foo => 3,
    bar => [qw/ this that 3 /],
    3   => undef,
    baz => {
        trois  => 3,
        quatre => [qw/ 1 2 3 4 /],
    }
};
my $target_value = 3;
ok $paths = path( $data, $target_value ), 'Fetching paths for matching data should succeed';
my @expected = sort qw(
  {bar}[2]
  {baz}{trois}
  {baz}{quatre}[2]
  {foo}
);
eq_or_diff $path, \@expected,
    '... and it should return all paths to data values found';
for ( 0 .. $#expected ) {
    my $found_value = eval "\$data->$expected[$_]";
    is $found_value, $target_value,
        '... and all values should match the value you looked for';
    }
}

EXPORT

FUNCTIONS

path

Exported on demand via:

use Data::FetchPath 'path';
my $paths = path($data_structure, $value);
my $paths = path($data_structure, $regex);

Passed a data structure and either a scalar value or a regex (qr/foo.*bar/), this function will return an array reference to the paths to said value. Each path is suitable for using eval against said data structure:

my %data = (
    one   => 'uno',
    two   => 'dos',
    three => 'tres',
);
# find values with the letter 'o' in them
my $paths = path(\%data, qr/o/);
foreach my $path (@$data) {
    print eval "\$data$path\n";
}
__END__
uno
dos

Currently the data structure must be an array or hash reference. The value must be a scalar or a regular expression.

AUTHOR

Curtis "Ovid" Poe, <ovid at cpan.org>

BUGS

Please report any bugs or feature requests to bug-data-fetchpath at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-FetchPath. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Data::FetchPath

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

Copyright 2008 Curtis "Ovid" Poe, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.