lib/IO/Factotum.pm

filter: Statementstmt 88.2% 98 / 111 Branchbran 66.6% 32 / 48 Conditioncond 68.5% 24 / 35 MC/DCmcdc 40.0% 8 / 20 Subroutinesub 94.4% 17 / 18 Podpod 62.5% 5 / 8 totaltotal 76.6% 184 / 240 CC 41 SCAR 23.7
CC
41
Cov
79%
load
30.0
register
15.1
convert
14.7
CRAP 10.7
↑ Summary blib/lib/Getopt/EX/Factotum.pm »
1 package IO::Factotum;
2  
3 54 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 54 our $VERSION = "0.05";
7  
8 54 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 54 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 54 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 54 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 54 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 54 ▶use Command::Run::Tmpfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 54 my @converter;
17  
18 102 ▶sub register {
Subroutine register: called CC=14 SCAR=27.1
Pod documented
19 102 my %arg = @_;
20 102 my $match = delete $arg{match};
21 102 my $command = delete $arg{command};
22 102 my $with = delete $arg{with};
23 0 ▶ croak "register: unknown parameter: @{[ sort keys %arg ]}" if %arg;
Branch: 1 branch
BranchTrueFalseTotal
if %arg 0 102 102
24 102 ▶ if (defined $with) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $with) { } 20 82 102
25 # An object which gives the content of a file by its method
26 # file. It tells by itself which files are its own, so match
27 # is not required.
28 20 ▶ croak "register: with and command can not be given together"
Branch: 1 branch
BranchTrueFalseTotal
if defined $command 2 18 20
29 if defined $command;
30 18 ▶ Scalar::Util::blessed($with) && $with->can('file')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($with) and $with->can("file") or croak("register: with must be an object which has the method file") 14 4 18
Condition100%3/3
builtin::blessed($with) and $with->can("file")
!ll&&!rl&&r
2214
MC/DC100%2/2
builtin::blessed($with) and $with->can("file")
builtin::blessed($with) $with->can("file")
Truth table100%3/3
A and B
A= builtin::blessed($with)B= $with->can("file")
ABresult
0X0
100
111
31 or croak "register: with must be an object which has the method file";
32 } else {
33 82 ▶ $match // croak "register: match is required";
Condition100%2/2
$match // croak("register: match is required")
l!l
802
MC/DC100%1/1
$match // croak("register: match is required")
$match
Truth table100%2/2
A
A= $match
Aresult
11
00
34 80 ▶ $command // croak "register: command is required";
Condition50%1/2
$command // croak("register: command is required")
l!l
800
MC/DC0%0/1
$command // croak("register: command is required")
$command
Truth table50%1/2
A
A= $command
Aresult
11
00
35 80 ▶ ref $command eq 'ARRAY' && @$command
Branch: 1 branch
BranchTrueFalseTotal
ref $command eq "ARRAY" and @$command or croak("register: command must be an array reference") 80 0 80
Condition33%1/3
ref $command eq "ARRAY" and @$command
!ll&&!rl&&r
0080
MC/DC0%0/2
ref $command eq "ARRAY" and @$command
ref $command eq "ARRAY" @$command
Truth table33%1/3
A and B
A= ref $command eq "ARRAY"B= @$command
ABresult
0X0
100
111
36 or croak "register: command must be an array reference";
37 }
38 94 ▶ !defined $match or ref $match eq 'Regexp' or ref $match eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
not defined $match or ref $match eq "Regexp" or ref $match eq "CODE" or croak("register: match must be a regex or a code reference") 94 0 94
Condition83%5/6
not defined $match or ref $match eq "Regexp" or ref $match eq "CODE"
l!l&&r!l&&!r
9130
not defined $match or ref $match eq "Regexp"
l!l&&r!l&&!r
13783
MC/DC0%0/3
not defined $match or ref $match eq "Regexp" or ref $match eq "CODE"
not defined $match ref $match eq "Regexp" ref $match eq "CODE"
Truth table75%3/4
not(A) or B or C
A= not defined $matchB= ref $match eq "Regexp"C= ref $match eq "CODE"
ABCresult
1XX1
01X1
0011
0000
39 or croak "register: match must be a regex or a code reference";
40 94 ▶ push @converter, {
Branch: 1 branch
BranchTrueFalseTotal
defined $with ? : 14 80 94
41 match => $match,
42 defined $with ? (with => $with) : (command => [ @$command ]),
43 };
44 94 return;
45 }
46  
47 49 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
48 49 @converter = ();
49 49 return;
50 }
51  
52 # The converters which take the file, in the order they were registered
53 124 ▶sub converter {
Subroutine converter: called CC=3 SCAR=11.0
Pod undocumented
54 124 my $file = shift;
55 grep {
56 124 my $match = $_->{match};
57 131 ▶ !defined $match or ref $match eq 'CODE' ? $match->($file) : $file =~ $match;
Branch: 1 branch
BranchTrueFalseTotal
ref $match eq 'CODE' ? : 6 105 111
Condition100%3/3
!defined($match) || (ref $match eq 'CODE' ? &$match($file) : $file =~ /$match/u)
l!l&&r!l&&!r
209912
MC/DC100%2/2
!defined($match) || (ref $match eq 'CODE' ? &$match($file) : $file =~ /$match/u)
!defined($match) (ref $match eq 'CODE' ? &$match($file) : $file =~ /$match/u)
Truth table100%3/3
not(A) || B
A= !defined($match)B= (ref $match eq 'CODE' ? &$match($file) : $file =~ /$match/u)
ABresult
1X1
011
000
58 } @converter;
59 }
60  
61 136 ▶sub open_input {
Subroutine open_input: called CC=7 SCAR=19.5
Pod documented
62 136 my $file = shift;
63  
64 # The standard input is read as it is, by STDIN itself: another
65 # handle on it would not see what STDIN has read ahead
66 136 ▶ return *STDIN{IO} if $file eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "-" 4 132 136
67  
68 # The file is opened first: a file which can not be read is an
69 # error of the caller with $! set, whatever the converter does
70 132 ▶ my $fh = IO::File->new($file, '<') // return undef;
Condition100%2/2
"IO::File"->new($file, "<") // (return undef)
l!l
1248
MC/DC100%1/1
"IO::File"->new($file, "<") // (return undef)
"IO::File"->new($file, "<")
Truth table100%2/2
A
A= "IO::File"->new($file, "<")
Aresult
11
00
71 124 binmode $fh, ':raw';
72  
73 124 for my $c (converter($file)) {
74 111 ▶ if (my $with = $c->{with}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $with = $c->{"with"}) 17 94 111
75 # an object which gives nothing passes the file to the next
76 17 ▶ my $converted = content($with, $file) // next;
Condition100%2/2
content($with, $file) // (next)
l!l
116
MC/DC100%1/1
content($with, $file) // (next)
content($with, $file)
Truth table100%2/2
A
A= content($with, $file)
Aresult
11
00
77 11 return $converted;
78 }
79 # a command which fails leaves the file as it is
80 94 ▶ return convert($c->{command}, $file) // $fh;
Condition66%2/3
convert($c->{"command"}, $file) // $fh
l!l&&r!l&&!r
5670
MC/DC0%0/2
convert($c->{"command"}, $file) // $fh
convert($c->{"command"}, $file) $fh
Truth table66%2/3
A // B
A= convert($c->{"command"}, $file)B= $fh
ABresult
1X1
011
000
81 }
82 19 $fh;
83 }
84  
85 # Ask the object for the content of the file, and return the handle of
86 # a temporary file which has it, or undef when the object gives
87 # nothing, or dies. Characters are written as UTF-8.
88 17 ▶sub content {
Subroutine content: called CC=5 SCAR=16.2
Pod undocumented
89 17 my($with, $file) = @_;
90 17 ▶ my $content = eval { $with->file($file) } // return undef;
Condition100%2/2
eval { do { $with->file($file) } } // (return undef)
l!l
116
MC/DC100%1/1
eval { do { $with->file($file) } } // (return undef)
eval { do { $with->file($file) } }
Truth table100%2/2
A
A= eval { do { $with->file($file) } }
Aresult
11
00
91 11 ▶ utf8::encode($content) if utf8::is_utf8($content);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($content) 2 9 11
92 11 my $tmp = Command::Run::Tmpfile->new(raw => 1);
93 11 my $fh = $tmp->fh;
94 11 ▶ print $fh $content or return undef;
Branch: 1 branch
BranchTrueFalseTotal
print $fh $content or return undef 11 0 11
95 11 $fh->flush;
96 11 ▶ $fh->seek(0, 0) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or return undef 11 0 11
97 11 $fh;
98 }
99  
100 # Run the command with the file name, in a process of its own, and
101 # return the handle of a temporary file which has what it wrote to the
102 # standard output, or undef when it fails. The output goes to the file
103 # as it is, byte for byte. The standard error is thrown away.
104 94 ▶sub convert {
Subroutine convert: called CC=11 SCAR=26.9
Pod undocumented
105 94 my($command, $file) = @_;
106 94 my @command = (@$command, $file);
107 94 my $tmp = Command::Run::Tmpfile->new(raw => 1);
108  
109 # $? and $! of the caller are kept, and the child is waited for
110 # even when the caller ignores SIGCHLD
111 94 local($?, $!);
112 94 local $SIG{CHLD} = 'DEFAULT';
113 # what is in the buffers is not to be written twice
114 94 STDOUT->flush;
115 94 STDERR->flush;
116  
117 94 ▶ my $pid = fork // return undef;
Condition50%1/2
fork // (return undef)
l!l
940
MC/DC0%0/1
fork // (return undef)
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
118 94 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 31 63 94
119 # The child never returns to the program: it ends by _exit, so
120 # that the END blocks and the destructors of the program do not
121 # run here, even when open_input is called inside eval.
122 31 ▶ my $status = eval {
Condition0%0/2
eval { do { open STDIN, "<", "File::Spec"->devnull or die; open STDOUT, ">&", $tmp->fh or die; open STDERR, ">", "File::Spec"->devnull or die; binmode STDOUT, ":raw"; if (ref $command[0] eq "CODE") { my $code = shift @command; &$code(@command); "STDOUT"->flush; 0; } else { BEGIN {${^WARNING_BITS} = "\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} exec {$command[0];} @command or POSIX::_exit(127); } } } // 1
l!l
00
MC/DC0%0/1
eval { do { open STDIN, "<", "File::Spec"->devnull or die; open STDOUT, ">&", $tmp->fh or die; open STDERR, ">", "File::Spec"->devnull or die; binmode STDOUT, ":raw"; if (ref $command[0] eq "CODE") { my $code = shift @command; &$code(@command); "STDOUT"->flush; 0; } else { BEGIN {${^WARNING_BITS} = "\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} exec {$command[0];} @command or POSIX::_exit(127); } } } // 1
eval { do { open STDIN, "<", "File::Spec"->devnull or die; open STDOUT, ">&", $tmp->fh or die; open STDERR, ">", "File::Spec"->devnull or die; binmode STDOUT, ":raw"; if (ref $command[0] eq "CODE") { my $code = shift @command; &$code(@command); "STDOUT"->flush; 0; } else { BEGIN {${^WARNING_BITS} = "\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} exec {$command[0];} @command or POSIX::_exit(127); } } }
Truth table0%0/2
A
A= eval { do { open STDIN, "<", "File::Spec"->devnull or die; open STDOUT, ">&", $tmp->fh or die; open STDERR, ">", "File::Spec"->devnull or die; binmode STDOUT, ":raw"; if (ref $command[0] eq "CODE") { my $code = shift @command; &$code(@command); "STDOUT"->flush; 0; } else { BEGIN {${^WARNING_BITS} = "\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} exec {$command[0];} @command or POSIX::_exit(127); } } }
Aresult
11
00
123 31 ▶ open STDIN, '<', File::Spec->devnull or die;
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", "File::Spec"->devnull or die 31 0 31
124 31 ▶ open STDOUT, '>&', $tmp->fh or die;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $tmp->fh or die 31 0 31
125 31 ▶ open STDERR, '>', File::Spec->devnull or die;
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">", "File::Spec"->devnull or die 31 0 31
126 31 binmode STDOUT, ':raw';
127 31 ▶ if (ref $command[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command[0] eq 'CODE') { } 0 31 31
128 0 my $code = shift @command;
129 0 $code->(@command);
130 0 STDOUT->flush;
131 0 0;
132 } else {
133 # "or" keeps old perls from warning about a statement
134 # after exec
135 54 ▶ no warnings 'exec';
Subroutine BEGIN: called CC=1 SCAR=0.0
136 31 ▶ exec { $command[0] } @command
Condition0%0/3
exec({$command[0];} @command) || POSIX::_exit(127)
l!l&&r!l&&!r
000
MC/DC0%0/2
exec({$command[0];} @command) || POSIX::_exit(127)
exec({$command[0];} @command) POSIX::_exit(127)
Truth table0%0/3
A || B
A= exec({$command[0];} @command)B= POSIX::_exit(127)
ABresult
1X1
011
000
137 or POSIX::_exit(127);
138 }
139 } // 1;
140 0 POSIX::_exit($status);
141 }
142 63 waitpid $pid, 0;
143 63 ▶ return undef if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 7 56 63
144  
145 56 my $converted = $tmp->fh;
146 56 ▶ $converted->seek(0, 0) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$converted->seek(0, 0) or return undef 56 0 56
147 56 $converted;
148 }
149  
150 # Register the converters of IO::Factotum::NAME by their names
151 0 ▶sub load {
Subroutine load: not called CC=5 SCAR=34.0
Pod documented
152 0 for my $name (@_) {
153 0 ▶ $name =~ /\A\w+(?:::\w+)*\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A\w+(?:::\w+)*\z/u or croak("IO::Factotum: ${name}: invalid converter name") 0 0 0
154 or croak "IO::Factotum: $name: invalid converter name";
155 0 my $module = __PACKAGE__ . "::$name";
156 0 ▶ eval "require $module; 1"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $module; 1" or die "IO::Factotum: ${name}: no such converter\n$@" 0 0 0
157 or die "IO::Factotum: $name: no such converter\n$@";
158 0 ▶ my $setup = $module->can('setup')
Branch: 1 branch
BranchTrueFalseTotal
my $setup = $module->can("setup") or die "IO::Factotum: $module has no setup function\n" 0 0 0
159 or die "IO::Factotum: $module has no setup function\n";
160 0 $setup->();
161 }
162 0 return;
163 }
164  
165 31 ▶sub tie_argv {
Subroutine tie_argv: called CC=2 SCAR=6.9
Pod documented
166 31 ▶ return if tied *ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if tied *ARGV 2 29 31
167 29 require IO::Factotum::ARGV;
168 29 tie *ARGV, 'IO::Factotum::ARGV';
169 29 return;
170 }
171  
172 54 1;
173  
174 __END__
↑ Summary blib/lib/Getopt/EX/Factotum.pm »