Subject: | Context::_search method does not return a new context as documented |
The documentation for DPath::Context::_search states that it returns a new context. It doesn't; it changes current_points on the *existing* context object. I've attached a test script "tst.pl" which illustrates the behavior.
Here's the output (which includes the important lines of code):
my $root = dpathi($RESULTS);
$root = {
'Benchmark' => [
'1.39033102989197',
'1.37',
0,
0,
0,
5
],
'data_size' => 254242,
'result' => '2',
'goal' => 15
};
-----------------------
my $benchmarks = $root->isearch('//Benchmark');
my $benchmark = $benchmarks->value;
$root = [
'1.39033102989197',
'1.37',
0,
0,
0,
5
];
$benchmark = [
'1.39033102989197',
'1.37',
0,
0,
0,
5
];
-----------------------
my $ancestors = $benchmark->isearch ('/::ancestor');
$root = [
'1.39033102989197',
'1.37',
0,
0,
0,
5
];
$benchmark = {
'Benchmark' => [
'1.39033102989197',
'1.37',
0,
0,
0,
5
],
'data_size' => 254242,
'result' => '2',
'goal' => 15
};
-----------------------
Note how each call to isearch changes the current points for the invoking object. Based on the documentation, I would expect isearch/_search to leave the invoking object unmodified.
When I modify the code in _search from
465 $self->current_points( $current_points );
466 return $self;
to
465 return __PACKAGE__->new->current_points( $current_points );
the behavior is as I expect, but results in a number of errors in the test suite.
Perhaps this behavior is by design?
I would like to use a context for several unrelated searches. The following code works to isolate changes to a new object,
my $new_context = Data::DPath::Context->new->current_points( $old_context->current_points );
but it seems counter-intuitive to have to do this.
If the code can't be changed, I'm willing to submit a documentation change to clarify things.
Thanks!
Diab
Subject: | tst.pl |
#! /usr/bin/env perl
use 5.10.0;
use lib 'lib';
use Data::DPath 'dpathi';
use Data::Dumper;
sub p {say Data::Dumper->Dump( @_ ) };
my $RESULTS = {
'goal' => 15,
'data_size' => 254242,
'Benchmark' => [ '1.39033102989197', '1.37', 0, 0, 0, 5 ],
'result' => '2'
};
my $root = dpathi($RESULTS);
say q|my $root = dpathi($RESULTS);|;
p [$root->_all], [ '$root' ];
say q|-----------------------|;
my $benchmarks = $root->isearch('//Benchmark');
say q|my $benchmarks = $root->isearch('//Benchmark');|;
my $benchmark = $benchmarks->value;
say q|my $benchmark = $benchmarks->value;|;
p [$root->_all], [ '$root' ];
p [$benchmark->_all], [ '$benchmark' ];
say q|-----------------------|;
my $ancestors = $benchmark->isearch ('/::ancestor');
say q|my $ancestors = $benchmark->isearch ('/::ancestor');|;
p [$root->_all], [ '$root' ];
p [$benchmark->_all], [ '$benchmark' ];
say q|-----------------------|;