http://www.perlmonks.org?node_id=379042


in reply to Re: Why do we need $a and $b to be special?
in thread Why do we need $a and $b to be special?

... however, since 5.6.0, perl has supported having a named comparison routine with a $$ prototype to indicate that you want the elements to sort passed as parameters instead of as $a and $b
I didn't know that. How would the $$ prototype be seen in a CODE ref? Would it still work?

I tend to avoid prototypes normally, as they don't do what you expect, and seem to be more trouble than they are worth.

--
I'm Not Just Another Perl Hacker

  • Comment on Re^2: Why do we need $a and $b to be special?

Replies are listed 'Best First'.
Re^3: Why do we need $a and $b to be special?
by adrianh (Chancellor) on Aug 01, 2004 at 10:38 UTC
    I didn't know that. How would the $$ prototype be seen in a CODE ref? Would it still work?

    Just apply the prototype to the coderef, for example:

    my $sort = sub ($$) { $_[0] cmp $_[1] }; my @sorted = sort $sort @unsorted;
Re^3: Why do we need $a and $b to be special?
by ysth (Canon) on Aug 01, 2004 at 09:34 UTC
    Agree with you re: prototypes. I don't think you can do it with a code block specified after "sort", you actually have to declare a sub. (However, in general you can attach prototypes to coderefs by putting the prototype between "sub" and the code block.)

    I think this was implemented because without it you can't very well use a sort subroutine from another package, since it will be looking at that package's $a and $b, while sort will be setting the current package's $a and $b.