Common.pm

filter: Statementstmt 41.2% 80 / 194 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 43.5% 17 / 39 Podpod 0.0% 0 / 21 totaltotal 28.3% 97 / 342 CC 35 SCAR 29.5
CC
35
Cov
31%
decorate
72.0
dmsg
56.0
fatal
42.0
CRAP 19.2
« Common/Handle.pm ↑ Summary Common/Base.pm »
1 1 ▶use Object::Pad ':experimental(:all)';
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package IO::Handle::Common;
4  
5 1 ▶class IO::Handle::Common : does(IO::Handle::Common::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '0.01_2';
7  
8 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp qw'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper::Names;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Devel::StackTrace::WithLexicals;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use PadWalker;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use IO::Handle::Common::Base qw'refstr';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IO::Handle::Common::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use List::Util qw'any first pairmap';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use builtin;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use parent 'Class::Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use vars qw'@EXPORT @EXPORT_OK';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 @EXPORT = qw(dmsg info success error fatal msg readline prompt);
27 1 @EXPORT_OK = (@EXPORT);
28  
29 field $debug : param = $ENV{DEBUG} // 0;
30  
31 0 field $ddn_uplvl : param : accessor = 3;
32 0 ▶field $trace_indent : param : accessor = $ENV{DEBUG_INDENT} // 1;
Subroutine ddn_uplvl: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $ddn_uplvl = shift() 0 0 0
33 0 ▶field $skip_frames : param : accessor = $ENV{DEBUG_SKIPFRAMES} // 1;
Subroutine trace_indent: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $trace_indent = shift() 0 0 0
34  
35 0 ▶field $info_head : param : accessor = [qw'▶ ▷'];
Subroutine skip_frames: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $skip_frames = shift() 0 0 0
36 0 ▶field $err_head : param : accessor = ['❌️'];
Subroutine info_head: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $info_head = shift() 0 0 0
37 0 ▶field $field_head : param : accessor = [@$err_head];
Subroutine err_head: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $err_head = shift() 0 0 0
38 0 ▶field $success_head : param : accessor = ['⭕️'];
Subroutine field_head: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $field_head = shift() 0 0 0
39 0 ▶field $msg_head : param : accessor = [undef];
Subroutine success_head: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $success_head = shift() 0 0 0
40  
41 # field $dmsg_head : accessor;
42  
43 # ADJUST : params (:$dmsg_head = undef) {
44 # $dmsg_head //= [
45 # sub {
46 # $self->headfmt(
47 # Time::Piece->now->to_string('%Y-%m-%d %H:%M:%S%f') );
48 # }
49 # ]
50 # };
51  
52 0 ▶const my @head_key => qw(info err success fatal msg dmsg);
Subroutine msg_head: not called CC=2 SCAR=10.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $msg_head = shift() 0 0 0
53  
54 field $head = {
55 ( map { $_ => false } @head_key )
56 };
57  
58 1 ADJUST : params (:$decorate //= undef) {
59  
60 # my $headfield_ptn = join "|", @$prepend_head;
61 # const my $headfield_re => qr/($headfield_ptn)+/i;
62 if ($decorate) {
63 if ( $decorate == 1 ) {
64 $self->decorate(builtin::true)
65 }
66 elsif ( refstr($decorate) eq 'ARRAY' ) {
67 const my $headfield_re => eval "qr/(" . join "|",
68 @$decorate . ")+/i";
69 $$head{$_} = 1 for grep { $_ =~ $headfield_re } @$decorate;
70 }
71 elsif ( refstr($decorate) eq 'HASH' ) {
72 foreach my ( $k, $v ) (%$decorate) {
73 $$head{$k} = 1;
74 eval "\$${k}_head = [\$v]";
75 }
76 }
77 elsif ($decorate == 0) {
78 $self->decorate(builtin::false)
79 }
80 }
81 else {
82 # $self->decorate
83 }
84  
85 # elsif ( !reftype($prepend_head) && $prepend_head =~ $headfield_re ) {
86 # $self->adjust( $prepend_head, 1 );
87 # }
88 };
89  
90 0 ▶method decorate ( $enable = undef, %opt ) {
Subroutine decorate: not called CC=8 SCAR=42.8
Pod undocumented
91 0 ▶ if ( defined $enable && !reftype($enable) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $enable and not builtin::reftype($enable)) { } 0 0 0
elsif (not $enable) { } 0 0 0
Condition0%0/3
defined $enable and not builtin::reftype($enable)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $enable and not builtin::reftype($enable)
defined $enable not builtin::reftype($enable)
Truth table0%0/3
A and not(B)
A= defined $enableB= not builtin::reftype($enable)
ABresult
0X0
100
111
92 0 ▶ $$head{$_} = $enable ? true : false for keys %$head
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
93 }
94 elsif(!$enable) {
95 0 $$head{$_} = !$$head{$_} for keys %$head
96 }
97 1 }
98  
99 1 const our $ltrimtab_re => qr/^\t/;
100 1 const our $lb_re => qr/\R/;
101  
102 0 ▶method dmsg {
Subroutine dmsg: not called CC=7 SCAR=40.3
Pod undocumented
103 0 ▶ return unless $debug // $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
unless $debug // $ENV{"DEBUG"} 0 0 0
Condition0%0/3
$debug // $ENV{"DEBUG"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$debug // $ENV{"DEBUG"}
$debug $ENV{"DEBUG"}
Truth table0%0/3
A // B
A= $debugB= $ENV{"DEBUG"}
ABresult
1X1
011
000
104 0 my @caller = caller 1;
105  
106 0 local $Data::Dumper::Names::UpLevel = $ddn_uplvl;
107 0 local $Data::Dumper::Pad = " ";
108 0 local $Data::Dumper::Indent = 1;
109  
110 0 my $out;
111 0 $out .= Dumper(@_);
112 $out .=
113 $debug && $debug == 2
114 ? join "\n",
115 0 ▶ map { ( my $line = $_ ) =~ s/$ltrimtab_re/ /; " $line" } split /$lb_re/,
Branch: 1 branch
BranchTrueFalseTotal
$debug && $debug == 2 ? : 0 0 0
Condition0%0/7
$debug and $debug == 2
!ll&&!rl&&r
000
$trace_indent // 1
l!l
00
$skip_frames // 1
l!l
00
MC/DC0%0/2
$debug and $debug == 2
$debug $debug == 2
MC/DC0%0/1
$trace_indent // 1
$trace_indent
MC/DC0%0/1
$skip_frames // 1
$skip_frames
Truth table0%0/3
A and B
A= $debugB= $debug == 2
ABresult
0X0
100
111
Truth table0%0/2
A
A= $trace_indent
Aresult
11
00
Truth table0%0/2
A
A= $skip_frames
Aresult
11
00
116 Devel::StackTrace::WithLexicals->new(
117 indent => $trace_indent // 1,
118 skip_frames => $skip_frames // 1
119 )->as_string
120 : "at $caller[1]:$caller[2]\n";
121  
122 0 $self->errh($out);
123 0 $out;
124 1 }
125  
126 0 ▶method writeh( $h, $line, %opt ) {
Subroutine writeh: not called CC=1 SCAR=6.9
Pod undocumented
127 0 $h->refresh( method => 'say', callarg => [$line], %opt );
128 1 }
129  
130 0 ▶method info ($line) {
Subroutine info: not called CC=2 SCAR=17.9
Pod undocumented
131 0 ▶ $line = "$info_head $line" if $$head{info};
Branch: 1 branch
BranchTrueFalseTotal
if $head->{"info"} 0 0 0
132 0 $self->errh($line);
133 1 }
134  
135 0 ▶method error ($line) {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
136 0 ▶ $line = "$err_head $line" if $$head{error};
Branch: 1 branch
BranchTrueFalseTotal
if $head->{"error"} 0 0 0
137 0 $self->errh($line);
138 1 }
139  
140 0 ▶method fatal ( $line, $status = ( $? || 255 ), %opt ) {
Subroutine fatal: not called CC=6 SCAR=37.4
Pod undocumented
Condition0%0/2
$? || 255
l!l
00
MC/DC0%0/1
$? || 255
$?
Truth table0%0/2
A
A= $?
Aresult
11
00
141 0 ▶ $status = $status >> 8 if ( $status > 255 );
Branch: 1 branch
BranchTrueFalseTotal
if $status > 255 0 0 0
142  
143 0 ▶ error "Status ($status) must be between 0 and 255"
Branch: 1 branch
BranchTrueFalseTotal
unless $status >= 0 or $status <= 255 0 0 0
Condition0%0/3
$status >= 0 or $status <= 255
l!l&&r!l&&!r
000
MC/DC0%0/2
$status >= 0 or $status <= 255
$status >= 0 $status <= 255
Truth table0%0/3
A or B
A= $status >= 0B= $status <= 255
ABresult
1X1
011
000
144 unless $status >= 0 || $status <= 255;
145  
146 0 $self->error( $line, %opt );
147 0 croak $status;
148 1 }
149  
150 0 ▶method success ($line) {
Subroutine success: not called CC=2 SCAR=17.9
Pod undocumented
151 0 ▶ $line = "$success_head $line" if $$head{success};
Branch: 1 branch
BranchTrueFalseTotal
if $head->{"success"} 0 0 0
152 0 $self->outh($line);
153 1 }
154  
155 0 ▶method msg ($line) {
Subroutine msg: not called CC=2 SCAR=17.9
Pod undocumented
156 0 ▶ $self->outh( ( $$head{msg} // '' ) . $line );
Condition0%0/2
$head->{"msg"} // ""
l!l
00
MC/DC0%0/1
$head->{"msg"} // ""
$head->{"msg"}
Truth table0%0/2
A
A= $head->{"msg"}
Aresult
11
00
157 1 }
158  
159 # method headfmt( $instr, %opt ) {
160 # $instr;
161 # }
162  
163 # method getc { state $h = $self->refresh( $h, 'getc', fd => *STDIN, mode => 'r', %opt ); }
164  
165 0 ▶method outh ( $line, %opt ) {
Subroutine outh: not called CC=2 SCAR=17.9
Pod undocumented
166 0 %opt = ( fd => *STDOUT, mode => '>' );
167 0 ▶ state $h //= IO::Handle::Common::Handle->new(%opt);
Condition0%0/3
state $h //= "IO::Handle::Common::Handle"->new(%opt)
l!l&&r!l&&!r
000
MC/DC0%0/2
state $h //= "IO::Handle::Common::Handle"->new(%opt)
state $h "IO::Handle::Common::Handle"->new(%opt)
Truth table0%0/3
A //= B
A= state $hB= "IO::Handle::Common::Handle"->new(%opt)
ABresult
1X1
011
000
168 0 $h = $self->writeh( $h, $line, %opt );
169 1 }
170  
171 0 ▶method errh ( $line, %opt ) {
Subroutine errh: not called CC=2 SCAR=17.9
Pod undocumented
172 0 %opt = ( fd => *STDERR, mode => '>' );
173 0 ▶ state $h //= IO::Handle::Common::Handle->new(%opt);
Condition0%0/3
state $h //= "IO::Handle::Common::Handle"->new(%opt)
l!l&&r!l&&!r
000
MC/DC0%0/2
state $h //= "IO::Handle::Common::Handle"->new(%opt)
state $h "IO::Handle::Common::Handle"->new(%opt)
Truth table0%0/3
A //= B
A= state $hB= "IO::Handle::Common::Handle"->new(%opt)
ABresult
1X1
011
000
174 0 $h = $self->writeh( $h, $line, %opt );
175 1 }
176  
177 0 ▶method readh ( %opt ) {
Subroutine readh: not called CC=2 SCAR=17.9
Pod undocumented
178 0 %opt = ( fd => *STDIN, mode => '<', %opt );
179 0 ▶ state $h //= IO::Handle::Common::Handle->new(%opt);
Condition0%0/3
state $h //= "IO::Handle::Common::Handle"->new(%opt)
l!l&&r!l&&!r
000
MC/DC0%0/2
state $h //= "IO::Handle::Common::Handle"->new(%opt)
state $h "IO::Handle::Common::Handle"->new(%opt)
Truth table0%0/3
A //= B
A= state $hB= "IO::Handle::Common::Handle"->new(%opt)
ABresult
1X1
011
000
180 0 $h = $self->refresh(%opt);
181 1 }
182  
183 0 ▶method getline (%opt) {
Subroutine getline: not called CC=1 SCAR=6.9
Pod undocumented
184 0 %opt = ( fd => *STDIN, mode => '<', %opt );
185 0 $self->readh( method => 'getline', %opt );
186 1 }
187  
188 0 ▶method prompt (%opt) {
Subroutine prompt: not called CC=2 SCAR=17.9
Pod undocumented
189 0 %opt = ( fd => *STDIN, mode => '<', %opt );
190 my $prompt = (
191 0 ▶ delete $opt{prompt} // sub ( $h, %opt ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 print $opt{prompt};
193 0 ( delete $opt{get} )->( $h, %opt );
194 }
195 0 ▶ );
Condition0%0/3
delete $opt{"prompt"} // sub ($h, %opt) { print $opt{"prompt"}; delete $opt{"get"}->($h, %opt); }
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opt{"prompt"} // sub ($h, %opt) { print $opt{"prompt"}; delete $opt{"get"}->($h, %opt); }
delete $opt{"prompt"} sub ($h, %opt) { print $opt{"prompt"}; delete $opt{"get"}->($h, %opt); }
Truth table0%0/3
A // B
A= delete $opt{"prompt"}B= sub ($h, %opt) { print $opt{"prompt"}; delete $opt{"get"}->($h, %opt); }
ABresult
1X1
011
000
196  
197 0 $self->readh( on => $prompt, %opt );
198 }
199  
200 =encoding utf-8
201  
202 =head1 NAME
203  
204 IO::Handle::Common - Nearly ubiquitous methods for script and file IO
205  
206 =head1 SYNOPSIS
207  
208 use IO::Handle::Common; # Exports info, error, fatal, success, msg, dmsg and path by default
209  
210 # Pretty-print @args only if $debug || $ENV{DEBUG} is set to something truth-y
211 dmsg ...; #
212  
213 fatal "$?..." if $?; # die without line numbers or other non-enduser information
214  
215 success "..." # ...
216  
217 =head1 DESCRIPTION
218  
219 IO::Handle::Common is a collection of methods useful for script output, both
220 debug and user-facing, and file-based IO wrapped loosely around IO::Handle and
221 Path::Tiny.
222  
223 =head1 AUTHOR
224  
225 Ian P Bradley E<lt>ian@pennyfoss.orgE<gt>
226  
227 =head1 COPYRIGHT
228  
229 Copyright 2026- Ian P Bradley
230  
231 =head1 LICENSE
232  
233 This library is free software; you can redistribute it and/or modify
234 it under the same terms as Perl itself.
235  
236 =head1 SEE ALSO
237  
238 =cut
« Common/Handle.pm ↑ Summary Common/Base.pm »