in reply to just how special are $a and $b these days?

how do I load data into $a and $b so that a sort comparator defined in a distant module can see them?

I'm not sure exactly what you are trying to do. Can you explain it in pseudocode? There is no problem with having a comparator function defined in a "distant module". The $a and $b variables are package variables. If you absolutely had to set them from some other module (which seems unlikely or at least unlikely to be a good practice) you could specifically reference the package that the sort comparator function is defined in.

-sauoq
"My two cents aren't worth a dime.";
  • Comment on Re: just how special are $a and $b these days?

Replies are listed 'Best First'.
Re^2: just how special are $a and $b these days?
by davidnicol (Acolyte) on Mar 04, 2010 at 19:37 UTC

    What I'm trying to do is write a binary search module that takes the same comparator function that was used to sort the array being search.

    I'm getting the idea here that since $a and $b are package variables, sort comparators need to be declared in the same package as their use. I had thought that, for instance

    # the following is dumb and is written strictly as an example package backwards; sub pmc { $b cmp $a }; package main; print sort backwards::pmc ( split /\s+/, `cat $0` )

    would work as intended.

    it doesn't

    so it looks like ${caller().'::a'} is going to be where the search key goes, and ${caller().'::b'} is going to be where the guesses go for evaluation. Done with glob aliasing, of course.

    Thank you everyone

      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.
        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.";