IO::Stringy | IO::AtomicFile | IO::Lines | IO::Scalar |
IO::ScalarArray | IO::Stringy | IO::Wrap | IO::WrapTie |
IO:: |
IO::ScalarArray - IO:: interface for reading/writing an array of scalars
If you have any Perl5, you can use the basic OO interface...
use IO::ScalarArray;
# Open a handle on an array-of-scalars: $AH = new IO::ScalarArray; $AH->open(\@a);
# Open a handle on an array-of-scalars, read it line-by-line, # then close it: $AH = new IO::ScalarArray \@a; while ($_ = $AH->getline) { print "Line: $_" } $AH->close;
# Open a handle on an array-of-scalars, and slurp in all the lines: $AH = new IO::ScalarArray \@a; print $AH->getlines;
# Open a handle on an array-of-scalars, and append to it: $AH = new IO::ScalarArray \@a; $AH->print("bar\n"); print "some string is now: ", $somestring, "\n";
# Get the current position: $pos = $AH->getpos; ### $AH->tell() also works
# Set the current position: $AH->setpos($pos); ### $AH->seek(POS,WHENCE) also works
# Open an anonymous temporary scalar array: $AH = new IO::ScalarArray; $AH->print("Hi there!\nHey there!\n"); $AH->print("Ho there!\n"); print "I got: ", @{$AH->aref}, "\n"; ### get at value
If your Perl is 5.004 or later, you can use the TIEHANDLE interface, and read/write as array-of-scalars just like files:
use IO::ScalarArray;
# Writing to a scalar array... my @a; tie *OUT, 'IO::ScalarArray', \@a; print OUT "line 1\nline 2\n", "line 3\n"; print "s is now... [", join('', @a), "]\n";
# Reading and writing an anonymous scalar array... tie *OUT, 'IO::ScalarArray'; print OUT "line 1\nline 2\n", "line 3\n"; tied(OUT)->seek(0,0); while (<OUT>) { print "LINE: ", $_ }
This class implements objects which behave just like FileHandle (or IO::Handle) objects, except that you may use them to write to (or read from) scalars. They can be tiehandle'd as well.
For writing large amounts of data with individual print() statements, this is likely to be more efficient than IO::Scalar.
Basically, this:
my @a; $AH = new IO::ScalarArray \@a; $AH->print("Hel", "lo, "); $AH->print("world!\n");
Or this (if you have 5.004 or later):
my @a; $AH = tie *OUT, 'IO::ScalarArray', \@a; print OUT "Hel", "lo, "; print OUT "world!\n";
Causes @a to be set to the following arrayt of 3 strings:
( "Hel" , "lo, " , "world!\n" )
Compare this with IO::Scalar.
Returns the self object on success, undefined on error.
Currently, this always causes a "seek to the end of the array" and generates a new array entry. This may change in the future.
$Id: ScalarArray.pm,v 1.112 1998/12/16 02:00:04 eryq Exp $
Eryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com).
Thanks to Andy Glew for suggesting getc()
.
Thanks to Brandon Browning for suggesting opened()
.