Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: searching nested structures

by exussum0 (Vicar)
on Jan 05, 2004 at 02:12 UTC ( [id://318743]=note: print w/replies, xml ) Need Help??


in reply to searching nested structures

Update: It's the eval as roger implicitly pointed out in his reply as well. Time to be explicit. From man perlfunc on exists

Given an expression that specifies a hash element or array ele- ment, returns true if the specified element in the hash or array has ever been initialized, even if the corresponding value is undefined. The element is not autovivified if it doesn't exist.

You are autovifying.


You can see this by modifying your code as such.. (use Data::Dumper)
. . . print "----\n"; print Dumper $data; my $ref = qq|\$data->{$k}$path|; print "String: $ref\n"; if (my $val = eval($ref)) { push @results, ref($val) ? $val : eval "\\$ref"; } if (exists $data->{$k}) { print Dumper $data; push @results, rec_data($path, $data->{$k}); . . .
Your eval is creating the a-formentioned structure. You may wanna break down your search structure in a different way.
Here's the output from above. You are creating a circular reference.
$VAR1 = { 'foo' => { 'baz' => 'baz', 'bar' => 'bar' }, 'bar' => { 'baz' => 'baz' } }; String: $data->{foo}->{foo}->{bar} $VAR1 = { 'foo' => { 'foo' => {}, 'baz' => 'baz', 'bar' => 'bar' }, 'bar' => { 'baz' => 'baz' } };

Play that funky music white boy..

Replies are listed 'Best First'.
Re: Re: searching nested structures
by pg (Canon) on Jan 05, 2004 at 05:05 UTC

    add on top of what sporty quoted, exists() is just one case for autovivification. To be precise on autovivification: any attempt to read or test an element that does not exist, will create its ancestors.

    use Data::Dumper; use strict; use warnings; my $data = {"a" => 1}; $data->{"b"}->{"b"}->{"b"}->{"b"};#useless use of hash element in void + context? interesting error message. The useless use is actually not +that useless. print Dumper($data);

    this prints:

    $VAR1 = { 'a' => 1, 'b' => { 'b' => { 'b' => {} } } };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://318743]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-03-29 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found