File Coverage

blib/lib/Sort/Sub/naturally.pm
Criterion Covered Total %
statement 22 23 95.6
branch 6 6 100.0
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 34 38 89.4


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