blib/lib/Perl6/Slurp.pm

filter: Statementstmt 99.0% 101 / 102 Branchbran 88.8% 64 / 72 Conditioncond 80.9% 17 / 21 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 90.5% 201 / 222 CC 45 SCAR 36.1
CC
45
Cov
93%
slurp
45.9
__ANON__
1.0
BEGIN
1.0
CRAP 37.1
↑ Summary
1 package Perl6::Slurp;
2  
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 our $VERSION = '0.051005';
10  
11 # Exports only the slurp() sub...
12 13 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
13 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 *{caller().'::slurp'} = \&slurp;
15 }
16  
17 # Recognize mode arguments...
18 13 my $mode_pat = qr{
19 ^ \s* ( (?: < | \+< | \+>>? ) &? ) \s*
20 }x;
21  
22 # Recognize a mode followed by optional layer arguments...
23 13 my $mode_plus_layers = qr{
24 (?: $mode_pat | ^ \s* -\| \s* )
25 ( (?: :[^\W\d]\w* (?: \( .*? \) ?)? \s* )* )
26 \s*
27 \z
28 }x;
29  
30 # Is this a pure number???
31 26 ▶sub is_pure_num {
Subroutine is_pure_num: called CC=1 SCAR=0.0
Pod undocumented
32 26 return (~$_[0] & $_[0]) eq 0; # ~ acts differently for numbers and strings
33 }
34  
35 # The magic subroutine that does everything...
36 140 ▶sub slurp {
Subroutine slurp: called CC=45 SCAR=38.3
Pod undocumented
37 # Are we in a useful context???
38 140 my $list_context = wantarray;
39 140 ▶ croak "Useless use of &slurp in a void context"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list_context 1 139 140
40 unless defined $list_context;
41  
42 # Missing args default to $_, so we need to catch that early...
43 139 my $default = $_;
44  
45 # Remember any I/O layers and other options specified...
46 139 my @layers_or_options;
47  
48 # Process the argument list...
49 139 ▶ for (my $i=0; $i<@_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 247 139 386
50 # Ignore non-reference args...
51 247 ▶ my $type = ref $_[$i] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $_[$i] or next 139 108 247
52  
53 # Hashes indicate extra layers; remove from @_, add them in sequence...
54 139 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 48 91 139
elsif ($type eq 'ARRAY') { } 3 88 91
55 48 push @layers_or_options, splice @_, $i--, 1
56 }
57  
58 # Arrays also indicate extra layers; remove from @_, convert to hash
59 # form, and add them in sequence...
60 elsif ($type eq 'ARRAY') {
61 # Splice out the array and unpack it...
62 3 my @array = @{splice @_, $i--, 1};
63  
64 # Verify and convert each layer specified to a one-key hash...
65 3 ▶ while (@array) {
Branch: 1 branch
BranchTrueFalseTotal
if (@array) 6 3 9
66 6 my ($layer, $value) = splice @array, 0, 2;
67 6 ▶ croak "Incomplete layer specification for :$layer",
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 6 6
68 "\n(did you mean: $layer=>1)\n "
69 unless $value;
70 6 push @layers_or_options, { $layer=>$value };
71 }
72 }
73 }
74  
75 # Any remaining args are the read mode, source file, and whatever...
76 139 my ($mode, $source, @args) = @_;
77  
78 # If no arguments, use defaults...
79 139 ▶ if (!defined $mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mode) 6 133 139
80 6 ▶ $mode = defined $default ? $default
Branch: 2 branches
BranchTrueFalseTotal
defined $default ? : 5 1 6
@ARGV ? : 1 0 1
81 : @ARGV ? \*ARGV
82 : "<"
83 }
84  
85 # If mode was a reference, it must really have been the source...
86 139 ▶ if (ref $mode) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $mode) { } 53 86 139
elsif (not $mode =~ /$mode_plus_layers/x) { } 25 61 86
87 53 $source = $mode;
88 53 $mode = "<";
89 }
90  
91 # If mode isn't a valid mode, it must actually have been the source...
92 elsif ($mode !~ /$mode_plus_layers/x) {
93 25 $source = $mode;
94 25 ▶ $mode = $source =~ s/$mode_pat//x ? "$1"
Branch: 2 branches
BranchTrueFalseTotal
$source =~ s/$mode_pat//x ? : 8 17 25
$source =~ s/ \| \s* $//x ? : 1 16 17
95 : $source =~ s/ \| \s* $//x ? "-|"
96 : "<"
97 ;
98 }
99  
100 # Sources can be references, but only certain kinds of references...
101 139 my $ref = ref $source;
102 139 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 89 50 139
103 croak "Can't use $ref as a data source"
104 unless $ref eq 'SCALAR'
105 || $ref eq 'GLOB'
106 89 ▶ || eval { $source->isa('IO::Handle') };
Branch: 1 branch
BranchTrueFalseTotal
unless $ref eq "SCALAR" or $ref eq "GLOB" or eval { do { $source->isa("IO::Handle") } } 0 89 89
Condition83%5/6
$ref eq "SCALAR" or $ref eq "GLOB" or eval { do { $source->isa("IO::Handle") } }
l!l&&r!l&&!r
68210
$ref eq "SCALAR" or $ref eq "GLOB"
l!l&&r!l&&!r
462221
MC/DC0%0/3
$ref eq "SCALAR" or $ref eq "GLOB" or eval { do { $source->isa("IO::Handle") } }
$ref eq "SCALAR" $ref eq "GLOB" eval { do { $source->isa("IO::Handle") } }
Truth table75%3/4
A or B or C
A= $ref eq "SCALAR"B= $ref eq "GLOB"C= eval { do { $source->isa("IO::Handle") } }
ABCresult
1XX1
01X1
0011
0000
107 }
108  
109 # slurp() always uses \n as its input record separator (a la Perl 6)
110 139 local $/ = "\n";
111  
112 # This track the various options slurp() allows...
113 139 my ($chomp, $chomp_to, $layers) = (0, "", "");
114  
115 # Can this slurp be done in an optimized way (assume so initially)???
116 139 my $optimized = 1;
117  
118 # Decode the layers and options...
119 139 my $IRS = "\n";
120 139 for (@layers_or_options) {
121 # Input record separator...
122 54 ▶ if (exists $_->{irs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_->{"irs"}) 30 24 54
123 30 $IRS = $_->{irs};
124 30 ▶ $/ = $IRS if !ref($IRS);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $IRS 24 6 30
125 30 delete $_->{irs};
126 30 ▶ $optimized &&= !ref($IRS); # ...can't be optimized if irs is a regex
Condition66%2/3
$optimized &&= !ref($IRS)
!ll&&!rl&&r
0624
MC/DC50%1/2
$optimized &&= !ref($IRS)
$optimized !ref($IRS)
Truth table66%2/3
A &&= not(B)
A= $optimizedB= !ref($IRS)
ABresult
0X0
100
111
127 }
128  
129 # Autochomp...
130 54 ▶ if (exists $_->{chomp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_->{"chomp"}) 26 28 54
131 26 $chomp = 1;
132  
133 # If the chomp value is a string, that becomes to replacement...
134 26 ▶ if (defined $_->{chomp} && !is_pure_num($_->{chomp})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->{"chomp"} and not is_pure_num($_->{"chomp"})) 14 12 26
Condition66%2/3
defined $_->{"chomp"} and not is_pure_num($_->{"chomp"})
!ll&&!rl&&r
01214
MC/DC50%1/2
defined $_->{"chomp"} and not is_pure_num($_->{"chomp"})
defined $_->{"chomp"} not is_pure_num($_->{"chomp"})
Truth table66%2/3
A and not(B)
A= defined $_->{"chomp"}B= not is_pure_num($_->{"chomp"})
ABresult
0X0
100
111
135 $chomp_to = $_->{chomp}
136 14 }
137 26 delete $_->{chomp};
138 26 $optimized = 0; # ...chomped slurps can't be optimized
139 }
140  
141 # Any other entries are layers...
142 54 $layers .= join " ", map ":$_", keys %$_;
143 }
144  
145 # Add any layers found to the mode specification...
146 139 $mode .= " $layers";
147  
148 # Open the source as a filehandle...
149 139 my $FH;
150  
151 # Source is a typeglob...
152 139 ▶ if ($ref && $ref ne 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref and $ref ne 'SCALAR') { } 43 96 139
elsif (not $source) { } 0 96 96
Condition100%3/3
$ref and $ref ne "SCALAR"
!ll&&!rl&&r
504643
MC/DC100%2/2
$ref and $ref ne "SCALAR"
$ref $ref ne "SCALAR"
Truth table100%3/3
A and B
A= $refB= $ref ne "SCALAR"
ABresult
0X0
100
111
153 43 $FH = $source;
154 }
155  
156 # No source, specified: use *ARGV...
157 elsif (!$source) {
158 13 ▶ no warnings 'io';
Subroutine BEGIN: called CC=1 SCAR=0.0
159 0 ▶ open $FH, '<-'
Branch: 1 branch
BranchTrueFalseTotal
open $FH, "<-" or croak("Can't open stdin: $!") 0 0 0
160 or croak "Can't open stdin: $!";
161 }
162  
163 # Source specified: open it...
164 else {
165 13 ▶ no warnings 'io';
Subroutine BEGIN: called CC=1 SCAR=0.0
166 96 ▶ open $FH, $mode, $source, @args
Branch: 1 branch
BranchTrueFalseTotal
open $FH, $mode, $source, @args or croak("Can't open '${source}': $!") 94 2 96
167 or croak "Can't open '$source': $!";
168 }
169  
170 # Standardize chomp-converter sub...
171 137 ▶ my $chomp_into = ref $chomp_to eq 'CODE' ? $chomp_to : sub{ $chomp_to };
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
ref $chomp_to eq 'CODE' ? : 0 137 137
172  
173 # Optimized slurp if possible in list context...
174 137 ▶ if ($list_context && $optimized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_context and $optimized) 36 101 137
Condition100%3/3
$list_context and $optimized
!ll&&!rl&&r
871436
MC/DC100%2/2
$list_context and $optimized
$list_context $optimized
Truth table100%3/3
A and B
A= $list_contextB= $optimized
ABresult
0X0
100
111
175 36 return <$FH>;
176 }
177  
178 # Acquire data (working around bug between $/ and in magic ARGV)...
179 101 ▶ my $data = refaddr($FH) == \*ARGV ? join("",<>) : do { local $/; <$FH> };
Branch: 1 branch
BranchTrueFalseTotal
builtin::refaddr($FH) == \*ARGV ? : 1 100 101
180  
181 # Prepare input record separator regex...
182 101 ▶ my $irs = ref($IRS) ? $IRS
Branch: 2 branches
BranchTrueFalseTotal
ref $IRS ? : 6 95 101
defined $IRS ? : 90 5 95
183 : defined($IRS) ? qr{\Q$IRS\E}
184 : qr{(?!)};
185  
186 # List context may require input record separator processing...
187 101 ▶ if ($list_context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_context) { } 14 87 101
188 # No data --> nothing to return...
189 14 ▶ return () unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 14 14
190  
191 # Split acquired data into lines according to IRS...
192 14 my @components = split /($irs)/, $data;
193 14 my @lines;
194 14 ▶ while (@components) {
Branch: 1 branch
BranchTrueFalseTotal
if (@components) 56 14 70
195 # Extract the next line and separator...
196 56 my ($line, $sep) = splice @components, 0, 2;
197  
198 # Add the line...
199 56 push @lines, $line;
200  
201 # Chomp as requested...
202 56 ▶ if (defined $sep && length $sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sep and length $sep) 50 6 56
Condition66%2/3
defined $sep and length $sep
!ll&&!rl&&r
6050
MC/DC50%1/2
defined $sep and length $sep
defined $sep length $sep
Truth table66%2/3
A and B
A= defined $sepB= length $sep
ABresult
0X0
100
111
203 50 ▶ $lines[-1] .= $chomp ? $chomp_into->($sep) : $sep;
Branch: 1 branch
BranchTrueFalseTotal
$chomp ? : 45 5 50
204 }
205 }
206 14 return @lines;
207 }
208  
209 # Scalar context...
210 else {
211 # No data --> nothing to return...
212 87 ▶ return q{} unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 9 78 87
213  
214 # Otherwise, do any requested chomp-conversion...
215 78 ▶ if ($chomp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chomp) 13 65 78
216 13 $data =~ s{($irs)}{$chomp_into->($1)}ge;
217 }
218  
219 78 return $data;
220 }
221 }
222  
223 13 1;
224 __END__
↑ Summary