in reply to Re: Re: Re: Re: package globals and sort()
in thread package globals and sort()
No there is no copy. If you know about C you will know about *pointers*. Anyway even if you don't all that is passed is a pointer to the original array. @$list_ref just uses the array pointed to by $list_ref. No copy is made. This is why it is efficient to pass array refs to and from subs as you never copy an array you don't have to.
The is a way to implement things the way you wanted originally by passing $a and $b to the other package but this is far less efficient so don't do it! Here it is just to show you it can be done. We can even localise $a and $b :-)
package MyOrder; use strict; my %order = (); sub SortMyWay { my ($a,$b) = @_; if ( exists $order{$a} and exists $order{$b} ) { return $order{$a} <=> $order{$b} } elsif ( exists $order{$a} ) { return 1 } elsif ( exists $order{$b} ) { return -1 } else { return $a cmp $b } } package main; use strict; my @foo = qw( j a p h , ); my @bar = sort { MyOrder::SortMyWay($a,$b) } @foo; print "@bar";
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|