From: Sudarsan Raghavan <[EMAIL PROTECTED]>
> Elias Assmann wrote:
> 
> > On Tue, 11 Jun 2002, Jeff 'japhy' Pinyan wrote:
> > So Perl passes subroutine arguments by reference? I thought they
> > would be copies in the first place...
> 
> No the args are only copies not references. The question was to return
> a modifed form of the argument without changing the argument. After
> the s/... statement the contents of the argument to rcsname i.e. $_[0]
> or some variable in rscname into which it has been shifted to should
> not change.

Then how come

        $x = 'Hello';
        sub foo {
                $_[0] = 'Hi';
        }
        foo($x);
        print $x,"\n";

prints 
        Hi
?

The parameters are passed by reference, though in little strange way.
If for example you include an array (and I mean array, not array 
reference!) then the function gets the elements of the array ... but 
still by reference:

        $x = 0;
        @a = (1,2);
        sub foo {
                for (@_) {
                        $_ += 10;
                }
        }
        foo( $x, @a);
        print "\$x = $x\n";
        print "\@a = @a\n";

It's when you do the usual
        my $var = shift;
or
        my ( $a, $b, $c) = @_;
that Perl makes copies of the parameters. If you do not shift() and 
access @_ directly you CAN modify the parameters.

Jenda
=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
                                        --- me


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to