use v5.14;
use warnings;
use Test::More;
sub dive(+;@) :lvalue {
my $h = \ shift;
$h = \($$h->{$_}) for @_;
$$h;
}
my %hsh;
my $h_ref = \%hsh;
my $val = $hsh{a}{b}{c} = 3;
my $val2 = 42;
my @path = qw/a b c/;
my $test;
;;;;;;;;;; $test = "Prototypes";
is( dive(%hsh => @path) , $val, "$test accepts \%list form");
is( dive($h_ref=> @path) , $val, "$test accepts \$ref form");
;;;;;;;;;; $test = "assign directly to lvalue";
dive(%hsh=> @path) = $val2;
is( $hsh{a}{b}{c}, $val2, $test);
;;;;;;;;;; $test = "increment in place";
dive( %hsh=> @path )++;
is( $hsh{a}{b}{c}, ++$val2, $test);
;;;;;;;;;; $test = "grab scalar reference";
my $s_ref = \ dive(%hsh, @path);
$$s_ref++;
is( $hsh{a}{b}{c}, ++$val2, $test);
;;;;;;;;;; $test = "aliasing";
for my $alias ( dive(%hsh, @path) ) {
$alias++;
}
is( $hsh{a}{b}{c}, ++$val2, $test);
done_testing;
OUTPUT: ok 1 - Prototypes accepts %list form
ok 2 - Prototypes accepts $ref form
ok 3 - assign directly to lvalue
ok 4 - increment in place
ok 5 - grab scalar reference
ok 6 - aliasing
1..6
UPDATE: cosmetic corrections
|