I have been trying to add an OO interface to Sort::ArbBiLex, a handy module for doing lexicographic sorts for languages that don't have their own locales (e.g. endangered minority languages). In the process I ran into a problem with nesting the Schwartzian transform inside a sort, but only when the ST is in a different package than the outer sort. The following minimal code illustrates the problem:
#!/usr/bin/perl use strict; use warnings; my @pair = ( [ qw(a b) ], [ qw(b a) ], [ qw(c b) ], [ qw(b c) ], ); my @triplet = ( [ qw(a b c) ], [ qw(b a c) ], [ qw(c b a) ], [ qw(b c a) ], ); print "Individual calls to mycmp()...\n"; foreach (@pair) { print "Same package: "; print $_->[0], ' ', $_->[1], ' => '; print $_->[0], ' ', $_->[1], "\n" if mycmp(@$_) == -1; print $_->[1], ' ', $_->[0], "\n" if mycmp(@$_) == 1; print $_->[1], ' ', $_->[0], "\n" if mycmp(@$_) == 0; print "Different package: "; print $_->[0], ' ', $_->[1], ' => '; print $_->[0], ' ', $_->[1], "\n" if Diffpackage::mycmp(@$_) == -1; print $_->[1], ' ', $_->[0], "\n" if Diffpackage::mycmp(@$_) == 1; print $_->[1], ' ', $_->[0], "\n" if Diffpackage::mycmp(@$_) == 0; } print "\nMultiple calls to mycmp() in sort...\n"; foreach (@triplet) { print "Same package: "; print $_->[0], ' ', $_->[1], ' ', $_->[2], ' => '; print (join ' ', sort { mycmp($a, $b) } @$_ ); print "\n"; print "Different package: "; print $_->[0], ' ', $_->[1], ' ', $_->[2], ' => '; print (join ' ', sort { Diffpackage::mycmp($a, $b) } @$_ ); print "\n\n"; } exit; sub mycmp { my ($first, $second) = @_; return 1 if $first ne ( mysort($first, $second) ); return -1 if $first eq ( mysort($second, $first) ); return 0; } sub mysort { my @ans = map { $_->[0] } sort { $a->[0] cmp $b->[0] } # $a and $b are always defined map { [ $_ ] } @_; return $ans[0]; } package Diffpackage; # this is the same as previous mycmp sub mycmp { my ($first, $second) = @_; return 1 if $first ne ( mysort($first, $second) ); return -1 if $first eq ( mysort($second, $first) ); return 0; } # this is the same as previous mysort sub mysort { my @ans = map { $_->[0] } sort { $a->[0] cmp $b->[0] } # this is where $a and $b can be un +defined map { [ $_ ] } @_; return $ans[0]; }
When I run this code with perl v5.8.2 I get the following results. The mycmp function works with pairs of inputs, regardless of the package mycmp lives in.

When multiple calls to mycmp are required to sort three inputs, the nested ST appears to work fine if mysort is in the same package as the outer sort. Diffpackage::mysort generates lots of errors, though the first two characters sort properly:
Individual calls to mycmp()... Same package: a b => a b Different package: a b => a b Same package: b a => a b Different package: b a => a b Same package: c b => b c Different package: c b => b c Same package: b c => b c Different package: b c => b c Multiple calls to mycmp() in sort... Same package: a b c => a b c Use of uninitialized value in string comparison (cmp) at ./testsort3.p +l line 77. (Snip repeated error) Different package: a b c => a b c Same package: b a c => a b c Use of uninitialized value in string comparison (cmp) at ./testsort3.p +l line 77. (Snip repeated error) Different package: b a c => a b c <== first two inputs sort correctly Same package: c b a => a b c Use of uninitialized value in string comparison (cmp) at ./testsort3.p +l line 77. (Snip repeated error) Different package: c b a => b c a <== first two inputs sort correctly Same package: b c a => a b c Use of uninitialized value in string comparison (cmp) at ./testsort3.p +l line 77. (Snip repeated error) Different package: b c a => b c a
Looking at this in the debugger, I notice that when Diffpackage::mysort is nested in the outer sort, $a and $b are defined in the sort line of Diffpackage::mysort the first time that line is evaluated but not on subsequent evaluations of that line. This problem doesn't happen when mysort is in the same package as the outer sort.

Is this a known problem? Can anyone shed some light?

ronald

In reply to Problem with Schwartzian Transform nested in another sort from a different package by ronald

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.