File Coverage

blib/lib/Sort/Sub/numerically.pm
Criterion Covered Total %
statement 18 21 85.7
branch 4 6 66.6
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 28 36 77.7


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