in reply to Re: Initializing Hash Arrays in Perl
in thread Initializing Hash Arrays in Perl

PHP does have autovivification, but only on write operations.

<?php $arr = array(); $arr[0]['a'][1]['b'] = 2; print_r($arr); $other = array(); if ($other[0]['a'][1]['b'] == 2) { die; } else { # Note that autovivification has not happened. print_r($other); }

Versus:

#!/usr/bin/env perl use strict; use Data::Dumper; my @arr; $arr[0]{'a'}[1]{'b'} = 2; print Dumper \@arr; my @other; if ($other[0]{'a'}[1]{'b'} == 2) { die; } else { # Note that autovivification has happened! print Dumper \@other; }

For the purposes of autovivification, ++ is considered a write operation.

The Perl module autovivification allows you to have approximately the PHP behaviour:

no autovivification;
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name