in reply to define a hash-value, using hash-ref and a list of keys

You could make a subroutine, but you can use //= assignment for the equivalent of the code example you gave:
$cf{$k1}{$k2}{$k3}{"hlst"} //= "";
But that assumes the problem you're trying to solve is duplicating that variable twice.

Replies are listed 'Best First'.
Re^2: define a hash-value, using hash-ref and a list of keys
by LanX (Saint) on Oct 29, 2014 at 17:11 UTC
    > $cf{$k1}{$k2}{$k3}{"hlst"} //= "";

    IMHO the best solution in this case where autovivification is wanted.

    For completeness if the perl version is to old for defined-or, one could use a ref to the hash value.

    $ perl use Data::Dumper; my $hash; my $rval = \ $hash{a}{b}{c}{"hlst"}; $$rval = "" unless defined $$rval; print Dumper \%hash; __END__ $VAR1 = { 'a' => { 'b' => { 'c' => { 'hlst' => '' } } } };

    or you can define an defined-or-assign function to do so

    use Data::Dumper; my $hash; defor( $hash{a}{b}{c}{"hlst"} , ""); print Dumper \%hash; sub defor { $_[0] = $_[1] unless defined $_[0]; } __END__ $VAR1 = { 'a' => { 'b' => { 'c' => { 'hlst' => '' } } } };

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)