File Coverage

blib/lib/Sort/Sub/by_perl_op.pm
Criterion Covered Total %
statement 23 26 88.4
branch 6 10 60.0
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 35 45 77.7


line stmt bran cond sub pod time code
1             package Sort::Sub::by_perl_op;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-02-28'; # DATE
5             our $DIST = 'Sort-Sub'; # DIST
6             our $VERSION = '0.118'; # VERSION
7              
8 1     1   16 use 5.010;
  1         3  
9 1     1   5 use strict;
  1         2  
  1         16  
10 1     1   4 use warnings;
  1         1  
  1         128  
11              
12             sub meta {
13             return {
14 0     0 0 0 v => 1,
15             summary => 'Sort by Perl operator',
16             args => {
17             op => {
18             schema => ['str*', in=>['cmp', '<=>']],
19             req => 1,
20             },
21             },
22             };
23             }
24             sub gen_sorter {
25 3     3 0 5 my ($is_reverse, $is_ci, $args) = @_;
26              
27 3         5 my $op = $args->{op};
28 3 50       6 die "Please supply sorter argument 'op'"
29             unless defined $op;
30              
31 3         5 my $code_str = "sub { \$_[0] $op \$_[1] }";
32 3         184 my $code_cmp = eval $code_str;
33 3 50       8 die "Can't compile $code_str: $@" if $@;
34              
35             sub {
36 1     1   7 no strict 'refs';
  1         1  
  1         137  
37              
38 8     8   11 my $caller = caller();
39 8 50       14 my $a = @_ ? $_[0] : ${"$caller\::a"};
  0         0  
40 8 50       11 my $b = @_ ? $_[1] : ${"$caller\::b"};
  0         0  
41              
42 8         122 my $cmp = $code_cmp->($a, $b);
43 8 100       25 $is_reverse ? -1*$cmp : $cmp;
44 3         12 };
45             }
46              
47             1;
48             # ABSTRACT: Sort by Perl operator
49              
50             __END__