in reply to Re: Modifying hash keys via aliasing
in thread Modifying hash keys via aliasing

Your (and other's too) answer is correct and clear. But why no warnings are emitted in this case? like 'Redefine hash key not allowed at..' is similar to re-declare an already declared var, no?

For the OP question, the desired output can also be achievied reassigning the entire hash, like:
>perl -MData::Dumper -Mstrict -e 'sub t{ map {$_+=1 } @_}; my %h = (1 +=> 2, 3 => 4); %h = t(%h); print Dumper \%h' $VAR1 = { '4' => 5, '2' => 3 };


L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^3: Modifying hash keys via aliasing
by Laurent_R (Canon) on Aug 11, 2015 at 11:51 UTC
    Yes, I agree, I also would have expected a warning (which is why I added the -w flag in the first place).

    Just as I would also have expected a warning saying something like "attempt to modify read-only value" with the following pieces of code:

    $ perl -Mstrict -wE 'say ++$_ for 1..4;' 2 3 4 5 ~ $ perl -Mstrict -wE 'say $_ for map {++$_} 1..4;' 2 3 4 5
    But there is no warning. On the other hand, you get a warning with this:
    $ perl -Mstrict -wE 'say $_ for map {++$_} (1, 2, 3, 4);' Modification of a read-only value attempted at -e line 1.
    There must be some reason, but it does not look very consistent to me.