Search code examples
perlsortingpackagesubroutine

Using a sorting subroutine from another package


I have a script and a package like so:

# file: sortscript.pl
use strict;
use warnings;
use SortPackage;

my @arrays = ([1,"array1"],[10,"array3"],[4,"array2"]);

print "Using sort outside package\n";
foreach (sort SortPackage::simplesort @arrays){
    print $_->[1],"\n";
}

print "\nUsing sort in same package\n";
SortPackage::sort_from_same_package(@arrays);

--

# file: SortPackage.pm
use strict;
use warnings;
package SortPackage;

sub simplesort{
    return ($a->[0] <=> $b->[0]);
}

sub sort_from_same_package{
    my @arrs = @_;
    foreach (sort simplesort @arrs){
        print $_->[1],"\n";
    } 
}
1;

Running the script produces the output:

$ perl sortscript.pl
Using sort outside package
Use of uninitialized value in numeric comparison (<=>) at SortPackage.pm line 15.
Use of uninitialized value in numeric comparison (<=>) at SortPackage.pm line 15.
Use of uninitialized value in numeric comparison (<=>) at SortPackage.pm line 15.
Use of uninitialized value in numeric comparison (<=>) at SortPackage.pm line 15.
Use of uninitialized value in numeric comparison (<=>) at SortPackage.pm line 15.
Use of uninitialized value in numeric comparison (<=>) at SortPackage.pm line 15.
array1
array3
array2

Using sort in same package
array1
array2
array3

Why am I not able to correctly use the subroutine to sort with when it is in another package?


Solution

  • As has been mentioned, $a and $b are package globals, so another solution is to temporarily alias the globals at the call site to the ones in package SortPackage:

    {
        local (*a, *b) = (*SortPackage::a, *SortPackage::b);
        foreach (sort SortPackage::simplesort @arrays){
            print $_->[1],"\n";
        }
    }
    

    But this is pretty ugly, of course. I would just have SortPackage export a complete sorting routine, not just a comparator:

    package SortPackage;
    use strict;
    
    sub _sort_by_first_element_comparator {
        return $a->[0] <=> $b->[0];
    }
    
    sub sort_by_first_element {
        return sort _sort_by_first_element_comparator @_;
    }