renormalist / data-dpath Goto Github PK
View Code? Open in Web Editor NEWA perl lib to provide access to data structures inspired by XPath
Home Page: https://metacpan.org/pod/Data::DPath
A perl lib to provide access to data structures inspired by XPath
Home Page: https://metacpan.org/pod/Data::DPath
Since 0.57, just using Data::DPath will warn here:
% perl -Ilib -w -e 'use Data::DPath'
Name "main::INC" used only once: possible typo at /usr/share/perl/5.26/Safe.pm line 372.
This can be reduced to just
% perl -w -MSafe -e 'BEGIN { $c = Safe->new; $c->reval("no warnings") }'
Name "main::INC" used only once: possible typo at /usr/share/perl/5.26/Safe.pm line 372.
so some more Safe polishing seems to be in order.
Seen on Debian unstable, Perl 5.26.1. Original report is at https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=877093
Thanks for your work on Data-DPath.
#!/usr/bin/env perl
use 5.032;
use warnings;
use Data::DPath::Path;
my $query_path = '/*/*[2]/.[value eq "street"]/../*[3]/.[0]';
my $input_pl = [
[ 1, "SUCCESS", 1000, "" ],
[ 2, "ATTR", "street", "street1" ],
[ 3, "ATTR", "street", "street2" ]
];
my $dpath = Data::DPath::Path->new( path => $query_path );
my @result = $dpath->match($input_pl);
use Data::Dumper;
say Dumper(\@result);
this prints
$VAR1 = [
'street1',
'street2'
];
What path selects only one specific array item? For example, only street2
.
https://cpan.metacpan.org/authors/id/S/SC/SCHWIGON/Data-DPath-0.51.tar.gz : 8.1MB
https://cpan.metacpan.org/authors/id/S/SC/SCHWIGON/Data-DPath-0.50.tar.gz : 266kb
https://metacpan.org/source/SCHWIGON/Data-DPath-0.51/nytprof # Loads of cruft
https://metacpan.org/source/SCHWIGON/Data-DPath-0.51/nytprof.out # 7MB File
https://metacpan.org/diff/file?target=SCHWIGON/Data-DPath-0.51/&source=SCHWIGON/Data-DPath-0.50/ # Intrarelease diff that will choke your browser.
It would be nice to have a validate() method to verify correctness
of incoming DPaths.
One yould invent a "container syntax for dpath chains" which is easier to parse with the currently used balanced expression parser and even recognizable, like this:
{/this/is/path1}{/and/this/is/path2}{/and/even/a/path3}
Originally posted by @renormalist in #26 (comment)
Data::DPath::Context
isn't playing nice with Test::Exception
.
perl -MTest::Exception -MData::DPath::Context -e '1'
Number found where operator expected at /Users/jonathantaylor/perl5/perlbrew/perls/perl-5.26.0/lib/5.26.0/warnings.pm line 353, near "caller 1"
(Do you need to predeclare caller?)
perl -MTest::Exception -MData::DPath::Context -e '1'
Number found where operator expected at /Users/jonathantaylor/perl5/perlbrew/perls/perl-5.16.3/lib/5.16.3/warnings.pm line 444, near "caller 1"
(Do you need to predeclare caller?)
Reversing the load order stops the warning from happening:
perl -MData::DPath::Context -MTest::Exception -e '1' # No warning
The behavior has something to do with how the Safe
module is being used. Pull request forthcoming.
the different behaviour of single and double quotes inside of a filter is confusing to me and https://metacpan.org/pod/Data::DPath#Special-characters didnt entirely clear it up to us.
#!/usr/bin/env perl
use 5.018;
use warnings;
use Data::DPath 'dpath';
my $data = { foo => 'aaa[bbb/ccc]ddd' };
match( $data, q!/foo[value eq 'aaa\[bbb/ccc\]ddd']! );
match( $data, q!/foo[value eq 'aaa[bbb/ccc]ddd']! );
match( $data, q!/foo[value eq "aaa\\[bbb/ccc\\]ddd"]! );
sub match {
my ($input_pl, $query_path) = @_;
my $dpath = Data::DPath::Path->new( path => $query_path );
my @result = $dpath->match($input_pl);
say $query_path;
say scalar @result ? 'match' : 'no match';
}
perl dpath.pl
/foo[value eq 'aaa\[bbb/ccc\]ddd']
no match
/foo[value eq 'aaa[bbb/ccc]ddd']
no match
/foo[value eq "aaa\[bbb/ccc\]ddd"]
match
test test test
depending on the input of a "value eq ..." query I get a string or a HASH ref.
#!/usr/bin/env perl
use 5.018;
use warnings;
use Data::DPath::Path;
sub _match {
my ($path, $input) = @_;
my $dpath = Data::DPath::Path->new( path => $path );
my @result = $dpath->match($input);
use Data::Dumper;
say "###\n$path";
say Dumper(\@result);
}
my $input = { 'details' => 'foo bar baz' };
my $path1 = <<"HERE";
/details[ value eq 'foo bar baz' ]
HERE
my $path2 = <<"HERE";
/details[ value eq 'foo bar
baz' ]
HERE
_match($path1, $input);
_match($path2, $input);
###
/details[ value eq 'foo bar baz' ]
$VAR1 = [
'foo bar baz'
];
###
/details[ value eq 'foo bar
baz' ]
$VAR1 = [
{
'details' => 'foo bar baz'
}
];
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.