$ perl -e '
use strict;
use warnings;
use Test::More tests => 8;
my $h = { b => {} };
my ($f, $f2, $f3, $f4);
$f = $h->{b}{c}{d} if exists($h->{b}{c}) and exists($h->{b}{c}{d})
+;
is_deeply( $h, { b => {} }, "no autovivification" );
is( $f, undef, "no value assigned" );
$f2 = $h->{b}{c}{d} if exists $h->{b}{c};
is_deeply $h, { b => {} }, "f2: no autovivification";
is $f2, undef, "f2: no value assigned";
$f3 = $h->{b}{c}{d}{x}{y}{z} if
exists $h->{b}{c}
and exists $h->{b}{c}{d}
and exists $h->{b}{c}{d}{x}
and exists $h->{b}{c}{d}{x}{y};
is_deeply $h, { b => {} }, "f3: no autovivification";
is $f3, undef, "f3: no value assigned";
$f4 = $h->{b}{c}{d}{x}{y}{z};
delete $h->{b}{c};
is_deeply $h, { b => {} }, "f4: autovivification removed";
is $f4, undef, "f4: no value assigned";
'
Update:
The code above is a modification of the original.
Something was niggling me about what I first wrote, but I couldn't see the problem.
++hv's response to your post alerted me to the issue.
My first, less-than-good effort is in the spoiler below.
Note that the output is unchanged.
Output:
1..8
ok 1 - no autovivification
ok 2 - no value assigned
ok 3 - f2: no autovivification
ok 4 - f2: no value assigned
ok 5 - f3: no autovivification
ok 6 - f3: no value assigned
ok 7 - f4: autovivification removed
ok 8 - f4: no value assigned
-
Short-circuiting is not actually needed in your example (see f2)
-
Short-circuiting works but can be unwieldy with more complex data structures (see f3)
-
Allowing autovivification then removing it results in cleaner, and easier to maintain, code (see f4)
-
This option has potential drawbacks.
Consider the case where $h->{b}{c}{q} existed and had a meaningful and required value
but is removed by 'delete $h->{b}{c}'.
-
On second thoughts: this is probably a very bad idea
(even though it does work in this specific test script).
I think each has its merits
and probably comes down to best choice on a case by case basis.
|