How about this?
#! /usr/local/bin/perl -w
use strict;
sub forward {$a cmp $b} # removed prototype
sub backward {$b cmp $a} # removed prototype
my @sorter = qw/forward backward/;
my @list = qw(kholsky stencil dnubietna barkhausen
manganese fairing fleische flake schlozhauer gascoigne);
my $offset = shift || 0;
my $subname= $sorter[$offset];
print "$_\n" for sort $subname @list;
Simply supply the sort routine's name.
Update: Thanks to
kwaping for noticing that this doesn't work. In fact I oversaw the word "forward" inside the output. To fix it I needed the 3 changes shown above. Prototype must be removed because
If the subroutine's prototype is "($$)", the elements to be compared are passed by reference in @_, as for a normal subroutine.. The aditional $subname is needed because:
SUBNAME may be a scalar variable name (unsubscripted), in which case the value provides the name of (or a reference to) the actual subroutine to use.
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.