blib/lib/File/Open.pm

filter: Statementstmt 100% 76 / 76 Branchbran 83.3% 70 / 84 Conditioncond 18.1% 4 / 22 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 100% 19 / 19 Podpod 83.3% 5 / 6 totaltotal 80.2% 175 / 218 CC 55 SCAR 37.6
CC
55
Cov
82%
_sysopen
83.8
_open
12.0
_opendir
6.0
CRAP 43.0
↑ Summary
1 package File::Open;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶BEGIN { warnings->import(FATAL => 'layer') if $] >= 5.008; }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008" 7 0 7
6  
7 7 our $VERSION = '1.0102';
8  
9 7 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Exporter (); BEGIN { our @ISA = 'Exporter'; }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 our @EXPORT_OK = qw(
16 fopen fopen_nothrow
17 fsysopen fsysopen_nothrow
18 fopendir fopendir_nothrow
19 );
20  
21 sub _mode {
22 42 ▶ map { $_ => $_[0] } @_
Subroutine _mode: called CC=1 SCAR=0.0
23 }
24  
25 7 my %modemap = (
26 _mode(qw[< r ]),
27 _mode(qw[> w ]),
28 _mode(qw[>> a ]),
29 _mode(qw[+< r+]),
30 _mode(qw[+> w+]),
31 _mode(qw[+>> a+]),
32 );
33  
34 sub _open {
35 112 ▶ my ($func, $file, $mode, $layers) = @_;
Subroutine _open: called CC=12 SCAR=24.9
36 112 ▶ @_ < 2 and croak "Not enough arguments for $func";
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 and croak("Not enough arguments for $func") 2 110 112
37 110 ▶ @_ > 4 and croak "Too many arguments for $func";
Branch: 1 branch
BranchTrueFalseTotal
@_ > 4 and croak("Too many arguments for $func") 2 108 110
38 108 ▶ $mode = '<' if !defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 11 97 108
39  
40 108 my $binary = $mode =~ s/(?<=.)b//;
41 108 ▶ my $emode = $modemap{$mode} or croak "Unknown $func() mode '$mode'";
Branch: 1 branch
BranchTrueFalseTotal
my $emode = $modemap{$mode} or croak("Unknown $func() mode '${mode}'") 28 80 108
42  
43 80 ▶ if ($file =~ /\0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\0/) 2 78 80
44 2 ▶ $! = Errno::ENOENT() if exists &Errno::ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if exists &Errno::ENOENT 2 0 2
45 2 return undef;
46 }
47  
48 78 ▶ unless (defined $layers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $layers) 76 2 78
49 # grab our caller's 'use open' settings
50 76 my $hints = (caller 1)[10];
51 76 ▶ my $key = $emode =~ />/ ? 'open>' : 'open<';
Branch: 1 branch
BranchTrueFalseTotal
$emode =~ />/ ? : 25 51 76
52 76 $layers = $hints->{$key};
53 }
54  
55 78 ▶ open my $fh, $emode . (defined $layers ? " $layers" : ''), $file or return undef;
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, $emode . (defined $layers ? " $layers" : ""), $file or return undef 36 42 78
defined $layers ? : 7 71 78
56 42 ▶ binmode $fh if $binary;
Branch: 1 branch
BranchTrueFalseTotal
if $binary 21 21 42
57 42 $fh
58 }
59  
60 sub fopen_nothrow {
61 51 ▶ _open('fopen_nothrow', @_)
Subroutine fopen_nothrow: called CC=1 SCAR=0.0
Pod documented
62 }
63  
64 7 my $prog = basename $0;
65  
66 sub fopen {
67 61 ▶ _open('fopen', @_) || die "$prog: $_[0]: $!\n"
Subroutine fopen: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_open("fopen", @_) or die "${prog}: $_[0]: $!\n" 19 26 45
68 }
69  
70 sub _sysopen {
71 84 ▶ my ($func, $file, $mode, $flags) = @_;
Subroutine _sysopen: called CC=35 SCAR=44.3
72 84 ▶ @_ < 3 and croak "Not enough arguments for $func";
Branch: 1 branch
BranchTrueFalseTotal
@_ < 3 and croak("Not enough arguments for $func") 4 80 84
73 80 ▶ @_ > 4 and croak "Too many arguments for $func";
Branch: 1 branch
BranchTrueFalseTotal
@_ > 4 and croak("Too many arguments for $func") 2 78 80
74  
75 78 ▶ my $emode =
Branch: 3 branches
BranchTrueFalseTotal
$mode eq 'r' ? : 28 50 78
$mode eq 'w' ? : 24 26 50
$mode eq 'rw' ? : 4 22 26
76 $mode eq 'r' ? Fcntl::O_RDONLY() :
77 $mode eq 'w' ? Fcntl::O_WRONLY() :
78 $mode eq 'rw' ? Fcntl::O_RDWR() :
79 croak "Unknown $func() mode '$mode'"
80 ;
81 56 ▶ $flags = {} if !defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 22 34 56
82  
83 56 my $perms = 0;
84  
85 56 for my $k (keys %$flags) {
86 60 my $v = !!$flags->{$k};
87 $emode |=
88 $k eq 'creat' ?
89 60 ▶ defined $flags->{$k} ? do {
Branch: 2 branches
BranchTrueFalseTotal
$k eq 'creat' ? : 16 44 60
defined $flags->{$k} ? : 16 0 16
90 16 $perms = $flags->{$k};
91 16 ▶ Fcntl::O_CREAT()
Branch: 11 branches
BranchTrueFalseTotal
$k eq 'append' ? : 7 37 44
$k eq 'async' ? : 0 37 37
$k eq 'direct' ? : 0 37 37
$k eq 'directory' ? : 0 37 37
$k eq 'excl' ? : 11 26 37
$k eq 'noatime' ? : 0 26 26
$k eq 'noctty' ? : 0 26 26
$k eq 'nofollow' ? : 0 26 26
$k eq 'nonblock' ? : 0 26 26
$k eq 'sync' ? : 0 26 26
$k eq 'trunc' ? : 8 18 26
Condition18%4/22
$v and 1024
!ll
07
$v and 8192
!ll
00
$v and 16384
!ll
00
$v and 65536
!ll
00
$v and 128
!ll
110
$v and 262144
!ll
00
$v and 256
!ll
00
$v and 131072
!ll
00
$v and 2048
!ll
00
$v and 1052672
!ll
00
$v and 512
!ll
08
MC/DC0%0/1
$v and 1024
$v
MC/DC0%0/1
$v and 8192
$v
MC/DC0%0/1
$v and 16384
$v
MC/DC0%0/1
$v and 65536
$v
MC/DC100%1/1
$v and 128
$v
MC/DC0%0/1
$v and 262144
$v
MC/DC0%0/1
$v and 256
$v
MC/DC0%0/1
$v and 131072
$v
MC/DC0%0/1
$v and 2048
$v
MC/DC0%0/1
$v and 1052672
$v
MC/DC0%0/1
$v and 512
$v
Truth table50%1/2
A
A= $v
Aresult
00
11
Truth table0%0/2
A
A= $v
Aresult
00
11
Truth table0%0/2
A
A= $v
Aresult
00
11
Truth table0%0/2
A
A= $v
Aresult
00
11
Truth table100%2/2
A
A= $v
Aresult
00
11
Truth table0%0/2
A
A= $v
Aresult
00
11
Truth table0%0/2
A
A= $v
Aresult
00
11
Truth table0%0/2
A
A= $v
Aresult
00
11
Truth table0%0/2
A
A= $v
Aresult
00
11
Truth table0%0/2
A
A= $v
Aresult
00
11
Truth table50%1/2
A
A= $v
Aresult
00
11
92 } :
93 0
94 :
95 $k eq 'append' ? $v && Fcntl::O_APPEND() :
96 $k eq 'async' ? $v && Fcntl::O_ASYNC() :
97 $k eq 'direct' ? $v && Fcntl::O_DIRECT() :
98 $k eq 'directory' ? $v && Fcntl::O_DIRECTORY() :
99 $k eq 'excl' ? $v && Fcntl::O_EXCL() :
100 $k eq 'noatime' ? $v && Fcntl::O_NOATIME() :
101 $k eq 'noctty' ? $v && Fcntl::O_NOCTTY() :
102 $k eq 'nofollow' ? $v && Fcntl::O_NOFOLLOW() :
103 $k eq 'nonblock' ? $v && Fcntl::O_NONBLOCK() :
104 $k eq 'sync' ? $v && Fcntl::O_SYNC() :
105 $k eq 'trunc' ? $v && Fcntl::O_TRUNC() :
106 croak "Unknown $func() flag '$k'"
107 ;
108 }
109  
110 38 ▶ if ($file =~ /\0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\0/) 2 36 38
111 2 ▶ $! = Errno::ENOENT() if exists &Errno::ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if exists &Errno::ENOENT 2 0 2
112 2 return undef;
113 }
114  
115 36 ▶ sysopen my $fh, $file, $emode, $perms or return undef;
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $file, $emode, $perms or return undef 16 20 36
116 20 $fh
117 }
118  
119 sub fsysopen_nothrow {
120 42 ▶ _sysopen('fsysopen_nothrow', @_)
Subroutine fsysopen_nothrow: called CC=1 SCAR=0.0
Pod documented
121 }
122  
123 sub fsysopen {
124 42 ▶ _sysopen('fsysopen', @_) || die "$prog: $_[0]: $!\n"
Subroutine fsysopen: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_sysopen("fsysopen", @_) or die "${prog}: $_[0]: $!\n" 9 10 19
125 }
126  
127 sub _opendir {
128 8 ▶ my ($func, $dir) = @_;
Subroutine _opendir: called CC=6 SCAR=18.0
129 8 ▶ @_ < 2 and croak "Not enough arguments for $func";
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 and croak("Not enough arguments for $func") 2 6 8
130 6 ▶ @_ > 2 and croak "Too many arguments for $func";
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 and croak("Too many arguments for $func") 2 4 6
131  
132 4 ▶ if ($dir =~ /\0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /\0/) 2 2 4
133 2 ▶ $! = Errno::ENOENT() if exists &Errno::ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if exists &Errno::ENOENT 2 0 2
134 2 return undef;
135 }
136  
137 2 ▶ opendir my $dh, $dir or return undef;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return undef 0 2 2
138 2 $dh
139 }
140  
141 sub fopendir_nothrow {
142 3 ▶ _opendir('fopendir_nothrow', @_)
Subroutine fopendir_nothrow: called CC=1 SCAR=0.0
Pod undocumented
143 }
144  
145 sub fopendir {
146 5 ▶ _opendir('fopendir', @_) || die "$prog: $_[0]: $!\n"
Subroutine fopendir: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_opendir("fopendir", @_) or die "${prog}: $_[0]: $!\n" 1 2 3
147 }
148  
149 7 'ok'
150  
151 __END__
↑ Summary