in reply to •Re: Problem with Schwartzian Transform nested in another sort from a different package
in thread Problem with Schwartzian Transform nested in another sort from a different package

So my results are the expected behavior?

I had (perhaps naively) believed the inner sort in Diffpackage::mysort would set up its own localized $a and $b in its package, and the outer sort would have its own $a and $b in main.

I guess I don't understand the magic behind $a and $b very well. I am attempting to implement your suggestion about correlating package names, though I'm not quite sure I understand what you mean.

Many thanks for your reply.
  • Comment on Re: •Re: Problem with Schwartzian Transform nested in another sort from a different package
  • Download Code

Replies are listed 'Best First'.
Re: Re: •Re: Problem with Schwartzian Transform nested in another sort from a different package
by davido (Cardinal) on Apr 15, 2004 at 21:20 UTC
    See the output of the following two one-liners:

    perl -MO=Xref -e "my @array = sort { $a <=> $b } ( 2, 8, 3 );"

    perl -MO=Xref = "local ( $a, $b ); my @array = sort { $a <=> $b } ( 2, + 8, 3 );"

    The first example results in crossreference output that essentially makes no mention of $a and $b.

    The second example results in crossreference output that includes $a and $b under the package main namespace.


    Dave

Re: Re: •Re: Problem with Schwartzian Transform nested in another sort from a different package
by tilly (Archbishop) on Apr 15, 2004 at 21:42 UTC
    If you're confused about variables and packages, it sounds like you need to read Coping with Scoping.

    Since sort was there in Perl 4, the mechanism that it uses by default ($a, $b) accesses the package variable of that name in the current package.