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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to