line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Unixish::grep; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
578
|
use 5.010; |
|
1
|
|
|
|
|
6
|
|
4
|
1
|
|
|
1
|
|
375
|
use locale; |
|
1
|
|
|
|
|
521
|
|
|
1
|
|
|
|
|
4
|
|
5
|
1
|
|
|
1
|
|
32
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
6
|
1
|
|
|
1
|
|
341
|
use syntax 'each_on_array'; # to support perl < 5.12 |
|
1
|
|
|
|
|
20344
|
|
|
1
|
|
|
|
|
5
|
|
7
|
1
|
|
|
1
|
|
3218
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
8
|
|
|
|
|
|
|
#use Log::Any '$log'; |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
437
|
use Data::Unixish::Util qw(%common_args); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
384
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '1.570'; # VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our %SPEC; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
$SPEC{grep} = { |
17
|
|
|
|
|
|
|
v => 1.1, |
18
|
|
|
|
|
|
|
summary => 'Perl grep', |
19
|
|
|
|
|
|
|
description => <<'_', |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Filter each item through a callback. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
_ |
24
|
|
|
|
|
|
|
args => { |
25
|
|
|
|
|
|
|
%common_args, |
26
|
|
|
|
|
|
|
callback => { |
27
|
|
|
|
|
|
|
summary => 'The callback code or regexp to use', |
28
|
|
|
|
|
|
|
schema => ['any*' => of => ['str*', 're*', 'code*']], |
29
|
|
|
|
|
|
|
req => 1, |
30
|
|
|
|
|
|
|
pos => 0, |
31
|
|
|
|
|
|
|
}, |
32
|
|
|
|
|
|
|
}, |
33
|
|
|
|
|
|
|
tags => [qw/filtering perl unsafe/], |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
sub grep { |
36
|
4
|
|
|
4
|
1
|
12
|
my %args = @_; |
37
|
4
|
|
|
|
|
8
|
my ($in, $out) = ($args{in}, $args{out}); |
38
|
4
|
50
|
|
|
|
11
|
my $callback = $args{callback} or die "missing callback for grep"; |
39
|
4
|
100
|
|
|
|
20
|
if (ref($callback) eq ref(qr{})) { |
|
|
100
|
|
|
|
|
|
40
|
1
|
|
|
|
|
2
|
my $re = $callback; |
41
|
1
|
|
|
4
|
|
5
|
$callback = sub { $_ =~ $re }; |
|
4
|
|
|
|
|
35
|
|
42
|
|
|
|
|
|
|
} elsif (ref($callback) ne 'CODE') { |
43
|
1
|
50
|
|
|
|
4
|
if ($args{-cmdline}) { |
44
|
0
|
|
|
|
|
0
|
$callback = eval "no strict; no warnings; sub { $callback }"; |
45
|
0
|
0
|
|
|
|
0
|
die "invalid code for grep: $@" if $@; |
46
|
|
|
|
|
|
|
} else { |
47
|
1
|
|
|
|
|
8
|
die "Please supply coderef (or regex) for 'callback'"; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
|
|
|
24
|
local ($., $_); |
52
|
3
|
|
|
|
|
12
|
while (($., $_) = each @$in) { |
53
|
24
|
100
|
|
|
|
75
|
push @$out, $_ if $callback->(); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
3
|
|
|
|
|
17
|
[200, "OK"]; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
1; |
60
|
|
|
|
|
|
|
# ABSTRACT: Perl grep |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
__END__ |