1package sort; 2 3our $VERSION = '2.02'; 4 5# The hints for pp_sort are now stored in $^H{sort}; older versions 6# of perl used the global variable $sort::hints. -- rjh 2005-12-19 7 8$sort::quicksort_bit = 0x00000001; 9$sort::mergesort_bit = 0x00000002; 10$sort::sort_bits = 0x000000FF; # allow 256 different ones 11$sort::stable_bit = 0x00000100; 12 13use strict; 14 15sub import { 16 shift; 17 if (@_ == 0) { 18 require Carp; 19 Carp::croak("sort pragma requires arguments"); 20 } 21 local $_; 22 $^H{sort} //= 0; 23 while ($_ = shift(@_)) { 24 if (/^_q(?:uick)?sort$/) { 25 $^H{sort} &= ~$sort::sort_bits; 26 $^H{sort} |= $sort::quicksort_bit; 27 } elsif ($_ eq '_mergesort') { 28 $^H{sort} &= ~$sort::sort_bits; 29 $^H{sort} |= $sort::mergesort_bit; 30 } elsif ($_ eq 'stable') { 31 $^H{sort} |= $sort::stable_bit; 32 } elsif ($_ eq 'defaults') { 33 $^H{sort} = 0; 34 } else { 35 require Carp; 36 Carp::croak("sort: unknown subpragma '$_'"); 37 } 38 } 39} 40 41sub unimport { 42 shift; 43 if (@_ == 0) { 44 require Carp; 45 Carp::croak("sort pragma requires arguments"); 46 } 47 local $_; 48 no warnings 'uninitialized'; # bitops would warn 49 while ($_ = shift(@_)) { 50 if (/^_q(?:uick)?sort$/) { 51 $^H{sort} &= ~$sort::sort_bits; 52 } elsif ($_ eq '_mergesort') { 53 $^H{sort} &= ~$sort::sort_bits; 54 } elsif ($_ eq 'stable') { 55 $^H{sort} &= ~$sort::stable_bit; 56 } else { 57 require Carp; 58 Carp::croak("sort: unknown subpragma '$_'"); 59 } 60 } 61} 62 63sub current { 64 my @sort; 65 if ($^H{sort}) { 66 push @sort, 'quicksort' if $^H{sort} & $sort::quicksort_bit; 67 push @sort, 'mergesort' if $^H{sort} & $sort::mergesort_bit; 68 push @sort, 'stable' if $^H{sort} & $sort::stable_bit; 69 } 70 push @sort, 'mergesort' unless @sort; 71 join(' ', @sort); 72} 73 741; 75__END__ 76 77=head1 NAME 78 79sort - perl pragma to control sort() behaviour 80 81=head1 SYNOPSIS 82 83 use sort 'stable'; # guarantee stability 84 use sort '_quicksort'; # use a quicksort algorithm 85 use sort '_mergesort'; # use a mergesort algorithm 86 use sort 'defaults'; # revert to default behavior 87 no sort 'stable'; # stability not important 88 89 use sort '_qsort'; # alias for quicksort 90 91 my $current; 92 BEGIN { 93 $current = sort::current(); # identify prevailing algorithm 94 } 95 96=head1 DESCRIPTION 97 98With the C<sort> pragma you can control the behaviour of the builtin 99C<sort()> function. 100 101In Perl versions 5.6 and earlier the quicksort algorithm was used to 102implement C<sort()>, but in Perl 5.8 a mergesort algorithm was also made 103available, mainly to guarantee worst case O(N log N) behaviour: 104the worst case of quicksort is O(N**2). In Perl 5.8 and later, 105quicksort defends against quadratic behaviour by shuffling large 106arrays before sorting. 107 108A stable sort means that for records that compare equal, the original 109input ordering is preserved. Mergesort is stable, quicksort is not. 110Stability will matter only if elements that compare equal can be 111distinguished in some other way. That means that simple numerical 112and lexical sorts do not profit from stability, since equal elements 113are indistinguishable. However, with a comparison such as 114 115 { substr($a, 0, 3) cmp substr($b, 0, 3) } 116 117stability might matter because elements that compare equal on the 118first 3 characters may be distinguished based on subsequent characters. 119In Perl 5.8 and later, quicksort can be stabilized, but doing so will 120add overhead, so it should only be done if it matters. 121 122The best algorithm depends on many things. On average, mergesort 123does fewer comparisons than quicksort, so it may be better when 124complicated comparison routines are used. Mergesort also takes 125advantage of pre-existing order, so it would be favored for using 126C<sort()> to merge several sorted arrays. On the other hand, quicksort 127is often faster for small arrays, and on arrays of a few distinct 128values, repeated many times. You can force the 129choice of algorithm with this pragma, but this feels heavy-handed, 130so the subpragmas beginning with a C<_> may not persist beyond Perl 5.8. 131The default algorithm is mergesort, which will be stable even if 132you do not explicitly demand it. 133But the stability of the default sort is a side-effect that could 134change in later versions. If stability is important, be sure to 135say so with a 136 137 use sort 'stable'; 138 139The C<no sort> pragma doesn't 140I<forbid> what follows, it just leaves the choice open. Thus, after 141 142 no sort qw(_mergesort stable); 143 144a mergesort, which happens to be stable, will be employed anyway. 145Note that 146 147 no sort "_quicksort"; 148 no sort "_mergesort"; 149 150have exactly the same effect, leaving the choice of sort algorithm open. 151 152=head1 CAVEATS 153 154As of Perl 5.10, this pragma is lexically scoped and takes effect 155at compile time. In earlier versions its effect was global and took 156effect at run-time; the documentation suggested using C<eval()> to 157change the behaviour: 158 159 { eval 'use sort qw(defaults _quicksort)'; # force quicksort 160 eval 'no sort "stable"'; # stability not wanted 161 print sort::current . "\n"; 162 @a = sort @b; 163 eval 'use sort "defaults"'; # clean up, for others 164 } 165 { eval 'use sort qw(defaults stable)'; # force stability 166 print sort::current . "\n"; 167 @c = sort @d; 168 eval 'use sort "defaults"'; # clean up, for others 169 } 170 171Such code no longer has the desired effect, for two reasons. 172Firstly, the use of C<eval()> means that the sorting algorithm 173is not changed until runtime, by which time it's too late to 174have any effect. Secondly, C<sort::current> is also called at 175run-time, when in fact the compile-time value of C<sort::current> 176is the one that matters. 177 178So now this code would be written: 179 180 { use sort qw(defaults _quicksort); # force quicksort 181 no sort "stable"; # stability not wanted 182 my $current; 183 BEGIN { $current = sort::current; } 184 print "$current\n"; 185 @a = sort @b; 186 # Pragmas go out of scope at the end of the block 187 } 188 { use sort qw(defaults stable); # force stability 189 my $current; 190 BEGIN { $current = sort::current; } 191 print "$current\n"; 192 @c = sort @d; 193 } 194 195=cut 196 197