On 12/10/05, $Bill Luebkert <[EMAIL PROTECTED]> wrote:
> 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.

> 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.

No. You  are mistaken. That is exactly what passing by reference means.

Example:
    sub Increment($){ $_[0]++ };

If you want to protect your passed arguments from possible modification,
make a copy of them before submitting them:

my @args=($SomeVariable);
&Increment(@args);  # $SomeVariable will not be touched


You could even write a byvalue wrapper if you want, something like

sub ByValue(\&;){
    my $coderef = shift;
    my @argvals = @_;  # copying arg refs to new array, by value, or
at least COW
    &$coderef(@argvals);
};

then you could call untrusted subroutines like so

     @results = ByValue(\&Untrusted::module::baz, $one, $two);

without worring that $one  and $two might get touched.


although this might (or might not) be clearer:
     @results = &Untrusted::module::baz(my($notone,$nottwo)=( $one, $two));

--
David L Nicol
I'll see your time division multiplexing and
raise you an asynchronous trap

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to