in reply to Re: Extending Array
in thread Extending Array

Why do you want to create a new hash? Just adding to the old hash ref won't work?

Can you give a scenario where do you use 1st case?

Replies are listed 'Best First'.
Re^3: Extending Array
by moritz (Cardinal) on Mar 21, 2012 at 19:07 UTC
    Can you give a scenario where do you use 1st case?

    If I write a subroutine that receives a hash reference as an argument, I wouldn't modify it in-place, because the caller might want to re-use it.

    Simplified example:

    # caller: my %h = (a => 1, b => 2); something(\%h); say $_ for keys %h; # callee sub something { my $arg = shift; $arg = { %arg, c => 3, d => 4, }; # do more stuff with $arg here }

    If you use $arg->{c} = 3 in sub something, %h changes in the caller's code, which is very likely to cause confusion.