Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Why are my functions second-class citizens?

by geary@acm.org (Initiate)
on Feb 15, 2012 at 16:30 UTC ( #953996=perlquestion: print w/replies, xml ) Need Help??
geary@acm.org has asked for the wisdom of the Perl Monks concerning the following question:

So, let's say, purely for the sake of argument, that I have a function like:

sub rv (@) { reverse @_; };
and I want to call it in this context:
sort rv @list;
Perl won't let me because it thinks I'm trying to use &rv as the comparison function for sort. I could say:
sort(rv(@list));
but that doesn't look as cool.

This works:

sort reverse @list;
How do get perl to treat &rv the same way?

Replies are listed 'Best First'.
Re: Why are my functions second-class citizens?
by Old_Gray_Bear (Bishop) on Feb 15, 2012 at 17:01 UTC
    Don't code for 'cool', code for clarity.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Why are my functions second-class citizens?
by choroba (Bishop) on Feb 15, 2012 at 16:43 UTC
Re: Why are my functions second-class citizens?
by LanX (Bishop) on Feb 15, 2012 at 16:52 UTC
    Some people want this DWIM functionality, which inhibits to Do What You Mean.

    Instead of fiddling with this magical syntax, I'd recommend writing a function my_sort with a clear interface to do what you want.

    Cheers Rolf

Re: Why are my functions second-class citizens?
by JavaFan (Canon) on Feb 15, 2012 at 17:55 UTC
    If you don't like the sort-cut sort is offering you, you can always be explicit:
    sort {$a cmp $b} rv @list;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://953996]
Approved by Old_Gray_Bear
help
Chatterbox?
[holli]: Oh, seems like LanX' team has won the fourth World Cup ... awesome!
LanX grins
[marto]: anyway, don't come to Glasgow for the football :)
[holli]: the hell? i have a scriptblocker on.
[holli]: how do you update a different window without javascript?
[holli]: ah, chatterbox right
[marto]: it's a normal HTML form, with some hidden input fields
[holli]: forgot that thing exists for a moment.
LanX welcome to the twilight zone ...
[Eily]: LanX well since the onion is your eye you should see it everywhere

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2017-12-14 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (398 votes). Check out past polls.

    Notices?