1 package IO::ScalarArray;
6 IO::ScalarArray - IO:: interface for reading/writing an array of scalars
11 Perform I/O on strings, using the basic OO interface...
14 @data = ("My mes", "sage:\n");
16 ### Open a handle on an array, and append to it:
17 $AH = new IO::ScalarArray \@data;
19 $AH->print(", world!\nBye now!\n");
20 print "The array is now: ", @data, "\n";
22 ### Open a handle on an array, read it line-by-line, then close it:
23 $AH = new IO::ScalarArray \@data;
24 while (defined($_ = $AH->getline)) {
29 ### Open a handle on an array, and slurp in all the lines:
30 $AH = new IO::ScalarArray \@data;
31 print "All lines:\n", $AH->getlines;
33 ### Get the current position (either of two ways):
37 ### Set the current position (either of two ways):
39 $AH->seek($offset, 0);
41 ### Open an anonymous temporary array:
42 $AH = new IO::ScalarArray;
43 $AH->print("Hi there!");
44 print "I printed: ", @{$AH->aref}, "\n"; ### get at value
47 Don't like OO for your I/O? No problem.
48 Thanks to the magic of an invisible tie(), the following now
49 works out of the box, just as it does with IO::Handle:
52 @data = ("My mes", "sage:\n");
54 ### Open a handle on an array, and append to it:
55 $AH = new IO::ScalarArray \@data;
57 print $AH ", world!\nBye now!\n";
58 print "The array is now: ", @data, "\n";
60 ### Open a handle on a string, read it line-by-line, then close it:
61 $AH = new IO::ScalarArray \@data;
67 ### Open a handle on a string, and slurp in all the lines:
68 $AH = new IO::ScalarArray \@data;
69 print "All lines:\n", <$AH>;
71 ### Get the current position (WARNING: requires 5.6):
74 ### Set the current position (WARNING: requires 5.6):
77 ### Open an anonymous temporary scalar:
78 $AH = new IO::ScalarArray;
79 print $AH "Hi there!";
80 print "I printed: ", @{$AH->aref}, "\n"; ### get at value
83 And for you folks with 1.x code out there: the old tie() style still works,
84 though this is I<unnecessary and deprecated>:
88 ### Writing to a scalar...
90 tie *OUT, 'IO::ScalarArray', \@a;
91 print OUT "line 1\nline 2\n", "line 3\n";
92 print "Array is now: ", @a, "\n"
94 ### Reading and writing an anonymous scalar...
95 tie *OUT, 'IO::ScalarArray';
96 print OUT "line 1\nline 2\n", "line 3\n";
99 print "Got line: ", $_;
106 This class is part of the IO::Stringy distribution;
107 see L<IO::Stringy> for change log and general information.
109 The IO::ScalarArray class implements objects which behave just like
110 IO::Handle (or FileHandle) objects, except that you may use them
111 to write to (or read from) arrays of scalars. Logically, an
112 array of scalars defines an in-core "file" whose contents are
113 the concatenation of the scalars in the array. The handles created by
114 this class are automatically tiehandle'd (though please see L<"WARNINGS">
115 for information relevant to your Perl version).
117 For writing large amounts of data with individual print() statements,
118 this class is likely to be more efficient than IO::Scalar.
123 $AH = new IO::ScalarArray \@a;
124 $AH->print("Hel", "lo, "); ### OO style
125 $AH->print("world!\n"); ### ditto
130 $AH = new IO::ScalarArray \@a;
131 print $AH "Hel", "lo, "; ### non-OO style
132 print $AH "world!\n"; ### ditto
134 Causes @a to be set to the following array of 3 strings:
140 See L<IO::Scalar> and compare with this class.
143 =head1 PUBLIC INTERFACE
149 use vars qw($VERSION @ISA);
152 # The package version, both in 1.23 style *and* usable by MakeMaker:
156 @ISA = qw(IO::Handle);
157 require IO::WrapTie and push @ISA, 'IO::WrapTie::Slave' if ($] >= 5.004);
160 #==============================
168 #------------------------------
173 Return a new, unattached array handle.
174 If any arguments are given, they're sent to open().
180 my $class = ref($proto) || $proto;
181 my $self = bless \do { local *FH }, $class;
182 tie *$self, $class, $self;
183 $self->open(@_); ### open on anonymous by default
191 #------------------------------
193 =item open [ARRAYREF]
196 Open the array handle on a new array, pointed to by ARRAYREF.
197 If no ARRAYREF is given, a "private" array is created to hold
200 Returns the self object on success, undefined on error.
205 my ($self, $aref) = @_;
208 defined($aref) or do {my @a; $aref = \@a};
209 (ref($aref) eq "ARRAY") or croak "open needs a ref to a array";
212 $self->setpos([0,0]);
213 *$self->{AR} = $aref;
217 #------------------------------
222 Is the array handle opened on something?
230 #------------------------------
235 Disassociate the array handle from its underlying array.
236 Done automatically on destroy.
252 #==============================
254 =head2 Input and output
260 #------------------------------
265 No-op, provided for OO compatibility.
269 sub flush { "0 but true" }
271 #------------------------------
276 Return the next character, or undef if none remain.
277 This does a read(1), which is somewhat costly.
283 ($_[0]->read($buf, 1) ? $buf : undef);
286 #------------------------------
291 Return the next line, or undef on end of data.
292 Can safely be called in an array context.
293 Currently, lines are delimited by "\n".
299 my ($str, $line) = (undef, '');
302 ### Minimal impact implementation!
303 ### We do the fast fast thing (no regexps) if using the
304 ### classic input record separator.
306 ### Case 1: $/ is undef: slurp all...
309 return undef if ($self->eof);
311 ### Get the rest of the current string, followed by remaining strings:
312 my $ar = *$self->{AR};
314 substr($ar->[*$self->{Str}], *$self->{Pos}),
315 @$ar[(1 + *$self->{Str}) .. $#$ar ]
319 $self->_setpos_to_eof;
320 return join('', @slurp);
323 ### Case 2: $/ is "\n":
324 elsif ($/ eq "\012") {
326 ### Until we hit EOF (or exitted because of a found line):
328 ### If at end of current string, go fwd to next one (won't be EOF):
329 if ($self->_eos) {++*$self->{Str}, *$self->{Pos}=0};
331 ### Get ref to current string in array, and set internal pos mark:
332 $str = \(*$self->{AR}[*$self->{Str}]); ### get current string
333 pos($$str) = *$self->{Pos}; ### start matching from here
335 ### Get from here to either \n or end of string, and add to line:
336 $$str =~ m/\G(.*?)((\n)|\Z)/g; ### match to 1st \n or EOS
337 $line .= $1.$2; ### add it
338 *$self->{Pos} += length($1.$2); ### move fwd by len matched
339 return $line if $3; ### done, got line with "\n"
341 return ($line eq '') ? undef : $line; ### return undef if EOF
344 ### Case 3: $/ is ref to int. Bail out.
346 croak '$/ given as a ref to int; currently unsupported';
349 ### Case 4: $/ is either "" (paragraphs) or something weird...
352 croak '$/ as given is currently unsupported';
356 #------------------------------
361 Get all remaining lines.
362 It will croak() if accidentally called in a scalar context.
368 wantarray or croak("can't call getlines in scalar context!");
370 push @lines, $line while (defined($line = $self->getline));
374 #------------------------------
379 Print ARGS to the underlying array.
381 Currently, this always causes a "seek to the end of the array"
382 and generates a new array entry. This may change in the future.
388 push @{*$self->{AR}}, join('', @_) . (defined($\) ? $\ : ""); ### add the data
389 $self->_setpos_to_eof;
393 #------------------------------
395 =item read BUF, NBYTES, [OFFSET];
398 Read some bytes from the array.
399 Returns the number of bytes actually read, 0 on end-of-file, undef on error.
405 ### we must use $_[1] as a ref
407 my $off = $_[3] || 0;
409 ### print "getline\n";
412 ($off ? substr($_[1], $off) : $_[1]) = '';
414 ### Stop when we have zero bytes to go, or when we hit EOF:
416 until (!$n or $self->eof) {
417 ### If at end of current string, go forward to next one (won't be EOF):
423 ### Get longest possible desired substring of current string:
424 $justread = substr(*$self->{AR}[*$self->{Str}], *$self->{Pos}, $n);
425 $len = length($justread);
426 push @got, $justread;
428 *$self->{Pos} += $len;
430 $_[1] .= join('', @got);
431 return length($_[1])-$off;
434 #------------------------------
436 =item write BUF, NBYTES, [OFFSET];
439 Write some bytes into the array.
446 my $off = $_[3] || 0;
448 my $data = substr($_[1], $n, $off);
461 #==============================
463 =head2 Seeking/telling and other attributes
469 #------------------------------
474 No-op, provided for OO compatibility.
480 #------------------------------
485 No-op, provided for OO compatibility.
491 #------------------------------
495 I<Instance method.> Clear the error and EOF flags. A no-op.
501 #------------------------------
505 I<Instance method.> Are we at end of file?
510 ### print "checking EOF [*$self->{Str}, *$self->{Pos}]\n";
511 ### print "SR = ", $#{*$self->{AR}}, "\n";
513 return 0 if (*{$_[0]}->{Str} < $#{*{$_[0]}->{AR}}); ### before EOA
514 return 1 if (*{$_[0]}->{Str} > $#{*{$_[0]}->{AR}}); ### after EOA
515 ### ### at EOA, past EOS:
516 ((*{$_[0]}->{Str} == $#{*{$_[0]}->{AR}}) && ($_[0]->_eos));
519 #------------------------------
523 # I<Instance method, private.> Are we at end of the CURRENT string?
526 (*{$_[0]}->{Pos} >= length(*{$_[0]}->{AR}[*{$_[0]}->{Str}])); ### past last char
529 #------------------------------
531 =item seek POS,WHENCE
534 Seek to a given position in the stream.
535 Only a WHENCE of 0 (SEEK_SET) is supported.
540 my ($self, $pos, $whence) = @_;
543 if ($whence == 0) { $self->_seek_set($pos); }
544 elsif ($whence == 1) { $self->_seek_cur($pos); }
545 elsif ($whence == 2) { $self->_seek_end($pos); }
546 else { croak "bad seek whence ($whence)" }
550 #------------------------------
554 # Instance method, private.
555 # Seek to $pos relative to start:
558 my ($self, $pos) = @_;
560 ### Advance through array until done:
562 while (($pos >= 0) && ($istr < scalar(@{*$self->{AR}}))) {
563 if (length(*$self->{AR}[$istr]) > $pos) { ### it's in this string!
564 return $self->setpos([$istr, $pos]);
566 else { ### it's in next string
567 $pos -= length(*$self->{AR}[$istr++]); ### move forward one string
570 ### If we reached this point, pos is at or past end; zoom to EOF:
571 return $self->_setpos_to_eof;
574 #------------------------------
578 # Instance method, private.
579 # Seek to $pos relative to current position.
582 my ($self, $pos) = @_;
583 $self->_seek_set($self->tell + $pos);
586 #------------------------------
590 # Instance method, private.
591 # Seek to $pos relative to end.
592 # We actually seek relative to beginning, which is simple.
595 my ($self, $pos) = @_;
596 $self->_seek_set($self->_tell_eof + $pos);
599 #------------------------------
604 Return the current position in the stream, as a numeric offset.
612 for ($s = 0; $s < *$self->{Str}; $s++) { ### count all "whole" scalars
613 defined($str_s = *$self->{AR}[$s]) or $str_s = '';
614 ###print STDERR "COUNTING STRING $s (". length($str_s) . ")\n";
615 $off += length($str_s);
617 ###print STDERR "COUNTING POS ($self->{Pos})\n";
618 return ($off += *$self->{Pos}); ### plus the final, partial one
621 #------------------------------
625 # Instance method, private.
626 # Get position of EOF, as a numeric offset.
627 # This is identical to the size of the stream - 1.
632 foreach (@{*$self->{AR}}) { $len += length($_) }
636 #------------------------------
641 Seek to a given position in the array, using the opaque getpos() value.
642 Don't expect this to be a number.
647 my ($self, $pos) = @_;
648 (ref($pos) eq 'ARRAY') or
649 die "setpos: only use a value returned by getpos!\n";
650 (*$self->{Str}, *$self->{Pos}) = @$pos;
653 #------------------------------
657 # Fast-forward to EOF.
661 $self->setpos([scalar(@{*$self->{AR}}), 0]);
664 #------------------------------
669 Return the current position in the array, as an opaque value.
670 Don't expect this to be a number.
675 [*{$_[0]}->{Str}, *{$_[0]}->{Pos}];
678 #------------------------------
683 Return a reference to the underlying array.
695 #------------------------------
696 # Tied handle methods...
697 #------------------------------
699 ### Conventional tiehandle interface:
700 sub TIEHANDLE { (defined($_[1]) && UNIVERSAL::isa($_[1],"IO::ScalarArray"))
703 sub GETC { shift->getc(@_) }
704 sub PRINT { shift->print(@_) }
705 sub PRINTF { shift->print(sprintf(shift, @_)) }
706 sub READ { shift->read(@_) }
707 sub READLINE { wantarray ? shift->getlines(@_) : shift->getline(@_) }
708 sub WRITE { shift->write(@_); }
709 sub CLOSE { shift->close(@_); }
710 sub SEEK { shift->seek(@_); }
711 sub TELL { shift->tell(@_); }
712 sub EOF { shift->eof(@_); }
714 #------------------------------------------------------------
719 # SOME PRIVATE NOTES:
721 # * The "current position" is the position before the next
722 # character to be read/written.
724 # * Str gives the string index of the current position, 0-based
726 # * Pos gives the offset within AR[Str], 0-based.
728 # * Inital pos is [0,0]. After print("Hello"), it is [1,0].
734 Perl's TIEHANDLE spec was incomplete prior to 5.005_57;
735 it was missing support for C<seek()>, C<tell()>, and C<eof()>.
736 Attempting to use these functions with an IO::ScalarArray will not work
737 prior to 5.005_57. IO::ScalarArray will not have the relevant methods
738 invoked; and even worse, this kind of bug can lie dormant for a while.
739 If you turn warnings on (via C<$^W> or C<perl -w>),
740 and you see something like this...
742 attempt to seek on unopened filehandle
744 ...then you are probably trying to use one of these functions
745 on an IO::ScalarArray with an old Perl. The remedy is to simply
746 use the OO version; e.g.:
748 $AH->seek(0,0); ### GOOD: will work on any 5.005
749 seek($AH,0,0); ### WARNING: will only work on 5.005_57 and beyond
755 $Id: ScalarArray.pm,v 1.7 2005/02/10 21:21:53 dfs Exp $
760 =head2 Primary Maintainer
762 David F. Skoll (F<dfs@roaringpenguin.com>).
764 =head2 Principal author
766 Eryq (F<eryq@zeegee.com>).
767 President, ZeeGee Software Inc (F<http://www.zeegee.com>).
770 =head2 Other contributors
772 Thanks to the following individuals for their invaluable contributions
773 (if I've forgotten or misspelled your name, please email me!):
776 for suggesting C<getc()>.
779 for suggesting C<opened()>.
782 for his offset-using read() and write() implementations.
785 for the IO::Handle inheritance and automatic tie-ing.
789 #------------------------------