line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sort::Sub::by_rand; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
4
|
|
|
|
|
|
|
our $DATE = '2020-05-25'; # DATE |
5
|
|
|
|
|
|
|
our $DIST = 'Sort-Sub'; # DIST |
6
|
|
|
|
|
|
|
our $VERSION = '0.120'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
16
|
use 5.010; |
|
1
|
|
|
|
|
3
|
|
9
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
10
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
97
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub meta { |
13
|
|
|
|
|
|
|
return { |
14
|
1
|
|
|
1
|
0
|
4
|
v => 1, |
15
|
|
|
|
|
|
|
summary => "Sort randomly using Perl's rand()", |
16
|
|
|
|
|
|
|
}; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub gen_sorter { |
20
|
1
|
|
|
1
|
0
|
2
|
my ($is_reverse, $is_ci) = @_; |
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
0
|
|
5
|
sub { int(3*rand())-1 }; |
|
0
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
1; |
26
|
|
|
|
|
|
|
# ABSTRACT: Sort randomly using Perl's rand() |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
__END__ |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=pod |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=encoding UTF-8 |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 NAME |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Sort::Sub::by_rand - Sort randomly using Perl's rand() |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 VERSION |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
This document describes version 0.120 of Sort::Sub::by_rand (from Perl distribution Sort-Sub), released on 2020-05-25. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 SYNOPSIS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Generate sorter (accessed as variable) via L<Sort::Sub> import: |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
use Sort::Sub '$by_rand'; # use '$by_rand<i>' for case-insensitive sorting, '$by_rand<r>' for reverse sorting |
47
|
|
|
|
|
|
|
my @sorted = sort $by_rand ('item', ...); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Generate sorter (accessed as subroutine): |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
use Sort::Sub 'by_rand<ir>'; |
52
|
|
|
|
|
|
|
my @sorted = sort {by_rand} ('item', ...); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Generate directly without Sort::Sub: |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
use Sort::Sub::by_rand; |
57
|
|
|
|
|
|
|
my $sorter = Sort::Sub::by_rand::gen_sorter( |
58
|
|
|
|
|
|
|
ci => 1, # default 0, set 1 to sort case-insensitively |
59
|
|
|
|
|
|
|
reverse => 1, # default 0, set 1 to sort in reverse order |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
my @sorted = sort $sorter ('item', ...); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Use in shell/CLI with L<sortsub> (from L<App::sortsub>): |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
% some-cmd | sortsub by_rand |
66
|
|
|
|
|
|
|
% some-cmd | sortsub by_rand --ignore-case -r |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 DESCRIPTION |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
This is equivalent to: |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub { int(3*rand())-1 } |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
The case sensitivity (C<i>) and reverse (C<r>) flags are not relevant. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=for Pod::Coverage ^(gen_sorter|meta)$ |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 HOMEPAGE |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/Sort-Sub>. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 SOURCE |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Source repository is at L<https://github.com/perlancar/perl-Sort-Sub>. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 BUGS |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sort-Sub> |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
91
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
92
|
|
|
|
|
|
|
feature. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 SEE ALSO |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
L<Sort::Sub> |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 AUTHOR |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This software is copyright (c) 2020, 2019, 2018, 2016, 2015 by perlancar@cpan.org. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
107
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |