Chaddaï Fouché wrote:

> $Bill Luebkert wrote:
> 
> 
>>Perl normally passes by value .
>>
> 
> That is unaccurate : Perl always passes by reference (or rather by 
> alias), but we do the copy ourselves when we handle the arguments array :
> @_ always has aliases on the arguments.

The fact that Perl uses an aliasing method does not change the fact that
it passes by value rather than by reference.  You can modify the vrbl
that an arg is aliased to, but that doesn't make it a reference.  Many
of us prefer not to create/utilize that condition though.

Perlsub man page:

    The Perl model for function call and return values is simple: all functions
    are passed as parameters one single flat list of scalars, and all functions
    likewise return to their caller one single flat list of scalars. Any arrays
    or hashes in these call and return lists will collapse, losing their
    identities--but you may always use pass-by-reference instead to avoid this.
    Both call and return lists may contain as many or as few scalar elements as
    you'd like. (Often a function without an explicit return statement is called
    a subroutine, but there's really no difference from Perl's perspective.)

    Any arguments passed in show up in the array @_. Therefore, if you called a
    function with two arguments, those would be stored in $_[0] and $_[1]. The
    array @_ is a local array, but its elements are aliases for the actual
    scalar parameters. In particular, if an element $_[0] is updated, the
    corresponding argument is updated (or an error occurs if it is not
    updatable). If an argument is an array or hash element which did not exist
    when the function was called, that element is created only when (and if) it
    is modified or a reference to it is taken. (Some earlier versions of Perl
    created the element whether or not the element was assigned to.) Assigning
    to the whole array @_ removes that aliasing, and does not update any
    arguments.

In addition to that, you have the problem of passing a literal which would
not be modifiable and create much consternation. EG:
        print "ret : ", increment ('1'), "\n";

> eg :
> sub increment { $_[0]++ }
> sub increment2 {
>   my $arg = shift;
>   $arg++;
> }
> my $one = 1;
> print "ret : ", increment $one, "\n"; # "ret : 2"
> print "one : $one\n"; # "one : 2"
> print "ret : ", increment2 $one, "\n"; # "ret : 3"
> print "one : $one\n"; # "one : 2"

sub increment { $_[0]++; }
sub increment2 { my $arg = shift; $arg++; }

my $one = 1;
my $ret = increment ($one);     # will modify $one due to aliasing
print "ret: $ret\n";
print "one : $one\n";

$one = 1;
$ret = increment2 ($one);       # won't modify $one since $arg is localized
print "ret: $ret\n";
print "one : $one\n";

__END__

The fact that you can modify the vrbl passed to a sub from in the sub without
using a reference still doesn't mean that it's passed by reference.  This is
a special case that aliasing provides.  This could be partially an exercise
in semantics though.
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to