xref: /openbsd-src/gnu/usr.bin/perl/lib/sort.pm (revision 256a93a44f36679bee503f12e49566c2183f6181)
1package sort;
2
3use strict;
4use warnings;
5
6our $VERSION = '2.05';
7
8sub import {
9    shift;
10    if (@_ == 0) {
11	require Carp;
12	Carp::croak("sort pragma requires arguments");
13    }
14    $^H{sort} //= 0;
15    for my $subpragma (@_) {
16        next
17            if $subpragma eq 'stable' || $subpragma eq 'defaults';
18        require Carp;
19        Carp::croak("sort: unknown subpragma '$_'");
20    }
21}
22
23sub unimport {
24    shift;
25    if (@_ == 0) {
26	require Carp;
27	Carp::croak("sort pragma requires arguments");
28    }
29    for my $subpragma (@_) {
30        next
31            if $subpragma eq 'stable';
32        require Carp;
33        Carp::croak("sort: unknown subpragma '$_'");
34    }
35}
36
37sub current {
38    warnings::warnif("deprecated", "sort::current is deprecated, and will always return 'stable'");
39    return 'stable';
40}
41
421;
43__END__
44
45=head1 NAME
46
47sort - perl pragma to control sort() behaviour
48
49=head1 SYNOPSIS
50
51The sort pragma is now a no-op, and its use is discouraged. These three
52operations are valid, but have no effect:
53
54    use sort 'stable';		# guarantee stability
55    use sort 'defaults';	# revert to default behavior
56    no  sort 'stable';		# stability not important
57
58=head1 DESCRIPTION
59
60Historically the C<sort> pragma you can control the behaviour of the builtin
61C<sort()> function.
62
63Prior to v5.28.0 there were two other options:
64
65    use sort '_mergesort';
66    use sort '_qsort';		# or '_quicksort'
67
68If you try and specify either of these in v5.28+ it will croak.
69
70The default sort has been stable since v5.8.0, and given this consistent
71behaviour for almost two decades, everyone has come to assume stability.
72
73Stability will remain the default - hence there is no need for a pragma for
74code to opt into stability "just in case" this changes - it won't.
75
76We do not foresee going back to offering multiple implementations of general
77purpose sorting - hence there is no future need to offer a pragma to choose
78between them.
79
80If you know that you care that much about performance of your sorting, and
81that for your use case and your data, it was worth investigating
82alternatives, possible to identify an alternative from our default that was
83better, and the cost of switching was worth it, then you know more than we
84do. Likely whatever choices we can give are not as good as implementing your
85own. (For example, a Radix sort can be faster than O(n log n), but can't be
86used for all keys and has larger overheads.)
87
88We are not averse to B<changing> the sort algorithm, but we don't see the
89benefit in offering the choice of two general purpose implementations.
90
91=head1 CAVEATS
92
93The function C<sort::current()> was provided to report the current state of
94the sort pragmata. This function was not exported, and there is no code to
95call it on CPAN. It is now deprecated, and will warn by default.
96
97As we no longer store any sort "state", it can no longer return the correct
98value, so it will always return the string C<stable>, as this is consistent
99with what we actually have implemented.
100
101=cut
102