On 2013-02-17 08:16, Toralf Frster <> wrote:
> On 02/15/2013 08:07 PM, George Mpouras wrote:
>> At your statement my @Values = @Zero;
>> you are "playing" with array references.
>
> OTOH this was so clear for me b/c if I pass parameters to a sub like foo
> (@Values) then this means a call-by-value
No.
Parameters are passed by reference in Perl.
For example, consider this code:
sub foo {
$_[1] = 5;
}
my @x = (1, 2, 3);
foo(@x);
print "@x\n";
my ($x, $y, $z) = qw(a b c);
foo($x, $y, $z);
print "$x $y $z\n";
__END__
It prints
1 5 3
a 5 c
which clearly shows that the array @x and the variably $y were passed by
reference (in Perl jargon, we call this "aliasing", but it's the same
concept).
What creates the *illusion* that Perl function calls are by value is the
convention to immediately assign parameters to local variables. So you
would normally write foo as
sub foo {
my @p = @_;
$p[1] = 5;
}
or
sub foo {
my ($p1, $p2, $p3) = @_;
$p2 = 5;
}
Here the assignments in the second line alter only the local variable
(@p or $p2, respectively), not the parameters. But it's the assignment
in the first line which causes the values to be copied, not the function
call.
hp
--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
| | |
| die Satzbestandteile des Satzes nicht mehr
__/ |
http://www.hjp.at/ | zusammenpat. -- Ralph Babel