Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

5.24 -> 5.28 -- what has changed in autovivification?

by leszekdubiel (Scribe)
on Apr 17, 2020 at 13:42 UTC ( [id://11115682]=perlquestion: print w/replies, xml ) Need Help??

leszekdubiel has asked for the wisdom of the Perl Monks concerning the following question:

Hello!

Here is the program, and below results for perl 5.24 and 5.28:

#!/usr/bin/perl -CSDA use utf8; use Modern::Perl qw{2017}; no autovivification qw{fetch store exists delete}; my $h = { 'a' => { 'p' => 2, 'q' => 3, }, }; print "perl ver $]:\n"; $$h{a} and print "a ok\n"; ! $$h{b} and print "b not\n"; ! $$h{a}{x} and print "a/x not\n"; ! $$h{a}{x}{z} and print "a/x/z not\n"; perl ver 5.024001: a ok b not a/x not a/x/z not perl ver 5.028001: a ok b not a/x not Can't vivify reference at ./atest line 18.

Could someone please explain what has changed between 5.24 and 5.28 in regard of autovivification? And what would be the proper way to check existence of value in last line in ver 5.28?

ver 5.24 good way: ! $$h{a}{x}{z} and print "a/x/z not\n"; ver 5.28 good (?) way: ! ( $$h{a} && $$h{a}{$x} && $$h{a}{x}{z}) and print "a/x/z not\n"; # + ?? any better idea?

PS. I have to prevent perl from modifying my data structure "behind the scenes", because that structures get mapped to textual data that goes to manufacturing management system, and if any undef is inside data structure then error is thrown, because that means that someone made something wrong.

Replies are listed 'Best First'.
Re: 5.24 -> 5.28 -- what has changed in autovivification?
by haukex (Archbishop) on Apr 17, 2020 at 14:19 UTC

    I can't reproduce this, the output includes the "Can't vivify reference" error under both versions of Perl. Perhaps you're using different versions of the autovivification module?

    And what would be the proper way to check existence of value in last line in ver 5.28?

    The proper core-only way to check in any version of Perl is exists $$h{a} && exists $$h{a}{x} && exists $$h{a}{x}{z}. Using exists allows you to differentiate between a key not existing, a key existing and its value not being defined, and a key existing and having a false value. With no autovivification and if you know your values are always true, I guess you could say $$h{a} && $$h{a}{x} && $$h{a}{x}{z}, but I don't have much experience with that module.

    Note that there are other modules you can use to look at data structures without autovivifying them, without changing the behavior of core Perl, such as Data::Diver.

      Thank you for hints. I will check diver module.

      PS. In case anybody has the same problem versions were from Debian Stretch and Buster installations:

      # command: # perl -e 'print $], "\n"; ' ; dpkg -l | egrep autovivifi # Debian buster: 5.028001 ii libautovivification-perl 0.18-1+b1 amd +64 pragma for lexically disabling autovivification # Debian stretch: 5.024001 ii libautovivification-perl 0.16-1+b2 + amd64 pragma for lexically disabling autovivification

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11115682]
Approved by hippo
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-23 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found