Is the behavior defined when modifying $_ when doing a foreach directly on a %hash variable, i.e., without using keys or values, to iterate through the hash keys *and* values? If it's specified somewhere in the man pages it's not clear to me so pointing me to where would be fine.
In other words, is it defined that the example code below:
#!/usr/bin/env perl
use warnings;
use strict;
my @array;
my %hash;
%hash = (1, 2, 3, 4);
@array = %hash;
print("<<<< @array\n");
foreach (%hash) {
print("<< $_\n");
$_ *= 11;
print(">> $_\n");
}
@array = %hash;
print(">>>> @array\n");
outputs the following (last line is key)?
<<<< 1 2 3 4 << 1 >> 11 << 2 >> 22 << 3 >> 33 << 4 >> 44 >>>> 1 22 3 44
(This is Perl v5.26.1.)
My project is to implement a version of chomp that tries to modify its arguments as close to what chomp is capable of though I realize you cannot specify a prototype on a subroutine that handles it exactly the same.
Basically I'm trying to determine if it is defined what happens when a %hash argument is passed to a sub (@) prototyped subroutine.
In reply to Modifying $_ in foreach (%hash) by dse
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |