One approach is to say that any sort routine should never need an additional value to be passed in. Any case which looks like it needs that can have the assumption solved by prebinding an argument with a closure.
my $real_sort_routine = sub {$sort_routing->(\%href)}; foreach (sort $real_sort_routine keys %{ $somehashref }) { #do stff }
Now that you've moved the binding elsewhere, you can choose to put the binding wherever it makes sense - possibly during building up the definitions of sorts that you need.

You can find an example of setting up a complex sort using closures at Re: Fun with complex sorting on arbitrary criteria list..

A useful trick to know: if you'll be building a sort function in a different package than you're calling sort in, you'll want your functions to all have a prototype of ($$). With 5.6 or better, that will cause Perl to pass $a and $b in rather than setting appropriate globals in the package that sort was called from. (Which might not be the package that the sort function looks in - very confusing if it happens to you.)


In reply to Re: Sorting using coderef's, and passing parameters by tilly
in thread Sorting using coderef's, and passing parameters by the_slycer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.