foreach $rock (qw/ bedrock slate lava /) {
print "One rock is $rock.\n"; # Prints names of three rocks
}
The control variable ($rock in
that example) takes on a new value from the list for each iteration.
The first time through the loop, it's "bedrock"; the third time, it's
"lava".
The control variable is not a copy of the list element -- it
actually is the list element. That is, if you
modify the control variable inside the loop, you'll be
modifying the element itself, as shown in the following code snippet.
This is useful, and supported, but it would surprise you if you
weren't expecting it.
@rocks = qw/ bedrock slate lava /;
foreach $rock (@rocks) {
$rock = "\t$rock"; # put a tab in front of each element of @rocks
$rock .= "\n"; # put a newline on the end of each
}
print "The rocks are:\n", @rocks; # Each one is indented, on its own line