Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Problems? Is your data what you think it is?
 
PerlMonks  

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

by rinceWind (Monsignor)
on Aug 01, 2004 at 09:29 UTC ( #379042=note: print w/ replies, xml ) Need Help??


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?
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.

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://379042]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-04-21 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls