Data/Unixish/linum.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 87.5% 7 / 8 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 91.3% 74 / 81 CC 11 SCAR 13.9
CC
11
Cov
94%
_linum_item
7.0
_linum_begin
4.0
__ANON__
2.0
CRAP 4.0
« Data/Unixish/sprintf.pm ↑ Summary Data/Unixish/cond.pm »
1 package Data::Unixish::linum;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use syntax 'each_on_array'; # to support perl < 5.12
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #use Log::Any '$log';
8  
9 2 ▶use Data::Unixish::Util qw(%common_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 2 our $DATE = '2025-02-24'; # DATE
13 2 our $DIST = 'Data-Unixish'; # DIST
14 2 our $VERSION = '1.574'; # VERSION
15  
16 2 our %SPEC;
17  
18 $SPEC{linum} = {
19 v => 1.1,
20 summary => 'Add line numbers',
21 args => {
22 %common_args,
23 format => {
24 summary => 'Sprintf-style format to use',
25 description => <<'MARKDOWN',
26  
27 Example: `%04d|`.
28  
29 MARKDOWN
30 schema => [str => default=>'%4s|'],
31 cmdline_aliases => { f=>{} },
32 },
33 start => {
34 summary => 'Number to start from',
35 schema => [int => default => 1],
36 cmdline_aliases => { s=>{} },
37 },
38 blank_empty_lines => {
39 schema => [bool => default=>1],
40 description => <<'MARKDOWN',
41  
42 Example when set to false:
43  
44 1|use Foo::Bar;
45 2|
46 3|sub blah {
47 4| my %args = @_;
48  
49 Example when set to true:
50  
51 1|use Foo::Bar;
52 |
53 3|sub blah {
54 4| my %args = @_;
55  
56 MARKDOWN
57 cmdline_aliases => {
58 b => {},
59 B => {
60 summary => 'Equivalent to --noblank-empty-lines',
61 0 ▶ code => sub { $_[0]{blank_empty_lines} = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 },
63 },
64 },
65 },
66 2 tags => [qw/text itemfunc/],
67 "x.dux.strip_newlines" => 0, # for duxapp < 1.41, will be removed later
68 "x.app.dux.strip_newlines" => 0,
69 };
70 3 ▶sub linum {
Subroutine linum: called CC=2 SCAR=6.9
Pod documented
71 3 my %args = @_;
72 3 my ($in, $out) = ($args{in}, $args{out});
73  
74 3 _linum_begin(\%args);
75 3 ▶ while (my ($index, $item) = each @$in) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$in) 9 3 12
76 9 push @$out, _linum_item($item, \%args);
77 }
78  
79 3 [200, "OK"];
80 }
81  
82 6 ▶sub _linum_begin {
Subroutine _linum_begin: called CC=4 SCAR=13.9
83 6 my $args = shift;
84  
85 6 ▶ $args->{format} //= '%4s|';
Condition100%2/2
$args->{"format"} //= "%4s|"
l!l
24
MC/DC100%1/1
$args->{"format"} //= "%4s|"
$args->{"format"}
Truth table100%2/2
A
A= $args->{"format"}
Aresult
11
00
86 6 ▶ $args->{blank_empty_lines} //= 1;
Condition100%2/2
$args->{"blank_empty_lines"} //= 1
l!l
24
MC/DC100%1/1
$args->{"blank_empty_lines"} //= 1
$args->{"blank_empty_lines"}
Truth table100%2/2
A
A= $args->{"blank_empty_lines"}
Aresult
11
00
87 6 ▶ $args->{start} //= 1;
Condition50%1/2
$args->{"start"} //= 1
l!l
06
MC/DC0%0/1
$args->{"start"} //= 1
$args->{"start"}
Truth table50%1/2
A
A= $args->{"start"}
Aresult
11
00
88  
89 # abuse, use args to store a temp var
90 6 $args->{_lineno} = $args->{start};
91 }
92  
93 18 ▶sub _linum_item {
Subroutine _linum_item: called CC=7 SCAR=19.5
94 18 my ($item, $args) = @_;
95  
96 18 ▶ if (defined($item) && !ref($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item and not ref $item) 14 4 18
Condition66%2/3
defined $item and not ref $item
!ll&&!rl&&r
0414
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
97 14 my @l;
98 14 for (split /^/, $item) {
99 22 my $n;
100 22 ▶ $n = ($args->{blank_empty_lines} && !/\S/) ? "" : $args->{_lineno};
Branch: 1 branch
BranchTrueFalseTotal
$args->{'blank_empty_lines'} && !/\S/ ? : 2 20 22
Condition100%3/3
$args->{"blank_empty_lines"} and not /\S/
!ll&&!rl&&r
10102
MC/DC100%2/2
$args->{"blank_empty_lines"} and not /\S/
$args->{"blank_empty_lines"} not /\S/
Truth table100%3/3
A and not(B)
A= $args->{"blank_empty_lines"}B= not /\S/
ABresult
0X0
100
111
101 22 push @l, sprintf($args->{format}, $n), $_;
102 22 $args->{_lineno}++;
103 }
104 14 $item = join "", @l;
105 14 ▶ chomp($item) if $args->{-dux_cli};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"-dux_cli"} 0 14 14
106 }
107 18 return $item;
108 }
109  
110 2 1;
111 # ABSTRACT: Add line numbers
112  
113 __END__
« Data/Unixish/sprintf.pm ↑ Summary Data/Unixish/cond.pm »