f_sort.t

Code test snippets here are adapted from `perldoc -f map`

Due to a bleadperl optimization (Dave Mitchell, circa apr 04), the (map|grep)(start|while) opcodes have different flags in 5.9, their private flags /1, /2 are gone in blead (for the cases covered)

When the optree stuff was integrated into 5.8.6, these tests failed, and were todo'd. They're now done, by version-specific tweaking in mkCheckRex(), therefore the skip is removed too.

Test Notes

# chunk: #!perl #examples poached from perldoc -f sort

NOTE: name is no longer a required arg for checkOptree, as label is synthesized out of others. HOWEVER, if the test-code has newlines in it, the label must be overridden by an explicit name.

This is because t/TEST is quite particular about the test output it processes, and multi-line labels violate its 1-line-per-test expectations.

# chunk: # sort lexically @articles = sort @files;

# chunk: # same thing, but with explicit sort routine @articles = sort {$a cmp $b} @files;

# chunk: # now case-insensitively @articles = sort {uc($a) cmp uc($b)} @files;

# chunk: # same thing in reversed order @articles = sort {$b cmp $a} @files;

# chunk: # sort numerically ascending @articles = sort {$a <=> $b} @files;

# chunk: # sort numerically descending @articles = sort {$b <=> $a} @files;

# chunk: # this sorts the %age hash by value instead of key # using an in-line function @eldest = sort { $age{$b} <=> $age{$a} } keys %age;

# chunk: # sort using explicit subroutine name sub byage { $age{$a} <=> $age{$b}; # presuming numeric } @sortedclass = sort byage @class;

# chunk: sub backwards { $b cmp $a } @harry = qw(dog cat x Cain Abel); @george = qw(gone chased yz Punished Axed); print sort @harry; # prints AbelCaincatdogx print sort backwards @harry; # prints xdogcatCainAbel print sort @george, 'to', @harry; # prints AbelAxedCainPunishedcatchaseddoggonetoxyz

# chunk: # inefficiently sort by descending numeric compare using # the first integer after the first = sign, or the # whole record case-insensitively otherwise @new = @old[ sort { $nums[$b] <=> $nums[$a] || $caps[$a] cmp $caps[$b] } 0..$#old ];

# chunk: # same thing, but without any temps @new = map { $_->[0] } sort { $b->[1] <=> $a->[1] || $a->[2] cmp $b->[2] } map { [$_, /=(\d+)/, uc($_)] } @old;

# chunk: # using a prototype allows you to use any comparison subroutine # as a sort subroutine (including other package's subroutines) package other; sub backwards ($$) { $_[1] cmp $_[0]; } # $a and $b are not set here package main; @new = sort other::backwards @old;

# chunk: # repeat, condensed. $main::a and $b are unaffected sub other::backwards ($$) { $_[1] cmp $_[0]; } @new = sort other::backwards @old;

# chunk: # guarantee stability, regardless of algorithm use sort 'stable'; @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;

# chunk: # you should have a good reason to do this! @articles = sort {$FooPack::b <=> $FooPack::a} @files;

# chunk: # fancy @result = sort { $a <=> $b } grep { $_ == $_ } @input;

# chunk: # void return context sort sort { $a <=> $b } @input;

# chunk: # more void context, propagating ? sort { $a <=> $b } grep { $_ == $_ } @input;

# chunk: # scalar return context sort $s = sort { $a <=> $b } @input;

# chunk: $s = sort { $a <=> $b } grep { $_ == $_ } @input;