File Coverage

blib/lib/Sort/Sub/by_length.pm
Criterion Covered Total %
statement 20 21 95.2
branch 4 6 66.6
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 30 36 83.3


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