| IO::Scalar | IO::ScalarArray | IO::Wrap |
IO:: |
NAMEIO::Scalar - IO:: interface for reading/writing a scalar
SYNOPSISIf you have any Perl5, you can use the basic OO interface...
use IO::Scalar;
# Open a handle on a string:
$SH = new IO::Scalar;
$SH->open(\$somestring);
# Open a handle on a string, read it line-by-line, then close it:
$SH = new IO::Scalar \$somestring;
while ($_ = $SH->getline) {
print "Line: $_";
}
$SH->close;
# Open a handle on a string, and slurp in all the lines:
$SH = new IO::Scalar \$somestring;
print $SH->getlines;
$SH->close;
# Open a handle on a string, and append to it:
$SH = new IO::Scalar \$somestring
$SH->print("bar\n"); ### will add "bar\n" to the end
# Get the current position:
$pos = $SH->getpos; ### $SH->tell() also works
# Set the current position:
$SH->setpos($pos); ### $SH->seek(POS,WHENCE) also works
# Open an anonymous temporary scalar:
$SH = new IO::Scalar;
$SH->print("Hi there!");
print "I got: ", ${$SH->sref}, "\n"; ### get at value
If your Perl is 5.004 or later, you can use the TIEHANDLE interface, and read/write scalars just like files:
use IO::Scalar;
# Writing to a scalar...
my $s;
tie *OUT, 'IO::Scalar', \$s;
print OUT "line 1\nline 2\n", "line 3\n";
print "s is now... $s\n"
# Reading and writing an anonymous scalar...
tie *OUT, 'IO::Scalar';
print OUT "line 1\nline 2\n", "line 3\n";
tied(OUT)->setpos(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.
Basically, this:
my $s;
$SH = new IO::Scalar \$s;
$SH->print("Hel", "lo, ");
$SH->print("world!\n");
Or this (if you have 5.004 or later):
my $s;
$SH = tie *OUT, 'IO::Scalar', \$s;
print OUT "Hel", "lo, ";
print OUT "world!\n";
Causes $s to be set to:
"Hello, world!\n"
PUBLIC INTERFACE
ConstructionReturns the self object on success, undefined on error.
Input and outputWarning: Currently, this always causes a "seek to the end of the string"; this may change in the future.
Seeking and tellinggetpos().
VERSION$Id: Scalar.pm,v 1.105 1997/12/14 02:24:35 eryq Exp $
AUTHOREryq (eryq@zeegee.com), Zero G Inc.