in reply to Re^2: just how special are $a and $b these days?
in thread just how special are $a and $b these days?

This does:

package backwards; sub pmc { $::b cmp $::a }; package main; print sort backwards::pmc ( split /\s+/, `cat $0` )
c:\test>junk66 };{subsplitsortprintpmcpackagepackagemain;cmpbackwards;backwards::pmc` +cat/\s+/,)($::b$::a$0`

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^4: just how special are $a and $b these days?
by sauoq (Abbot) on Mar 04, 2010 at 22:31 UTC
    This does:

    Not really. That only works for the special case where you are performing your sort in main::. It doesn't work if you are in yet another package.

    In other words, this fails:

    package backwards; sub pmc { $::b cmp $::a }; package blah; sub foo { print for sort backwards::pmc ( split /\s+/, `cat $0` ) } package main; blah::foo();

    The right way to do it is to prototype your comparator function with ($$) and to use @_ thusly:

    package backwards; sub pmc($$) { $_[1] cmp $_[0] }; package blah; sub foo { print for sort backwards::pmc ( split /\s+/, `cat $0` ) } package main; blah::foo();
    -sauoq
    "My two cents aren't worth a dime.";

      True. Good call.