Data/Unixish/trim.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 8 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 96.3% 52 / 54 CC 6 SCAR 10.4
CC
6
Cov
98%
_trim_item
5.0
trim
2.0
BEGIN
1.0
CRAP 2.8
« Data/Unixish/tail.pm ↑ Summary Data/Unixish/subsort.pm »
1 package Data::Unixish::trim;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use syntax 'each_on_array'; # to support perl < 5.12
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #use Log::Any '$log';
8  
9 3 ▶use Data::Unixish::Util qw(%common_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 3 our $DATE = '2025-02-24'; # DATE
13 3 our $DIST = 'Data-Unixish'; # DIST
14 3 our $VERSION = '1.574'; # VERSION
15  
16 3 our %SPEC;
17  
18 $SPEC{trim} = {
19 3 v => 1.1,
20 summary => 'Strip whitespace at the beginning and end of each line of text',
21 description => <<'MARKDOWN',
22  
23 MARKDOWN
24 args => {
25 %common_args,
26 strip_newline => {
27 summary => 'Whether to strip newlines at the '.
28 'beginning and end of text',
29 schema =>[bool => {default=>0}],
30 cmdline_aliases => { nl=>{} },
31 },
32 },
33 tags => [qw/text itemfunc/],
34 };
35 2 ▶sub trim {
Subroutine trim: called CC=2 SCAR=6.9
Pod documented
36 2 my %args = @_;
37 2 my ($in, $out) = ($args{in}, $args{out});
38  
39 2 ▶ while (my ($index, $item) = each @$in) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$in) 8 2 10
40 8 push @$out, _trim_item($item, \%args);
41 }
42  
43 2 [200, "OK"];
44 }
45  
46 20 ▶sub _trim_item {
Subroutine _trim_item: called CC=5 SCAR=16.1
47 20 my ($item, $args) = @_;
48  
49 20 ▶ if (defined($item) && !ref($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item and not ref $item) 16 4 20
Condition66%2/3
defined $item and not ref $item
!ll&&!rl&&r
0416
MC/DC50%1/2
defined $item and not ref $item
defined $item not ref $item
Truth table66%2/3
A and not(B)
A= defined $itemB= not ref $item
ABresult
0X0
100
111
50 16 ▶ $item =~ s/\A[\r\n]+// if $args->{strip_newline};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"strip_newline"} 6 10 16
51 16 ▶ $item =~ s/[\r\n]+\z// if $args->{strip_newline};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"strip_newline"} 6 10 16
52 16 $item =~ s/^[ \t]+//mg;
53 16 $item =~ s/[ \t]+$//mg;
54 }
55 20 return $item;
56 }
57  
58 3 1;
59 # ABSTRACT: Strip whitespace at the beginning and end of each line of text
60  
61 __END__
« Data/Unixish/tail.pm ↑ Summary Data/Unixish/subsort.pm »