2

Consider this one piece of Perl code,

$array[$x]->{“foo”}->[0]= “January”;

I analyze this code as following: The entry with index $x in the "array" is a hashref. With respect to this hash, when its key is “foo”, its value is an array and the 0-th element for this array is “January”. Is my analysis correct or not? Thanks.

bit-question
  • 3,733
  • 17
  • 50
  • 63

3 Answers3

11

Your analysis of the structure is correct, however the related autovivification example would be something more like:

#!/usr/bin/env perl

use strict;
use warnings;

use 5.10.0; # say

my @array;

# check all levels are undef in structure

say defined $array[0] ? 'yes' : 'no';         # no
say defined $array[0]{foo} ? 'yes' : 'no';    # no
say defined $array[0]{foo}[0] ? 'yes' : 'no'; # no

# then check again

say defined $array[0] ? 'yes' : 'no';         # yes (!)
say defined $array[0]{foo} ? 'yes' : 'no';    # yes (!)
say defined $array[0]{foo}[0] ? 'yes' : 'no'; # no

Notice that you haven't assigned anything, in fact all you have done is to check whether something exists. Autovivification happens when you check a multilevel data structure at some level x, then suddenly all levels lower (x-1 ... 0) are suddenly existent.

This means that

say defined $array[0]{foo}[0] ? 'yes' : 'no';

is effectively equivalent to

$array[0] = {};
$array[0]{foo} = [];
say defined $array[0]{foo}[0] ? 'yes' : 'no';
Joel Berger
  • 20,180
  • 5
  • 49
  • 104
  • "This means that...is effectively equivalent to" assumes @array is empty (or at least that none of the elements used already exists). – ysth Dec 22 '11 at 06:02
  • @ysth, conceeded, yet still close enough for government work right? – Joel Berger Dec 22 '11 at 10:20
4

Yes, your analysis is correct.

It is NOT howerver, an analysis of autovivification, it is an analysis of a multilevel data structure.

We cannot know if there is autoviv going on here or not, because we cannot determine whether any of the intermediate values are undef...

tadmc
  • 3,714
  • 16
  • 14
1

tadmc has the correct answer. Your analysis is correct. The autovivification is something else, however. Consider this example:

perl -wE 'my @a; $a[1]->{foo}->[0] = "aa"; use Data::Dumper; print Dumper \@a;'

$VAR1 = [
          undef,
          {
            'foo' => [
                       'aa'
                     ]
          }
        ];

In this example, we only declare an array @a. But by assigning a value to an imagined structure in @a, we automatically create it. We add a hash reference to the second element in @a, and an array reference to the key foo in that hash. This is not something we declared, perl autovivifies it for us.

TLP
  • 66,756
  • 10
  • 92
  • 149
  • I should mention that TLP's is an example of autovivification too; only its the kind that people might expect. I posted mine as an example of the kind of autovivification that people might not expect (and some people wish Perl wouldn't do), namely modify something just by looking at it (Heisenberg anyone?). When you see people complaining about autovivification, thats the problem. Cheers – Joel Berger Dec 22 '11 at 05:26
  • @JoelBerger I guess I was looking more at the type of autovivification that occurred in his example code. Your example is better for demonstrating it's subtle points, though. – TLP Dec 22 '11 at 05:31