Re: how to pass array and varaible

From: Ajey Kulkarni (ajey_at_pdx.edu)
Date: 11/08/04


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



Relevant Pages

  • Re: array within array
    ... playing around with the script below to get a feel for Perl. ... script below is incomplete and I'm doing an array within an array ... You are assigning the list from the file to a hash. ...
    (perl.beginners)
  • Re: Problem with anonymous array in hash
    ... Thanks John, sometimes I am too concise with my code and omit things ... The first number is, as expected, the number of elements in the array ... If you had enabled warnings perl would have given you a hint: ... The problem is that you are dereferencing $hash when you should be ...
    (comp.lang.perl.misc)
  • Re: reading a list of files
    ... If all the files are in a single directory throw all the file ... Then loop through the array and perform whatever functions you want ... This is all plain ol' simple Perl... ...
    (comp.lang.perl.misc)
  • Re: White space split
    ... yitzle wrote: ... print for split @array; ... John ... Perl isn't a toolbox, but a small machine shop where you ...
    (perl.beginners)
  • Re: my doesnt totally isolate
    ... John> Hmmm.... ... So how am I supposed to distinguish between an array ... You want to slow down *all* of Perl just ... Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. ...
    (comp.lang.perl.misc)