| 1 | package IO::Any; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '0.10'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Carp 'confess'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use IO::String; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use IO::AtomicFile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Fcntl qw(:flock); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use List::Util qw(none any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 31 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=23 SCAR=32.4
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 31 | my $class = shift; | |||||||||||||||||||||||||||
| 20 | 31 | my $what = shift; | |||||||||||||||||||||||||||
| 21 | 31 | ▶ | my $how = shift || '<'; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || '<'
MC/DC0%0/1
shift() || '<'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 22 | 31 | ▶ | my $opt = shift || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 23 | 31 | ▶ | confess 'too many arguments' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | if @_; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 31 | ▶ | confess '$what is missing' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | if not defined $what; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 29 | ▶ | confess 'expecting hash ref' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if ref $opt ne 'HASH'; | ||||||||||||||||||||||||||||
| 31 | 29 | foreach my $key (keys %$opt) { | |||||||||||||||||||||||||||
| 32 | confess 'unknown option '.$key | ||||||||||||||||||||||||||||
| 33 | 25 | ▶ | if (none { $key eq $_ } qw(atomic LOCK_SH LOCK_EX LOCK_NB BINMODE)); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 27 | my ($type, $proper_what) = $class->_guess_what($what); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 27 | ▶ | if ($type eq 'string') { return IO::String->new($proper_what) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 24 | ▶ | if ($type eq 'file') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 23 | ▶ | my $fh = $opt->{'atomic'} ? IO::AtomicFile->new() : IO::File->new(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 23 | ▶ | $fh->open($proper_what, $how) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | or confess 'error opening file "'.$proper_what.'" - '.$!; | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | if (defined $opt->{BINMODE} and not binmode($fh, $opt->{BINMODE})) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
Condition100%3/3
defined $opt->{"BINMODE"} and not binmode($fh, $opt->{"BINMODE"})
MC/DC100%2/2
defined $opt->{"BINMODE"} and not binmode($fh, $opt->{"BINMODE"})
defined $opt->{"BINMODE"}
not binmode($fh, $opt->{"BINMODE"})
Truth table100%3/3
A and not(B)
A= defined $opt->{"BINMODE"}B= not binmode($fh, $opt->{"BINMODE"})
| |||||||||||||||||||||||||||||
| 45 | 1 | _discard_atomic($fh); | |||||||||||||||||||||||||||
| 46 | 1 | confess 'binmode('.$opt->{BINMODE}.') failed'; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # locking if requested | ||||||||||||||||||||||||||||
| 50 | 22 | ▶ | if ($opt->{'LOCK_SH'} or $opt->{'LOCK_EX'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opt->{"LOCK_SH"} or $opt->{"LOCK_EX"}
MC/DC100%2/2
$opt->{"LOCK_SH"} or $opt->{"LOCK_EX"}
$opt->{"LOCK_SH"}
$opt->{"LOCK_EX"}
Truth table100%3/3
A or B
A= $opt->{"LOCK_SH"}B= $opt->{"LOCK_EX"}
| |||||||||||||||||||||||||||||
| 51 | 9 | ▶ | if (not flock($fh, | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 52 | ($opt->{'LOCK_SH'} ? LOCK_SH : 0) | ||||||||||||||||||||||||||||
| 53 | | ($opt->{'LOCK_EX'} ? LOCK_EX : 0) | ||||||||||||||||||||||||||||
| 54 | | ($opt->{'LOCK_NB'} ? LOCK_NB : 0) | ||||||||||||||||||||||||||||
| 55 | )) { | ||||||||||||||||||||||||||||
| 56 | 5 | my $error = $!; | |||||||||||||||||||||||||||
| 57 | 5 | _discard_atomic($fh); | |||||||||||||||||||||||||||
| 58 | 5 | confess 'flock failed - '.$error; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 17 | return $fh; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 1 | ▶ | if ($type eq 'iofile') { return $proper_what } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($type eq 'iostring') { return $proper_what } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ($type eq 'http') { die 'no http support yet :-|' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 39 | ▶ | sub _guess_what { | ||||||||||||||||||||||||||
|
Subroutine
_guess_what: called CC=18 SCAR=29.5
| |||||||||||||||||||||||||||||
| 70 | 39 | my $class = shift; | |||||||||||||||||||||||||||
| 71 | 39 | my $what = shift; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 39 | ▶ | if (!blessed($what)) { } # not blessed, do nothing | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 74 | 0 | elsif ($what->isa('Path::Class::File')) { $what = $what->stringify } | |||||||||||||||||||||||||||
| 75 | 1 | ▶ | elsif (any { $what->isa($_) } qw(IO::File IO::AtomicFile IO::Uncompress::Bunzip2)) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | confess 'passed unopened IO::File' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | if not $what->opened; | ||||||||||||||||||||||||||||
| 78 | 1 | return ('iofile', $what); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 0 | elsif ($what->isa('IO::String')) { return ('iostring', $what) } | |||||||||||||||||||||||||||
| 81 | 0 | else { confess 'no support for '.blessed($what) }; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 38 | my $ref_what = ref($what); | |||||||||||||||||||||||||||
| 84 | 38 | ▶ | if ($ref_what eq 'ARRAY') { return ('file', File::Spec->catfile(@{$what})) } | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 85 | 1 | elsif ($ref_what eq 'SCALAR') { return ('string', $what) } | |||||||||||||||||||||||||||
| 86 | elsif ($ref_what eq '') {} # do nothing here if not reference | ||||||||||||||||||||||||||||
| 87 | 0 | else { confess 'no support for ref '.(ref $what) } | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # check for typeglobs | ||||||||||||||||||||||||||||
| 90 | 14 | ▶ | if ((ref \$what eq 'GLOB') and (my $fh = *{$what}{IO})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref \$what eq "GLOB" and my $fh = *{$what;}{"IO"}
MC/DC100%1/1
ref \$what eq "GLOB" and my $fh = *{$what;}{"IO"}
ref \$what eq "GLOB"
Truth table100%2/2
A
A= ref \$what eq "GLOB"
| |||||||||||||||||||||||||||||
| 91 | 1 | return ('iofile', $fh); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 13 | ▶ | if ($what =~ m{^file://(.+)$}) { return ('file', $1) } # local file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 12 | ▶ | if ($what =~ m{^https?://}) { return ('http', $what) } # http link | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 10 | ▶ | if ($what =~ m{^<}) { return ('string', $what) } # xml string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 9 | ▶ | if ($what =~ m(^{)) { return ('string', $what) } # json string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 7 | ▶ | if ($what =~ m{^\[}) { return ('string', $what) } # json string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 6 | ▶ | if ($what =~ m{\n[\s\w]}) { return ('string', $what) } # multi-line string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 5 | ▶ | if ($what eq '') { return ('string', '') } # empty string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 3 | { return ('file', $what) } # default is filename | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 12 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 12 | my $class = shift; | |||||||||||||||||||||||||||
| 106 | 12 | my $what = shift; | |||||||||||||||||||||||||||
| 107 | 12 | my $opt = shift; | |||||||||||||||||||||||||||
| 108 | 12 | ▶ | confess 'too many arguments' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | if @_; | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 12 | return $class->new($what, '<', $opt); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 116 | 2 | my $what = shift; | |||||||||||||||||||||||||||
| 117 | 2 | my $opt = shift; | |||||||||||||||||||||||||||
| 118 | 2 | ▶ | confess 'too many arguments' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | if @_; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | return $class->new($what, '>', $opt); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 6 | ▶ | sub slurp { | ||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 126 | 6 | my $what = shift; | |||||||||||||||||||||||||||
| 127 | 6 | my $opt = shift; | |||||||||||||||||||||||||||
| 128 | 6 | ▶ | confess 'too many arguments' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | if @_; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 6 | my $fh = $class->read($what, $opt); | |||||||||||||||||||||||||||
| 132 | 6 | my $content = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 133 | 6 | close $fh; | |||||||||||||||||||||||||||
| 134 | 6 | return $content; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 11 | ▶ | sub spew { | ||||||||||||||||||||||||||
|
Subroutine
spew: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 11 | my $class = shift; | |||||||||||||||||||||||||||
| 139 | 11 | my $what = shift; | |||||||||||||||||||||||||||
| 140 | 11 | my $data = shift; | |||||||||||||||||||||||||||
| 141 | 11 | my $opt = shift; | |||||||||||||||||||||||||||
| 142 | 11 | ▶ | confess 'too many arguments' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | if @_; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # "parade" to allow safe locking | ||||||||||||||||||||||||||||
| 146 | 11 | my $fh = $class->new($what, '+>>', $opt); | |||||||||||||||||||||||||||
| 147 | 8 | $fh->seek(0,0); | |||||||||||||||||||||||||||
| 148 | 8 | $fh->truncate(0); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 8 | ▶ | if (not print $fh $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | my $error = $!; | |||||||||||||||||||||||||||
| 152 | 0 | _discard_atomic($fh); | |||||||||||||||||||||||||||
| 153 | 0 | confess 'failed to write file - '.$error; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 8 | ▶ | if (not $fh->close) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 1 | my $error = $!; | |||||||||||||||||||||||||||
| 157 | 1 | _discard_atomic($fh); | |||||||||||||||||||||||||||
| 158 | 1 | confess 'failed to close file - '.$error; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 7 | return; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # remove the temporary file of a failed IO::AtomicFile write, the target stays intact | ||||||||||||||||||||||||||||
| 164 | 7 | ▶ | sub _discard_atomic { | ||||||||||||||||||||||||||
|
Subroutine
_discard_atomic: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 165 | 7 | my $fh = shift; | |||||||||||||||||||||||||||
| 166 | 7 | ▶ | return if not blessed($fh) or not $fh->isa('IO::AtomicFile'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($fh) and $fh->isa("IO::AtomicFile")
MC/DC50%1/2
builtin::blessed($fh) and $fh->isa("IO::AtomicFile")
builtin::blessed($fh)
$fh->isa("IO::AtomicFile")
Truth table66%2/3
A and B
A= builtin::blessed($fh)B= $fh->isa("IO::AtomicFile")
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # delete() is a no-op once close() has run (also a failed one) | ||||||||||||||||||||||||||||
| 169 | 3 | my $temp = ${*$fh}{'io_atomicfile_temp'}; | |||||||||||||||||||||||||||
| 170 | 3 | $fh->delete; | |||||||||||||||||||||||||||
| 171 | 3 | ▶ | unlink($temp) if defined $temp and -e $temp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $temp and -e $temp
MC/DC50%1/2
defined $temp and -e $temp
defined $temp
-e $temp
Truth table66%2/3
A and B
A= defined $tempB= -e $temp
| |||||||||||||||||||||||||||||
| 172 | 3 | return; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | 1; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||