Perl: How to push key/value pair onto hashref and still retain reference

Viewed 6233
$a = {b=>{c=>1}};   # set up ref
$b = $a->{b};       # ref the ref
$b .= (d=>1,e=>1);  # where we want to assign multiple key/val at once

At the end of it $a should look like:

  • {
      'b' => {
               'c' => 1,
               'd' => 1,
               'e' => 1
             }
    };
    

At the end of it $b should look like:

  • {
      'c' => 1,
      'd' => 1,
      'e' => 1
    }
    

Note: it would be the same as doing:

$b->{d} = 1;
$b->{e} = 1;

$b = { %$b, d=>1, e=>1 }; Is not desired because it creates a copy of $a and loses the reference.

2 Answers
Related