$a and $b clearly are special - they are allowed to violate strict 'vars', due to their role as placeholders in the optional sort comparison function. This is the case even when no sort is in sight.

Turning our attention to sort, I have had need recently for externalising the compare function. Consider this:

my $obj = Foo->new( sort => 1); ... while (my $val = $obj->next) {
I have a new object of class Foo, which has an iterator. In my design of Foo.pm, I want the user to be able to choose to sort or not to sort via the 'sort' parameter. What I want next is to allow the user instead, to supply a CODEREF for a compare routine, to allow custom sort orders.

Conventially, the way a sort compare routine receives its operands is via $a and $b, and this is what I first envisaged. But, what do these actually mean? If called from package main, these will be $main::a and $main::b - fine, but my code is looking at package Foo by default.

I could look at caller in my new routine, and export $a and $b into the caller's package name space, or import the caller's $a and $b, or hold globrefs so that I know which $a and $b are being referred to. This is messy.

I have had a discussion with a respected module author about this subject, and he sympathised with me about my plight regarding $a and $b.

I have thought up an alternative, which is for the user to supply a routine which expects the operands in @_ instead. These are nicely localised by the parameter passing mechanism, so as not to interfere with other outer sorts that may be happening. In order to make my code work, I pass in $a and $b into the called routine using an anonymous wrapper.

if ($self->{sort}) { @out = (ref($self->{sort}) eq 'CODE') ? (sort {&{$self->{sort}}($a,$b)} @out) : (sort @out); }
I clearly state in the POD that the compare routine will be passed its operands in @_. Is there anything wrong with this approach from a user perspective? If not, why should we not look to dispense with $a and $b altogether in a future perl release? I know $a and $b will be gone in perl 6, but I think that this anachronism ought to be gone sooner than that.

--
I'm Not Just Another Perl Hacker


In reply to Why do we need $a and $b to be special? by rinceWind

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.