So I have a Perl class. It has a sort()
method, and I want it to be more or less identical to the built-in sort()
function:
$object->sort(sub ($$) { $_[0] <=> $_[1] });
But I can't do:
$object->sort(sub { $a <=> $b });
Because of scoping. But the List::Util module does this with reduce()
. I looked at the List::Util module, and they do some rather nasty things with no strict 'vars'
to make this happen. I tried that, but to no avail.
It is my understanding that reduce()
works the way it does because it is exported into the appropriate namespace, and thus my class can't do this since the function is quite firmly in another namespace. Is this correct, or is there some (undoubtedly more hideous and ill-advised) way to do this in my situation?