Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Strange memory growth

by Marshall (Canon)
on Feb 15, 2018 at 06:36 UTC ( [id://1209193]=note: print w/replies, xml ) Need Help??


in reply to Strange memory growth

I updated my post with some more test cases. defined and exists behave similarly in regards to generating intermediate keys in the process of doing their work. I show an example of how to prevent this below.

I don't know if this helps or not, but checking for defined vs or exists can generate hash entries.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ('key' => 'value'); print Dumper \%hash; #prints: #$VAR1 = { # 'key' => 'value' # }; print "defined\n" if defined $hash{abc}{xyz}; # will create new keys print "defined\n" if defined ($hash{x}) and defined ($hash{x}{y}); # a +dded: won't create new keys print "exists\n" if exists ($hash{x}) and exists($hash{x}{y}); # a +dded: won't create new keys print "exists\n" if exists $hash{x1}{x2}; # added: will create new +keys print "defined\n" if defined $hash{x3}; # added: no new key print Dumper \%hash; __END__ #prints: # The "abc" key is created due to the check for defined of 2nd dimensi +on # exists will also create new intermediate keys $VAR1 = { 'key' => 'value', 'abc' => {}, 'x1' => {} };
Sometimes you have to check at each level if the hash "dimension" exists. A check for "defined" or "exists" can generate automatic dimensions.
Sometimes you have to you have to check whether some hash key exists at all before checking what its value is (defined or not).

Replies are listed 'Best First'.
Re^2: Strange memory growth
by Anonymous Monk on Feb 15, 2018 at 13:29 UTC
    So are you saying that Huck's post should have used exists, instead of defined? To be absolutely clear, can you post a quick example similar to Huck's? Auto-vivification is obviously the root cause problem here ... but are you saying that defined will auto-viv too? (That does seem counter-intuitive, but ...)

      No difference between exists and defined in this regard. In both cases, the *intermediate* levels will spring into existance. The last level isn't dereferenced as a hash.

      If you have perl installed, you can try it out yourself:

      $ perl -e 'use Data::Dump; exists $a->{foo}{bar}; dd $a;' { foo => {} } $ perl -e 'use Data::Dump; defined $a->{foo}{bar}; dd $a;' { foo => {} }

        I guess that I'm still confused. When I look at Huck's solution, I see that short-circuit conditional expression optimization will stop the evaluation at the first "false" result. Therefore, as far as I can see, no auto-vivification would occur. My question was therefore whether it actually mattered which function-call Huck used.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-03-28 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found