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_3';
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 getline 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 = { ( map { $_ => false } @head_key ) };
55  
56 1 ADJUST : params (:$decorate //= undef) {
57  
58 # my $headfield_ptn = join "|", @$prepend_head;
59 # const my $headfield_re => qr/($headfield_ptn)+/i;
60 if ($decorate) {
61 if ( $decorate == 1 ) {
62 $self->decorate(builtin::true);
63 }
64 elsif ( refstr($decorate) eq 'ARRAY' ) {
65 const my $headfield_re => eval "qr/(" . join "|",
66 @$decorate . ")+/i";
67 $$head{$_} = 1 for grep { $_ =~ $headfield_re } @$decorate;
68 }
69 elsif ( refstr($decorate) eq 'HASH' ) {
70 foreach my ( $k, $v ) (%$decorate) {
71 $$head{$k} = 1;
72 eval "\$${k}_head = [\$v]";
73 }
74 }
75 elsif ( $decorate == 0 ) {
76 $self->decorate(builtin::false);
77 }
78 }
79 else {
80 # $self->decorate
81 }
82  
83 # elsif ( !reftype($prepend_head) && $prepend_head =~ $headfield_re ) {
84 # $self->adjust( $prepend_head, 1 );
85 # }
86 };
87  
88 0 ▶method decorate ( $enable = undef, %opt ) {
Subroutine decorate: not called CC=8 SCAR=42.8
Pod undocumented
89 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
90 0 ▶ $$head{$_} = $enable ? true : false for keys %$head;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
91 }
92 elsif ( !$enable ) {
93 0 $$head{$_} = !$$head{$_} for keys %$head;
94 }
95 1 }
96  
97 1 const our $ltrimtab_re => qr/^\t/;
98 1 const our $lb_re => qr/\R/;
99  
100 0 ▶method dmsg {
Subroutine dmsg: not called CC=7 SCAR=40.3
Pod undocumented
101 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
102 0 my @caller = caller 1;
103  
104 0 local $Data::Dumper::Names::UpLevel = $ddn_uplvl;
105 0 local $Data::Dumper::Pad = " ";
106 0 local $Data::Dumper::Indent = 1;
107  
108 0 my $out;
109 0 $out .= Dumper(@_);
110 $out .=
111 $debug && $debug == 2
112 ? join "\n",
113 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
114 Devel::StackTrace::WithLexicals->new(
115 indent => $trace_indent // 1,
116 skip_frames => $skip_frames // 1
117 )->as_string
118 : "at $caller[1]:$caller[2]\n";
119  
120 0 $self->errh($out);
121 0 $out;
122 1 }
123  
124 0 ▶method writeh( $h, $line, %opt ) {
Subroutine writeh: not called CC=1 SCAR=6.9
Pod undocumented
125 0 $h->refresh( method => 'say', callarg => [$line], %opt );
126 1 }
127  
128 0 ▶method info ($line) {
Subroutine info: not called CC=2 SCAR=17.9
Pod undocumented
129 0 ▶ $line = "$info_head $line" if $$head{info};
Branch: 1 branch
BranchTrueFalseTotal
if $head->{"info"} 0 0 0
130 0 $self->errh($line);
131 1 }
132  
133 0 ▶method error ($line) {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
134 0 ▶ $line = "$err_head $line" if $$head{error};
Branch: 1 branch
BranchTrueFalseTotal
if $head->{"error"} 0 0 0
135 0 $self->errh($line);
136 1 }
137  
138 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
139 0 ▶ $status = $status >> 8 if ( $status > 255 );
Branch: 1 branch
BranchTrueFalseTotal
if $status > 255 0 0 0
140  
141 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
142 unless $status >= 0 || $status <= 255;
143  
144 0 $self->error( $line, %opt );
145 0 croak $status;
146 1 }
147  
148 0 ▶method success ($line) {
Subroutine success: not called CC=2 SCAR=17.9
Pod undocumented
149 0 ▶ $line = "$success_head $line" if $$head{success};
Branch: 1 branch
BranchTrueFalseTotal
if $head->{"success"} 0 0 0
150 0 $self->outh($line);
151 1 }
152  
153 0 ▶method msg ($line) {
Subroutine msg: not called CC=2 SCAR=17.9
Pod undocumented
154 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
155 1 }
156  
157 # method headfmt( $instr, %opt ) {
158 # $instr;
159 # }
160  
161 # method getc { state $h = $self->refresh( $h, 'getc', fd => *STDIN, mode => 'r', %opt ); }
162  
163 0 ▶method outh ( $line, %opt ) {
Subroutine outh: not called CC=2 SCAR=17.9
Pod undocumented
164 0 %opt = ( fd => *STDOUT, mode => '>' );
165 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
166 0 $h = $self->writeh( $h, $line, %opt );
167 1 }
168  
169 0 ▶method errh ( $line, %opt ) {
Subroutine errh: not called CC=2 SCAR=17.9
Pod undocumented
170 0 %opt = ( fd => *STDERR, mode => '>' );
171 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
172 0 $h = $self->writeh( $h, $line, %opt );
173 1 }
174  
175 0 ▶method readh (%opt) {
Subroutine readh: not called CC=2 SCAR=17.9
Pod undocumented
176 0 %opt = ( fd => *STDIN, mode => '<', %opt );
177 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
178 0 $h = $self->refresh(%opt);
179 1 }
180  
181 0 ▶method getline (%opt) {
Subroutine getline: not called CC=1 SCAR=6.9
Pod undocumented
182 0 %opt = ( fd => *STDIN, mode => '<', %opt );
183 0 $self->readh( method => 'getline', %opt );
184 1 }
185  
186 0 ▶method prompt (%opt) {
Subroutine prompt: not called CC=2 SCAR=17.9
Pod undocumented
187 0 %opt = ( fd => *STDIN, mode => '<', %opt );
188 my $prompt = (
189 0 ▶ delete $opt{prompt} // sub ( $h, %opt ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 print $opt{prompt};
191 0 ( delete $opt{get} )->( $h, %opt );
192 }
193 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
194  
195 0 $self->readh( on => $prompt, %opt );
196 }
197  
198 =encoding utf-8
199  
200 =head1 NAME
201  
202 IO::Handle::Common - Nearly ubiquitous methods for script and file IO
203  
204 =head1 SYNOPSIS
205  
206 use IO::Handle::Common; # Exports info, error, fatal, success, msg, dmsg and path by default
207  
208 # Pretty-print @args only if $debug || $ENV{DEBUG} is set to something truth-y
209 dmsg ...; #
210  
211 fatal "$?..." if $?; # die without line numbers or other non-enduser information
212  
213 success "..." # ...
214  
215 =head1 DESCRIPTION
216  
217 IO::Handle::Common is a collection of methods useful for script output, both
218 debug and user-facing, and file-based IO wrapped loosely around IO::Handle and
219 Path::Tiny.
220  
221 =head1 AUTHOR
222  
223 Ian P Bradley E<lt>ian@pennyfoss.orgE<gt>
224  
225 =head1 COPYRIGHT
226  
227 Copyright 2026- Ian P Bradley
228  
229 =head1 LICENSE
230  
231 This library is free software; you can redistribute it and/or modify
232 it under the same terms as Perl itself.
233  
234 =head1 SEE ALSO
235  
236 =cut
« Common/Handle.pm ↑ Summary Common/Base.pm »