Home: Fun With Perl: Perl Quizzes - Learn Perl the Fun Way:
Scope



Cure
User

Aug 13, 2000, 10:25 PM


Views: 26754
Scope

package Sort;
sub cure
{ $b <=> $a }

package Other;
@new = sort Sort::cure 5, 2, 4, 8;

Whats the VALUE of @new and explain how @new obtained those VALUES


Cure


Cameron
Deleted

Aug 14, 2000, 1:40 PM


Views: 26754
Re: Scope

The @new array is left unchanged at
5, 2, 4, 8;
How? I don't have my books handy, but i think it goes something like this.

First the call is made to Sort::cure, where
$a and $b are package global variables. These should be qualified with the package name of the caller, since the sort routine is not in the same package as the caller
Since $a and $b are out of scope, the subroutine returns '0' for each call.

Then sort compares the values returned from
Sort::cure, and since they are all '0', the order remains unchanged.

This populates the @new array with the unchanged list of values.


Cameron



Cure
User

Sep 13, 2000, 8:30 AM


Views: 26754
Re: Scope

Cameron your right, good job..


Cure