Re: how to pass array and varaible
From: Ajey Kulkarni (ajey_at_pdx.edu)
Date: 11/08/04
- Next message: Arjun Mallik: "How to get text after comment"
- Previous message: John W. Krahn: "Re: Deleting File.........roy"
- In reply to: John W. Krahn: "Re: how to pass array and varaible"
- Next in thread: Gunnar Hjalmarsson: "Re: how to pass array and varaible"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 8 Nov 2004 00:04:34 -0800 (PST) To: "John W. Krahn" <krahnj@telus.net>
John and Gunnar,
Thanks a ton for clarification.
~A
On Sun, 7 Nov 2004, John W. Krahn wrote:
> Gunnar Hjalmarsson wrote:
> >
> > Ajey Kulkarni wrote:
> >>
> >> Alright, but what is the reason for less time when you pass the ref?
> >> I always thought the perl optimizes by sending the ref even if you
> >> use a direct array.
> >
> > When you pass an array, Perl *copies* the array elements to @_, so you
> > get two instances of the data in memory.
>
> perl stores an alias of each element passed to it into @_ just like foreach
> does so the *data* is only stored once.
>
> $ perl -le'
> sub my_sub { $_ += 5 foreach @_ }
> my @x = 10 .. 15;
> my $x = 55;
> print "@x $x";
> my_sub @x, $x;
> print "@x $x";
> '
> 10 11 12 13 14 15 55
> 15 16 17 18 19 20 60
>
>
> >> Where can i find more detailed info?
> >
> > perldoc perlsub
>
> perldoc perlsub
> [snip]
> 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.
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
- Next message: Arjun Mallik: "How to get text after comment"
- Previous message: John W. Krahn: "Re: Deleting File.........roy"
- In reply to: John W. Krahn: "Re: how to pass array and varaible"
- Next in thread: Gunnar Hjalmarsson: "Re: how to pass array and varaible"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|