changing local variable affects array values?

F

fatted

Can someone explain this to me please?

use strict;
use warnings;

my @array = ( "one", "two", "three", "four", "fi_ve");

foreach my $value (@array)
{
$value =~ s/_/ /g;
print @array;
print "\n";
}
------
Results in:

onetwothreefourfi_ve
onetwothreefourfi_ve
onetwothreefourfi_ve
onetwothreefourfi_ve
onetwothreefourfi ve

But:
------
use strict;
use warnings;

my @array = ( "one", "two", "three", "four", "fi_ve");

foreach (@array)
{
my $value = $_;
$value =~ s/_/ /g;
print @array;
print "\n";
}
-------
Gives:

onetwothreefourfi_ve
onetwothreefourfi_ve
onetwothreefourfi_ve
onetwothreefourfi_ve
onetwothreefourfi_ve

(Note _ on last line of this result set compared to previous).
 
J

John W. Krahn

fatted said:
Can someone explain this to me please?

[ snipped foreach examples ]

Yes, the documentation supplied with perl can.

perldoc perlsyn
[snip]
Compound Statements

In Perl, a sequence of statements that defines a scope is called a
block. Sometimes a block is delimited by the file containing it (in
the case of a required file, or the program as a whole), and sometimes
a block is delimited by the extent of a string (in the case of an
eval).

But generally, a block is delimited by curly brackets, also known as
braces. We will call this syntactic construct a BLOCK.

The following compound statements may be used to control flow:

if (EXPR) BLOCK
if (EXPR) BLOCK else BLOCK
if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
LABEL while (EXPR) BLOCK
LABEL while (EXPR) BLOCK continue BLOCK
LABEL for (EXPR; EXPR; EXPR) BLOCK
LABEL foreach VAR (LIST) BLOCK
LABEL foreach VAR (LIST) BLOCK continue BLOCK
LABEL BLOCK continue BLOCK

[snip]

Foreach Loops

The "foreach" loop iterates over a normal list value and sets the
variable VAR to be each element of the list in turn. If the variable
is preceded with the keyword "my", then it is lexically scoped, and is
therefore visible only within the loop. Otherwise, the variable is
implicitly local to the loop and regains its former value upon exiting
the loop. If the variable was previously declared with "my", it uses
that variable instead of the global one, but it's still localized to
the loop. This implicit localisation occurs only in a "foreach" loop.

The "foreach" keyword is actually a synonym for the "for" keyword, so
you can use "foreach" for readability or "for" for brevity. (Or
because the Bourne shell is more familiar to you than csh, so writing
"for" comes more naturally.) If VAR is omitted, $_ is set to each
value.

If any element of LIST is an lvalue, you can modify it by modifying VAR
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
inside the loop. Conversely, if any element of LIST is NOT an lvalue,
^^^^^^^^^^^^^^^
any attempt to modify that element will fail. In other words, the
^^^^^^^^^^^^^^^^^^^
"foreach" loop index variable is an implicit alias for each item in the
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
list that you're looping over.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top