Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Autovivify Module?

by InfiniteSilence (Curate)
on Sep 08, 2003 at 21:51 UTC ( [id://289882]=perlquestion: print w/replies, xml ) Need Help??

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

I have come across an interesting problem:
perldoc -f exists undef $ref; if (exists $ref->{"Some key"}) { } print $ref; # prints HASH(0x80d3d5c)
The documentation goes further to say:
This surprising autovivification in what does not at first--or even second--glance appear to be an lvalue context may be fixed in a future release.
Normally I would not care that perl is creating keys in deeply nested hashes so long as I know that the value is undef, but if I grab a list of keys without thinking:
C:\>perl -e "undef $ref; if (exists $ref->{'key1'}->{'key2'}) {print 'I think i am okay, but...'}; print keys %{$ref}" key1
So, is there a module out there that will control the way perl autovivifies (kind of like AutoLoader but for variables) nested structures?

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re: Autovivify Module?
by Abigail-II (Bishop) on Sep 08, 2003 at 22:02 UTC
    So, is there a module out there that will control the way perl autovivifies

    No. If you want to prevent autovivification, you have to code appropriately:

    undef $ref; if ("HASH" eq ref $ref && exists $ref -> {"Some key"}) { }

    If you can't assume that $ref is a reference to a hash, you'd need extra testing anyway, even if there wasn't autovivification. For instance:

    $ref = []; if (exists $ref -> {key}) { }

    will give you a Can't coerce array into hash error.

    Abigail

Re: Autovivify Module?
by Limbic~Region (Chancellor) on Sep 08, 2003 at 21:55 UTC
    InfiniteSilence,
    A quick search turned up Hash::NoVivify, which only works by exporting functions Exists and Defined as alternatives to exists and defined, but that looks like what you want anyway

    Cheers - L~R

    Update: Italic verbiage thanks to Abigail's comment.

      Well, that module doesn't really turn off autovivification. It just defines functions 'Defined' and 'Exists', with quite different syntax from 'defined' and 'exists'.

      Abigail

What does Autovivify mean?
by bm (Hermit) on Sep 09, 2003 at 06:37 UTC
    I hope this is not a silly question.
    Can someone please define autovivify for me? I have seen it around the monastery but have never found a clear explanation...
    Thanks
    --
    bm

      Vivify:to give life to; bring to life; to animate.

      my $hash; print "\$hash is: ", defined $hash ? $hash : "undefined", "\n"; if ($hash->{one}){ print "'one' exists\n"; } print "\$hash is: ", defined $hash ? $hash : "undefined", "\n";

      $hash is undefined, but when we use it as if it were a reference to a hash ($hash->{one}) perl automatically brings a hash reference to life and stores it in $hash. Hence, it autovivifies the hash reference for us.

      It's documented in perlref(tut), perlfaq4(*), perlfunc as well as at "Autovivification".

      If you're wondering how I know, I searched my pod for autovivi (and just typed in Autovivification in the search box above).

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Autovivify Module?
by Roger (Parson) on Sep 08, 2003 at 23:17 UTC
    In your example, the $ref is actually a reference to an empty hash, not a hash itself. It was automatically created in the if statement. Therefore print $ref will print out the hash location $ref points to.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-24 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found