wvbrandt has asked for the wisdom of the Perl Monks concerning the following question:

Perl Monks--I come seeking enlightenment. I've run into this problem before and have eventually given up but this time I'm going to the mountain.

I'm using a straightforward sort sub to sort various package versions out of a Maven repository like this:

sub compare_dotted_decimal { #---------------------------------------- my @a = $a =~ /(\d+)\.(\d+)\.*(\d*)\.*(\d*)\.*(\d*)/; my @b = $b =~ /(\d+)\.(\d+)\.*(\d*)\.*(\d*)\.*(\d*)/; $b[0] <=> $a[0] || $b[1] <=> $a[1] || $b[2] <=> $a[2] || $b[3] <=> + $a[3] || $b[4] <=> $a[4]; } #========================================= compare_dotted_decimal ==

This works fine. However I'd like to reuse it to compare only two values to see if the one I have is greater. It seems to me this could be called to return a -1,0,1 just like a comparison operator but everything I've tried fails to return anything. Any ideas on how to call a sort sub like a regular one?

Replies are listed 'Best First'.
Re: Using sort sub as regular sub
by NetWallah (Canon) on May 25, 2012 at 16:27 UTC
    fails to return anything.

    Not even "undef" ?

    Are you setting $a and $b before calling the sub ?

    Please show your calling sequence.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Yes, I was setting $a and $b. I dumped the result into a string, which was blank, so I assumed the sub was sending back undefined. However, I went back to check for sure, ended up qualifying the call with its package name, and viola it immediately started working. So thanks for the question. :-)