File Coverage

blib/lib/Data/Unixish/trim.pm
Criterion Covered Total %
statement 26 26 100.0
branch 6 6 100.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 42 43 97.6


line stmt bran cond sub pod time code
1             package Data::Unixish::trim;
2              
3 2     2   461 use 5.010;
  2         9  
4 2     2   12 use strict;
  2         5  
  2         45  
5 2     2   352 use syntax 'each_on_array'; # to support perl < 5.12
  2         20260  
  2         20  
6 2     2   3186 use warnings;
  2         4  
  2         107  
7             #use Log::Any '$log';
8              
9             our $VERSION = '1.571'; # VERSION
10              
11 2     2   387 use Data::Unixish::Util qw(%common_args);
  2         5  
  2         778  
12              
13             our %SPEC;
14              
15             $SPEC{trim} = {
16             v => 1.1,
17             summary => 'Strip whitespace at the beginning and end of each line of text',
18             description => <<'_',
19              
20             _
21             args => {
22             %common_args,
23             strip_newline => {
24             summary => 'Whether to strip newlines at the '.
25             'beginning and end of text',
26             schema =>[bool => {default=>0}],
27             cmdline_aliases => { nl=>{} },
28             },
29             },
30             tags => [qw/text itemfunc/],
31             };
32             sub trim {
33 2     2 1 6 my %args = @_;
34 2         4 my ($in, $out) = ($args{in}, $args{out});
35              
36 2         10 while (my ($index, $item) = each @$in) {
37 8         12 push @$out, _trim_item($item, \%args);
38             }
39              
40 2         13 [200, "OK"];
41             }
42              
43             sub _trim_item {
44 20     20   34 my ($item, $args) = @_;
45              
46 20 100 66     69 if (defined($item) && !ref($item)) {
47 16 100       40 $item =~ s/\A[\r\n]+// if $args->{strip_newline};
48 16 100       35 $item =~ s/[\r\n]+\z// if $args->{strip_newline};
49 16         47 $item =~ s/^[ \t]+//mg;
50 16         43 $item =~ s/[ \t]+$//mg;
51             }
52 20         60 return $item;
53             }
54              
55             1;
56             # ABSTRACT: Strip whitespace at the beginning and end of each line of text
57              
58             __END__