lib/IO/Factotum.pm

filter: Statementstmt 85.5% 77 / 90 Branchbran 58.8% 20 / 34 Conditioncond 50.0% 12 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 93.7% 15 / 16 Podpod 71.4% 5 / 7 totaltotal 70.8% 131 / 185 CC 30 SCAR 23.1
CC
30
Cov
74%
load
30.0
convert
14.7
register
11.0
CRAP 10.1
« blib/lib/IO/Factotum/sourcehighlight.pm ↑ Summary blib/lib/Getopt/EX/Factotum.pm »
1 package IO::Factotum;
2  
3 52 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 52 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 52 our $VERSION = "0.03";
7  
8 52 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 52 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 52 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 52 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 52 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 52 ▶use Command::Run::Tmpfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 52 my @converter;
16  
17 80 ▶sub register {
Subroutine register: called CC=8 SCAR=24.0
Pod documented
18 80 my %arg = @_;
19 80 ▶ my $match = delete $arg{match} // croak "register: match is required";
Condition50%1/2
delete $arg{"match"} // croak("register: match is required")
l!l
800
MC/DC0%0/1
delete $arg{"match"} // croak("register: match is required")
delete $arg{"match"}
Truth table50%1/2
A
A= delete $arg{"match"}
Aresult
11
00
20 80 ▶ my $command = delete $arg{command} // croak "register: command is required";
Condition50%1/2
delete $arg{"command"} // croak("register: command is required")
l!l
800
MC/DC0%0/1
delete $arg{"command"} // croak("register: command is required")
delete $arg{"command"}
Truth table50%1/2
A
A= delete $arg{"command"}
Aresult
11
00
21 0 ▶ croak "register: unknown parameter: @{[ sort keys %arg ]}" if %arg;
Branch: 1 branch
BranchTrueFalseTotal
if %arg 0 80 80
22 80 ▶ ref $match eq 'Regexp' or ref $match eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $match eq "Regexp" or ref $match eq "CODE" or croak("register: match must be a regex or a code reference") 80 0 80
Condition66%2/3
ref $match eq "Regexp" or ref $match eq "CODE"
l!l&&r!l&&!r
7550
MC/DC0%0/2
ref $match eq "Regexp" or ref $match eq "CODE"
ref $match eq "Regexp" ref $match eq "CODE"
Truth table66%2/3
A or B
A= ref $match eq "Regexp"B= ref $match eq "CODE"
ABresult
1X1
011
000
23 or croak "register: match must be a regex or a code reference";
24 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
25 or croak "register: command must be an array reference";
26 80 push @converter, { match => $match, command => [ @$command ] };
27 80 return;
28 }
29  
30 36 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
31 36 @converter = ();
32 36 return;
33 }
34  
35 106 ▶sub converter {
Subroutine converter: called CC=4 SCAR=13.9
Pod undocumented
36 106 my $file = shift;
37 106 for my $c (@converter) {
38 101 my $match = $c->{match};
39 101 ▶ if (ref $match eq 'CODE' ? $match->($file) : $file =~ $match) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $match eq "CODE" ? &$match($file) : $file =~ /$match/u) 92 9 101
ref $match eq 'CODE' ? : 6 95 101
40 92 return $c;
41 }
42 }
43 14 undef;
44 }
45  
46 115 ▶sub open_input {
Subroutine open_input: called CC=5 SCAR=16.1
Pod documented
47 115 my $file = shift;
48  
49 # The standard input is read as it is, by STDIN itself: another
50 # handle on it would not see what STDIN has read ahead
51 115 ▶ return *STDIN{IO} if $file eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "-" 3 112 115
52  
53 # The file is opened first: a file which can not be read is an
54 # error of the caller with $! set, whatever the converter does
55 112 ▶ my $fh = IO::File->new($file, '<') // return undef;
Condition100%2/2
"IO::File"->new($file, "<") // (return undef)
l!l
1066
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
56 106 binmode $fh, ':raw';
57  
58 106 ▶ my $c = converter($file) // return $fh;
Condition100%2/2
converter($file) // (return $fh)
l!l
9214
MC/DC100%1/1
converter($file) // (return $fh)
converter($file)
Truth table100%2/2
A
A= converter($file)
Aresult
11
00
59 # a converter which fails leaves the file as it is
60 92 ▶ convert($c->{command}, $file) // $fh;
Condition66%2/3
convert($c->{"command"}, $file) // $fh
l!l&&r!l&&!r
5570
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
61 }
62  
63 # Run the command with the file name, in a process of its own, and
64 # return the handle of a temporary file which has what it wrote to the
65 # standard output, or undef when it fails. The output goes to the file
66 # as it is, byte for byte. The standard error is thrown away.
67 92 ▶sub convert {
Subroutine convert: called CC=11 SCAR=26.9
Pod undocumented
68 92 my($command, $file) = @_;
69 92 my @command = (@$command, $file);
70 92 my $tmp = Command::Run::Tmpfile->new(raw => 1);
71  
72 # $? and $! of the caller are kept, and the child is waited for
73 # even when the caller ignores SIGCHLD
74 92 local($?, $!);
75 92 local $SIG{CHLD} = 'DEFAULT';
76 # what is in the buffers is not to be written twice
77 92 STDOUT->flush;
78 92 STDERR->flush;
79  
80 92 ▶ my $pid = fork // return undef;
Condition50%1/2
fork // (return undef)
l!l
920
MC/DC0%0/1
fork // (return undef)
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
81 92 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 30 62 92
82 # The child never returns to the program: it ends by _exit, so
83 # that the END blocks and the destructors of the program do not
84 # run here, even when open_input is called inside eval.
85 30 ▶ 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
86 30 ▶ open STDIN, '<', File::Spec->devnull or die;
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", "File::Spec"->devnull or die 30 0 30
87 30 ▶ open STDOUT, '>&', $tmp->fh or die;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $tmp->fh or die 30 0 30
88 30 ▶ open STDERR, '>', File::Spec->devnull or die;
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">", "File::Spec"->devnull or die 30 0 30
89 30 binmode STDOUT, ':raw';
90 30 ▶ if (ref $command[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command[0] eq 'CODE') { } 0 30 30
91 0 my $code = shift @command;
92 0 $code->(@command);
93 0 STDOUT->flush;
94 0 0;
95 } else {
96 # "or" keeps old perls from warning about a statement
97 # after exec
98 52 ▶ no warnings 'exec';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 30 ▶ 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
100 or POSIX::_exit(127);
101 }
102 } // 1;
103 0 POSIX::_exit($status);
104 }
105 62 waitpid $pid, 0;
106 62 ▶ return undef if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 7 55 62
107  
108 55 my $converted = $tmp->fh;
109 55 ▶ $converted->seek(0, 0) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$converted->seek(0, 0) or return undef 55 0 55
110 55 $converted;
111 }
112  
113 # Register the converters of IO::Factotum::NAME by their names
114 0 ▶sub load {
Subroutine load: not called CC=5 SCAR=34.0
Pod documented
115 0 for my $name (@_) {
116 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
117 or croak "IO::Factotum: $name: invalid converter name";
118 0 my $module = __PACKAGE__ . "::$name";
119 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
120 or die "IO::Factotum: $name: no such converter\n$@";
121 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
122 or die "IO::Factotum: $module has no setup function\n";
123 0 $setup->();
124 }
125 0 return;
126 }
127  
128 33 ▶sub tie_argv {
Subroutine tie_argv: called CC=2 SCAR=6.9
Pod documented
129 33 ▶ return if tied *ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if tied *ARGV 3 30 33
130 30 require IO::Factotum::ARGV;
131 30 tie *ARGV, 'IO::Factotum::ARGV';
132 30 return;
133 }
134  
135 52 1;
136  
137 __END__
« blib/lib/IO/Factotum/sourcehighlight.pm ↑ Summary blib/lib/Getopt/EX/Factotum.pm »