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

Re: sorting function arcument to 'sort': Can I pass additional parameters?

by blazar (Canon)
on Nov 10, 2008 at 10:22 UTC ( #722605=note: print w/replies, xml ) Need Help??


in reply to sorting function arcument to 'sort': Can I pass additional parameters?

I personally believe that you don't want to "pass in other arguments to the sorting sub" because the sub is meant to be used sort and sort() itself won't pass anything in. Chances are you want to use closures instead:

#!/usr/bin/perl use strict; use warnings; use 5.010; my @trans = ( sub { shift }, sub { ~ shift } ); my @unsorted = qw/foo bar baz/; for my $tr (@trans) { my @sorted = sort { $tr->($a) cmp $tr->($b) } @unsorted; say "@sorted"; } __END__
--
If you can't understand the incipit, then please check the IPB Campaign.