ScalarArray.pm

filter: Statementstmt 83.5% 137 / 164 Branchbran 74.1% 46 / 62 Conditioncond 55.0% 11 / 20 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 62.7% 27 / 43 Podpod 100% 21 / 21 totaltotal 76.1% 246 / 323 CC 38 SCAR 12.6
CC
38
Cov
79%
getline
9.4
read
6.0
write
6.0
CRAP 3.5
« Lines.pm ↑ Summary Wrap.pm »
1 package IO::ScalarArray;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # The package version, both in 1.23 style *and* usable by MakeMaker:
8 3 our $VERSION = '2.113';
9  
10 # Inheritance:
11 3 our @ISA = qw(IO::Handle);
12 3 ▶require IO::WrapTie and push @ISA, 'IO::WrapTie::Slave' if ($] >= 5.004);
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.004" 3 0 3
Condition50%2/4
$] >= 5.004 && (require IO::WrapTie && push(@IO::ScalarArray::ISA, 'IO::WrapTie::Slave'))
!ll
03
require IO::WrapTie and push @IO::ScalarArray::ISA, "IO::WrapTie::Slave"
!ll
03
MC/DC0%0/3
$] >= 5.004 && (require IO::WrapTie && push(@IO::ScalarArray::ISA, 'IO::WrapTie::Slave'))
$] >= 5.004 require IO::WrapTie push @IO::ScalarArray::ISA, "IO::WrapTie::Slave"
Truth table0%0/4
A && B and C
A= $] >= 5.004B= require IO::WrapTieC= push @IO::ScalarArray::ISA, "IO::WrapTie::Slave"
ABCresult
0XX0
10X0
1100
1111
13  
14 =head1 NAME
15  
16 IO::ScalarArray - IO:: interface for reading/writing an array of scalars
17  
18  
19 =head1 SYNOPSIS
20  
21 Perform I/O on strings, using the basic OO interface...
22  
23 use IO::ScalarArray;
24 @data = ("My mes", "sage:\n");
25  
26 ### Open a handle on an array, and append to it:
27 $AH = new IO::ScalarArray \@data;
28 $AH->print("Hello");
29 $AH->print(", world!\nBye now!\n");
30 print "The array is now: ", @data, "\n";
31  
32 ### Open a handle on an array, read it line-by-line, then close it:
33 $AH = new IO::ScalarArray \@data;
34 while (defined($_ = $AH->getline)) {
35 print "Got line: $_";
36 }
37 $AH->close;
38  
39 ### Open a handle on an array, and slurp in all the lines:
40 $AH = new IO::ScalarArray \@data;
41 print "All lines:\n", $AH->getlines;
42  
43 ### Get the current position (either of two ways):
44 $pos = $AH->getpos;
45 $offset = $AH->tell;
46  
47 ### Set the current position (either of two ways):
48 $AH->setpos($pos);
49 $AH->seek($offset, 0);
50  
51 ### Open an anonymous temporary array:
52 $AH = new IO::ScalarArray;
53 $AH->print("Hi there!");
54 print "I printed: ", @{$AH->aref}, "\n"; ### get at value
55  
56  
57 Don't like OO for your I/O? No problem.
58 Thanks to the magic of an invisible tie(), the following now
59 works out of the box, just as it does with IO::Handle:
60  
61 use IO::ScalarArray;
62 @data = ("My mes", "sage:\n");
63  
64 ### Open a handle on an array, and append to it:
65 $AH = new IO::ScalarArray \@data;
66 print $AH "Hello";
67 print $AH ", world!\nBye now!\n";
68 print "The array is now: ", @data, "\n";
69  
70 ### Open a handle on a string, read it line-by-line, then close it:
71 $AH = new IO::ScalarArray \@data;
72 while (<$AH>) {
73 print "Got line: $_";
74 }
75 close $AH;
76  
77 ### Open a handle on a string, and slurp in all the lines:
78 $AH = new IO::ScalarArray \@data;
79 print "All lines:\n", <$AH>;
80  
81 ### Get the current position (WARNING: requires 5.6):
82 $offset = tell $AH;
83  
84 ### Set the current position (WARNING: requires 5.6):
85 seek $AH, $offset, 0;
86  
87 ### Open an anonymous temporary scalar:
88 $AH = new IO::ScalarArray;
89 print $AH "Hi there!";
90 print "I printed: ", @{$AH->aref}, "\n"; ### get at value
91  
92  
93 And for you folks with 1.x code out there: the old tie() style still works,
94 though this is I<unnecessary and deprecated>:
95  
96 use IO::ScalarArray;
97  
98 ### Writing to a scalar...
99 my @a;
100 tie *OUT, 'IO::ScalarArray', \@a;
101 print OUT "line 1\nline 2\n", "line 3\n";
102 print "Array is now: ", @a, "\n"
103  
104 ### Reading and writing an anonymous scalar...
105 tie *OUT, 'IO::ScalarArray';
106 print OUT "line 1\nline 2\n", "line 3\n";
107 tied(OUT)->seek(0,0);
108 while (<OUT>) {
109 print "Got line: ", $_;
110 }
111  
112  
113  
114 =head1 DESCRIPTION
115  
116 This class is part of the IO::Stringy distribution;
117 see L<IO::Stringy> for change log and general information.
118  
119 The IO::ScalarArray class implements objects which behave just like
120 IO::Handle (or FileHandle) objects, except that you may use them
121 to write to (or read from) arrays of scalars. Logically, an
122 array of scalars defines an in-core "file" whose contents are
123 the concatenation of the scalars in the array. The handles created by
124 this class are automatically C<tiehandle>d (though please see L<"WARNINGS">
125 for information relevant to your Perl version).
126  
127 For writing large amounts of data with individual print() statements,
128 this class is likely to be more efficient than IO::Scalar.
129  
130 Basically, this:
131  
132 my @a;
133 $AH = new IO::ScalarArray \@a;
134 $AH->print("Hel", "lo, "); ### OO style
135 $AH->print("world!\n"); ### ditto
136  
137 Or this:
138  
139 my @a;
140 $AH = new IO::ScalarArray \@a;
141 print $AH "Hel", "lo, "; ### non-OO style
142 print $AH "world!\n"; ### ditto
143  
144 Causes @a to be set to the following array of 3 strings:
145  
146 ( "Hel" ,
147 "lo, " ,
148 "world!\n" )
149  
150 See L<IO::Scalar> and compare with this class.
151  
152  
153 =head1 PUBLIC INTERFACE
154  
155 =head2 Construction
156  
157 =over 4
158  
159 =cut
160  
161 #------------------------------
162  
163 =item new [ARGS...]
164  
165 I<Class method.>
166 Return a new, unattached array handle.
167 If any arguments are given, they're sent to open().
168  
169 =cut
170  
171 12 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
172 12 my $proto = shift;
173 12 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
174 12 my $self = bless \do { local *FH }, $class;
175 12 tie *$self, $class, $self;
176 12 $self->open(@_); ### open on anonymous by default
177 12 $self;
178 }
179 12 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
180 12 shift->close;
181 }
182  
183  
184 #------------------------------
185  
186 =item open [ARRAYREF]
187  
188 I<Instance method.>
189 Open the array handle on a new array, pointed to by ARRAYREF.
190 If no ARRAYREF is given, a "private" array is created to hold
191 the file data.
192  
193 Returns the self object on success, undefined on error.
194  
195 =cut
196  
197 12 ▶sub open {
Subroutine open: called CC=3 SCAR=11.0
Pod documented
198 12 my ($self, $aref) = @_;
199  
200 ### Sanity:
201 12 ▶ defined($aref) or do {my @a; $aref = \@a};
Branch: 1 branch
BranchTrueFalseTotal
defined $aref or do { my @a; $aref = \@a } 9 3 12
202 12 ▶ (ref($aref) eq "ARRAY") or croak "open needs a ref to a array";
Branch: 1 branch
BranchTrueFalseTotal
ref $aref eq "ARRAY" or croak("open needs a ref to a array") 12 0 12
203  
204 ### Setup:
205 12 $self->setpos([0,0]);
206 12 *$self->{AR} = $aref;
207 12 $self;
208 }
209  
210 #------------------------------
211  
212 =item opened
213  
214 I<Instance method.>
215 Is the array handle opened on something?
216  
217 =cut
218  
219 0 ▶sub opened {
Subroutine opened: not called CC=1 SCAR=6.9
Pod documented
220 0 *{shift()}->{AR};
221 }
222  
223 #------------------------------
224  
225 =item close
226  
227 I<Instance method.>
228 Disassociate the array handle from its underlying array.
229 Done automatically on destroy.
230  
231 =cut
232  
233 12 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
234 12 my $self = shift;
235 12 %{*$self} = ();
236 12 1;
237 }
238  
239 =back
240  
241 =cut
242  
243  
244  
245 #==============================
246  
247 =head2 Input and output
248  
249 =over 4
250  
251 =cut
252  
253 #------------------------------
254  
255 =item flush
256  
257 I<Instance method.>
258 No-op, provided for OO compatibility.
259  
260 =cut
261  
262 0 ▶sub flush { "0 but true" }
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
263  
264 #------------------------------
265  
266 =item fileno
267  
268 I<Instance method.>
269 No-op, returns undef
270  
271 =cut
272  
273 1 ▶sub fileno { }
Subroutine fileno: called CC=1 SCAR=0.0
Pod documented
274  
275 #------------------------------
276  
277 =item getc
278  
279 I<Instance method.>
280 Return the next character, or undef if none remain.
281 This does a read(1), which is somewhat costly.
282  
283 =cut
284  
285 6 ▶sub getc {
Subroutine getc: called CC=2 SCAR=7.2
Pod documented
286 6 my $buf = '';
287 6 ▶ ($_[0]->read($buf, 1) ? $buf : undef);
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->read($buf, 1) ? : 6 0 6
288 }
289  
290 #------------------------------
291  
292 =item getline
293  
294 I<Instance method.>
295 Return the next line, or undef on end of data.
296 Can safely be called in an array context.
297 Currently, lines are delimited by "\n".
298  
299 =cut
300  
301 41 ▶sub getline {
Subroutine getline: called CC=9 SCAR=22.4
Pod documented
302 41 my $self = shift;
303 41 my ($str, $line) = (undef, '');
304  
305  
306 ### Minimal impact implementation!
307 ### We do the fast thing (no regexps) if using the
308 ### classic input record separator.
309  
310 ### Case 1: $/ is undef: slurp all...
311 41 ▶ if (!defined($/)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $/) { } 2 39 41
elsif ($/ eq "\n") { } 39 0 39
elsif (ref $/) { } 0 0 0
312  
313 2 ▶ return undef if ($self->eof);
Branch: 1 branch
BranchTrueFalseTotal
if $self->eof 0 2 2
314  
315 ### Get the rest of the current string, followed by remaining strings:
316 2 my $ar = *$self->{AR};
317 my @slurp = (
318 substr($ar->[*$self->{Str}], *$self->{Pos}),
319 2 @$ar[(1 + *$self->{Str}) .. $#$ar ]
320 );
321  
322 ### Seek to end:
323 2 $self->_setpos_to_eof;
324 2 return join('', @slurp);
325 }
326  
327 ### Case 2: $/ is "\n":
328 elsif ($/ eq "\012") {
329  
330 ### Until we hit EOF (or exited because of a found line):
331 39 ▶ until ($self->eof) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->eof) 37 7 44
332 ### If at end of current string, go fwd to next one (won't be EOF):
333 37 ▶ if ($self->_eos) {++*$self->{Str}, *$self->{Pos}=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_eos) 20 17 37
334  
335 ### Get ref to current string in array, and set internal pos mark:
336 37 $str = \(*$self->{AR}[*$self->{Str}]); ### get current string
337 37 pos($$str) = *$self->{Pos}; ### start matching from here
338  
339 ### Get from here to either \n or end of string, and add to line:
340 37 $$str =~ m/\G(.*?)((\n)|\Z)/g; ### match to 1st \n or EOS
341 37 $line .= $1.$2; ### add it
342 37 *$self->{Pos} += length($1.$2); ### move fwd by len matched
343 37 ▶ return $line if $3; ### done, got line with "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $3 32 5 37
344 }
345 7 ▶ return ($line eq '') ? undef : $line; ### return undef if EOF
Branch: 1 branch
BranchTrueFalseTotal
$line eq '' ? : 6 1 7
346 }
347  
348 ### Case 3: $/ is ref to int. Bail out.
349 elsif (ref($/)) {
350 0 croak '$/ given as a ref to int; currently unsupported';
351 }
352  
353 ### Case 4: $/ is either "" (paragraphs) or something weird...
354 ### Bail for now.
355 else {
356 0 croak '$/ as given is currently unsupported';
357 }
358 }
359  
360 #------------------------------
361  
362 =item getlines
363  
364 I<Instance method.>
365 Get all remaining lines.
366 It will croak() if accidentally called in a scalar context.
367  
368 =cut
369  
370 2 ▶sub getlines {
Subroutine getlines: called CC=3 SCAR=11.0
Pod documented
371 2 my $self = shift;
372 2 ▶ wantarray or croak("can't call getlines in scalar context!");
Branch: 1 branch
BranchTrueFalseTotal
wantarray or croak("can't call getlines in scalar context!") 2 0 2
373 2 my ($line, @lines);
374 2 ▶ push @lines, $line while (defined($line = $self->getline));
Branch: 1 branch
BranchTrueFalseTotal
defined($line = $self->getline) and do { push @lines, $line } 10 2 12
375 2 @lines;
376 }
377  
378 #------------------------------
379  
380 =item print ARGS...
381  
382 I<Instance method.>
383 Print ARGS to the underlying array.
384  
385 Currently, this always causes a "seek to the end of the array"
386 and generates a new array entry. This may change in the future.
387  
388 =cut
389  
390 5 ▶sub print {
Subroutine print: called CC=2 SCAR=7.0
Pod documented
391 5 my $self = shift;
392 5 ▶ push @{*$self->{AR}}, join('', @_) . (defined($\) ? $\ : ""); ### add the data
Branch: 1 branch
BranchTrueFalseTotal
defined $\ ? : 0 5 5
393 5 $self->_setpos_to_eof;
394 5 1;
395 }
396  
397 #------------------------------
398  
399 =item read BUF, NBYTES, [OFFSET];
400  
401 I<Instance method.>
402 Read some bytes from the array.
403 Returns the number of bytes actually read, 0 on end-of-file, undef on error.
404  
405 =cut
406  
407 18 ▶sub read {
Subroutine read: called CC=6 SCAR=17.9
Pod documented
408 18 my $self = $_[0];
409 ### we must use $_[1] as a ref
410 18 my $n = $_[2];
411 18 ▶ my $off = $_[3] || 0;
Condition50%1/2
$_[3] || 0
l!l
018
MC/DC0%0/1
$_[3] || 0
$_[3]
Truth table50%1/2
A
A= $_[3]
Aresult
11
00
412  
413 ### print "getline\n";
414 18 my $justread;
415 my $len;
416 18 ▶ ($off ? substr($_[1], $off) : $_[1]) = '';
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 18 18
417  
418 ### Stop when we have zero bytes to go, or when we hit EOF:
419 18 my @got;
420 18 ▶ until (!$n or $self->eof) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $n or $self->eof) 27 18 45
Condition100%3/3
not $n or $self->eof
l!l&&r!l&&!r
16227
MC/DC100%2/2
not $n or $self->eof
not $n $self->eof
Truth table100%3/3
not(A) or B
A= not $nB= $self->eof
ABresult
1X1
011
000
421 ### If at end of current string, go forward to next one (won't be EOF):
422 27 ▶ if ($self->_eos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_eos) 9 18 27
423 9 ++*$self->{Str};
424 9 *$self->{Pos} = 0;
425 }
426  
427 ### Get longest possible desired substring of current string:
428 27 $justread = substr(*$self->{AR}[*$self->{Str}], *$self->{Pos}, $n);
429 27 $len = length($justread);
430 27 push @got, $justread;
431 27 $n -= $len;
432 27 *$self->{Pos} += $len;
433 }
434 18 $_[1] .= join('', @got);
435 18 return length($_[1])-$off;
436 }
437  
438 #------------------------------
439  
440 =item write BUF, NBYTES, [OFFSET];
441  
442 I<Instance method.>
443 Write some bytes into the array.
444  
445 =cut
446  
447 0 ▶sub write {
Subroutine write: not called CC=2 SCAR=17.9
Pod documented
448 0 my $self = $_[0];
449 0 my $n = $_[2];
450 0 ▶ my $off = $_[3] || 0;
Condition0%0/2
$_[3] || 0
l!l
00
MC/DC0%0/1
$_[3] || 0
$_[3]
Truth table0%0/2
A
A= $_[3]
Aresult
11
00
451  
452 0 my $data = substr($_[1], $n, $off);
453 0 $n = length($data);
454 0 $self->print($data);
455 0 return $n;
456 }
457  
458  
459 =back
460  
461 =cut
462  
463  
464  
465 #==============================
466  
467 =head2 Seeking/telling and other attributes
468  
469 =over 4
470  
471 =cut
472  
473 #------------------------------
474  
475 =item autoflush
476  
477 I<Instance method.>
478 No-op, provided for OO compatibility.
479  
480 =cut
481  
482 0 ▶sub autoflush {}
Subroutine autoflush: not called CC=1 SCAR=0.0
Pod documented
483  
484 #------------------------------
485  
486 =item binmode
487  
488 I<Instance method.>
489 No-op, provided for OO compatibility.
490  
491 =cut
492  
493 0 ▶sub binmode {}
Subroutine binmode: not called CC=1 SCAR=0.0
Pod documented
494  
495 #------------------------------
496  
497 =item clearerr
498  
499 I<Instance method.> Clear the error and EOF flags. A no-op.
500  
501 =cut
502  
503 0 ▶sub clearerr { 1 }
Subroutine clearerr: not called CC=1 SCAR=6.9
Pod documented
504  
505 #------------------------------
506  
507 =item eof
508  
509 I<Instance method.> Are we at end of file?
510  
511 =cut
512  
513 75 ▶sub eof {
Subroutine eof: called CC=4 SCAR=13.9
Pod documented
514 ### print "checking EOF [*$self->{Str}, *$self->{Pos}]\n";
515 ### print "SR = ", $#{*$self->{AR}}, "\n";
516  
517 75 ▶ return 0 if (*{$_[0]}->{Str} < $#{*{$_[0]}->{AR}}); ### before EOA
Branch: 1 branch
BranchTrueFalseTotal
if *{$_[0];}->{"Str"} < $#{*{$_[0];}->{"AR"};} 52 23 75
518 23 ▶ return 1 if (*{$_[0]}->{Str} > $#{*{$_[0]}->{AR}}); ### after EOA
Branch: 1 branch
BranchTrueFalseTotal
if *{$_[0];}->{"Str"} > $#{*{$_[0];}->{"AR"};} 0 23 23
519 ### ### at EOA, past EOS:
520 23 ▶ ((*{$_[0]}->{Str} == $#{*{$_[0]}->{AR}}) && ($_[0]->_eos));
Branch: 1 branch
BranchTrueFalseTotal
*{$_[0];}->{"Str"} == $#{*{$_[0];}->{"AR"};} and $_[0]->_eos 23 0 23
521 }
522  
523 #------------------------------
524 #
525 # _eos
526 #
527 # I<Instance method, private.> Are we at end of the CURRENT string?
528 #
529 87 ▶sub _eos {
Subroutine _eos: called CC=1 SCAR=0.0
530 87 (*{$_[0]}->{Pos} >= length(*{$_[0]}->{AR}[*{$_[0]}->{Str}])); ### past last char
531 }
532  
533 #------------------------------
534  
535 =item seek POS,WHENCE
536  
537 I<Instance method.>
538 Seek to a given position in the stream.
539 Only a WHENCE of 0 (SEEK_SET) is supported.
540  
541 =cut
542  
543 22 ▶sub seek {
Subroutine seek: called CC=4 SCAR=14.0
Pod documented
544 22 my ($self, $pos, $whence) = @_;
545  
546 ### Seek:
547 22 ▶ if ($whence == 0) { $self->_seek_set($pos); }
Branch: 3 branches
BranchTrueFalseTotal
if ($whence == 0) { } 18 4 22
elsif ($whence == 1) { } 2 2 4
elsif ($whence == 2) { } 2 0 2
548 2 elsif ($whence == 1) { $self->_seek_cur($pos); }
549 2 elsif ($whence == 2) { $self->_seek_end($pos); }
550 0 else { croak "bad seek whence ($whence)" }
551 22 return 1;
552 }
553  
554 #------------------------------
555 #
556 # _seek_set POS
557 #
558 # Instance method, private.
559 # Seek to $pos relative to start:
560 #
561 22 ▶sub _seek_set {
Subroutine _seek_set: called CC=4 SCAR=14.6
562 22 my ($self, $pos) = @_;
563  
564 ### Advance through array until done:
565 22 my $istr = 0;
566 22 ▶ while (($pos >= 0) && ($istr < scalar(@{*$self->{AR}}))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0 and $istr < scalar @{*$self->{"AR"};}) 40 0 40
Condition33%1/3
$pos >= 0 and $istr < scalar @{*$self->{"AR"};}
!ll&&!rl&&r
0040
MC/DC0%0/2
$pos >= 0 and $istr < scalar @{*$self->{"AR"};}
$pos >= 0 $istr < scalar @{*$self->{"AR"};}
Truth table33%1/3
A and B
A= $pos >= 0B= $istr < scalar @{*$self->{"AR"};}
ABresult
0X0
100
111
567 40 ▶ if (length(*$self->{AR}[$istr]) > $pos) { ### it's in this string!
Branch: 1 branch
BranchTrueFalseTotal
if (length *$self->{'AR'}[$istr] > $pos) { } 22 18 40
568 22 return $self->setpos([$istr, $pos]);
569 }
570 else { ### it's in next string
571 18 $pos -= length(*$self->{AR}[$istr++]); ### move forward one string
572 }
573 }
574 ### If we reached this point, pos is at or past end; zoom to EOF:
575 0 return $self->_setpos_to_eof;
576 }
577  
578 #------------------------------
579 #
580 # _seek_cur POS
581 #
582 # Instance method, private.
583 # Seek to $pos relative to current position.
584 #
585 2 ▶sub _seek_cur {
Subroutine _seek_cur: called CC=1 SCAR=0.0
586 2 my ($self, $pos) = @_;
587 2 $self->_seek_set($self->tell + $pos);
588 }
589  
590 #------------------------------
591 #
592 # _seek_end POS
593 #
594 # Instance method, private.
595 # Seek to $pos relative to end.
596 # We actually seek relative to beginning, which is simple.
597 #
598 2 ▶sub _seek_end {
Subroutine _seek_end: called CC=1 SCAR=0.0
599 2 my ($self, $pos) = @_;
600 2 $self->_seek_set($self->_tell_eof + $pos);
601 }
602  
603 #------------------------------
604  
605 =item tell
606  
607 I<Instance method.>
608 Return the current position in the stream, as a numeric offset.
609  
610 =cut
611  
612 4 ▶sub tell {
Subroutine tell: called CC=3 SCAR=11.0
Pod documented
613 4 my $self = shift;
614 4 my $off = 0;
615 4 my ($s, $str_s);
616 4 ▶ for ($s = 0; $s < *$self->{Str}; $s++) { ### count all "whole" scalars
Branch: 1 branch
BranchTrueFalseTotal
if ($s < *$self->{"Str"}) 9 4 13
617 9 ▶ defined($str_s = *$self->{AR}[$s]) or $str_s = '';
Branch: 1 branch
BranchTrueFalseTotal
defined($str_s = *$self->{"AR"}[$s]) or $str_s = "" 9 0 9
618 ###print STDERR "COUNTING STRING $s (". length($str_s) . ")\n";
619 9 $off += length($str_s);
620 }
621 ###print STDERR "COUNTING POS ($self->{Pos})\n";
622 4 return ($off += *$self->{Pos}); ### plus the final, partial one
623 }
624  
625 #------------------------------
626 #
627 # _tell_eof
628 #
629 # Instance method, private.
630 # Get position of EOF, as a numeric offset.
631 # This is identical to the size of the stream - 1.
632 #
633 2 ▶sub _tell_eof {
Subroutine _tell_eof: called CC=2 SCAR=6.9
634 2 my $self = shift;
635 2 my $len = 0;
636 2 foreach (@{*$self->{AR}}) { $len += length($_) }
637 2 $len;
638 }
639  
640 #------------------------------
641  
642 =item setpos POS
643  
644 I<Instance method.>
645 Seek to a given position in the array, using the opaque getpos() value.
646 Don't expect this to be a number.
647  
648 =cut
649  
650 41 ▶sub setpos {
Subroutine setpos: called CC=2 SCAR=7.1
Pod documented
651 41 my ($self, $pos) = @_;
652 41 ▶ (ref($pos) eq 'ARRAY') or
Branch: 1 branch
BranchTrueFalseTotal
ref $pos eq "ARRAY" or die "setpos: only use a value returned by getpos!\n" 41 0 41
653 die "setpos: only use a value returned by getpos!\n";
654 41 (*$self->{Str}, *$self->{Pos}) = @$pos;
655 }
656  
657 #------------------------------
658 #
659 # _setpos_to_eof
660 #
661 # Fast-forward to EOF.
662 #
663 7 ▶sub _setpos_to_eof {
Subroutine _setpos_to_eof: called CC=1 SCAR=0.0
664 7 my $self = shift;
665 7 $self->setpos([scalar(@{*$self->{AR}}), 0]);
666 }
667  
668 #------------------------------
669  
670 =item getpos
671  
672 I<Instance method.>
673 Return the current position in the array, as an opaque value.
674 Don't expect this to be a number.
675  
676 =cut
677  
678 0 ▶sub getpos {
Subroutine getpos: not called CC=1 SCAR=6.9
Pod documented
679 0 [*{$_[0]}->{Str}, *{$_[0]}->{Pos}];
680 }
681  
682 #------------------------------
683  
684 =item aref
685  
686 I<Instance method.>
687 Return a reference to the underlying array.
688  
689 =cut
690  
691 2 ▶sub aref {
Subroutine aref: called CC=1 SCAR=0.0
Pod documented
692 2 *{shift()}->{AR};
693 }
694  
695 =back
696  
697 =cut
698  
699 #------------------------------
700 # Tied handle methods...
701 #------------------------------
702  
703 ### Conventional tiehandle interface:
704 14 ▶sub TIEHANDLE { (defined($_[1]) && UNIVERSAL::isa($_[1],"IO::ScalarArray"))
Subroutine TIEHANDLE: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] && UNIVERSAL::isa($_[1], 'IO::ScalarArray') ? : 12 2 14
Condition100%3/3
defined $_[1] and UNIVERSAL::isa($_[1], "IO::ScalarArray")
!ll&&!rl&&r
1112
MC/DC100%2/2
defined $_[1] and UNIVERSAL::isa($_[1], "IO::ScalarArray")
defined $_[1] UNIVERSAL::isa($_[1], "IO::ScalarArray")
Truth table100%3/3
A and B
A= defined $_[1]B= UNIVERSAL::isa($_[1], "IO::ScalarArray")
ABresult
0X0
100
111
705 ? $_[1]
706 : shift->new(@_) }
707 0 ▶sub GETC { shift->getc(@_) }
Subroutine GETC: not called CC=1 SCAR=6.9
708 6 ▶sub PRINT { shift->print(@_) }
Subroutine PRINT: called CC=1 SCAR=0.0
709 0 ▶sub PRINTF { shift->print(sprintf(shift, @_)) }
Subroutine PRINTF: not called CC=1 SCAR=6.9
710 0 ▶sub READ { shift->read(@_) }
Subroutine READ: not called CC=1 SCAR=6.9
711 16 ▶sub READLINE { wantarray ? shift->getlines(@_) : shift->getline(@_) }
Subroutine READLINE: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 14 16
712 0 ▶sub WRITE { shift->write(@_); }
Subroutine WRITE: not called CC=1 SCAR=6.9
713 0 ▶sub CLOSE { shift->close(@_); }
Subroutine CLOSE: not called CC=1 SCAR=6.9
714 0 ▶sub SEEK { shift->seek(@_); }
Subroutine SEEK: not called CC=1 SCAR=6.9
715 0 ▶sub TELL { shift->tell(@_); }
Subroutine TELL: not called CC=1 SCAR=6.9
716 0 ▶sub EOF { shift->eof(@_); }
Subroutine EOF: not called CC=1 SCAR=6.9
717 0 ▶sub BINMODE { 1; }
Subroutine BINMODE: not called CC=1 SCAR=6.9
718  
719 #------------------------------------------------------------
720  
721 3 1;
722 __END__
« Lines.pm ↑ Summary Wrap.pm »