On Monday 21 Dec 2009 18:09:32 Bryan R Harris wrote:
> > Wagner, David --- Senior Programmer Analyst --- CFS wrote:
> >> You pass as a refernce as ni
> >> called_sub(\...@d);
> >> Now when you update, you are updating @d and not a copy.
> >
> > No need to use a reference for that:
> >
> > perl -wle '
> >
> >      sub inc{ ++$_ for @_ }
> >
> >      my @x = 1 .. 5;
> >
> >      inc @x;
> >
> >      print "@x";
> > '
> > 2 3 4 5 6
> 
> FYI, the reason we wanted a reference was because the data set might end up
> being huge.
> 
> Uh, come to think of it, I'm surprised your script does what it does.  I'd
> have thought that the changes made internal to inc would've stayed there
> since they're not being "returned".  This bothers me a little...
> 

@_ contains aliases to the variables. So you can do (untested):

<<<<<<<<<
sub modify_var
{
        $_[0] += 100;

        return;
}

my $x = 5;
modify_var($x);

# $x is now 105.
>>>>>>>>>

However, note that I consider tricks like that as a bad idea and prefer to use 
references if I wish to do it. using << my $x = shift; >> or << my ($x) = @_; 
>> will not keep the aliasing. That's how the Perl 5 implementation works.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Freecell Solver - http://fc-solve.berlios.de/

Bzr is slower than Subversion in combination with Sourceforge. 
( By: http://dazjorz.com/ )

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to