Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by rinceWind (Monsignor)
on Aug 01, 2004 at 09:29 UTC ( [id://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?

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found