ActivePerl Documentation
|
NAMEIO::ScalarArray - IO:: interface for reading/writing an array of scalars
SUPPORTED PLATFORMS
SYNOPSISIf 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: ", $_ }
DESCRIPTIONThis 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 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.
PUBLIC INTERFACE
Construction
Input and output
Seeking and telling
VERSION$Id: ScalarArray.pm,v 1.112 1998/12/16 02:00:04 eryq Exp $
AUTHOREryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com). Thanks to Andy Glew for suggesting Thanks to Brandon Browning for suggesting
|