NAME

IO::Scalar - IO:: interface for reading/writing a scalar

SYNOPSIS

If 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; 
 
### 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)->seek(0,0);
while (<OUT>) { print "LINE: ", $_ }

Stringification now works, too!

my $SH = new IO::Scalar \$somestring;
$SH->print("Hello, ");
$SH->print("world!");
print "I've got: <$SH>\n";

DESCRIPTION

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.

Basically, this:

my $s;
$SH = new IO::Scalar \$s;
$SH->print("Hel", "lo, ");         # OO style
$SH->print("world!\n");            # ditto

Or this (if you have 5.004 or later):

my $s;
$SH = tie *OUT, 'IO::Scalar', \$s;
print OUT "Hel", "lo, ";           # non-OO style
print OUT "world!\n";              # ditto

Or this (if you have 5.004 or later):

my $s;
$SH = IO::Scalar->new_tie(\$s);
$SH->print("Hel", "lo, ");         # OO style...
print $SH "world!\n";              # ...or non-OO style!

Causes $s to be set to:

"Hello, world!\n" 

PUBLIC INTERFACE

Construction

new [ARGS...]

Class method. Return a new, unattached scalar handle. If any arguments are given, they're sent to open().

open [SCALARREF]

Instance method. Open the scalar handle on a new scalar, pointed to by SCALARREF. If no SCALARREF is given, a "private" scalar is created to hold the file data.

Returns the self object on success, undefined on error.

opened

Instance method. Is the scalar handle opened on something?

close

Instance method. Disassociate the scalar handle from its underlying scalar. Done automatically on destroy.

Input and output

flush

Instance method. No-op, provided for OO compatibility.

getc

Instance method. Return the next character, or undef if none remain.

getline

Instance method. Return the next line, or undef on end of string. Can safely be called in an array context. Currently, lines are delimited by "\n".

getlines

Instance method. Get all remaining lines. It will croak() if accidentally called in a scalar context.

Instance method. Print ARGS to the underlying scalar.

Warning: Currently, this always causes a "seek to the end of the string"; this may change in the future.

read BUF, NBYTES, [OFFSET]

Instance method. Read some bytes from the scalar. Returns the number of bytes actually read, 0 on end-of-file, undef on error.

write BUF, NBYTES, [OFFSET]

Instance method. Write some bytes to the scalar.

Seeking/telling and other attributes

autoflush

Instance method. No-op, provided for OO compatibility.

binmode

Instance method. No-op, provided for OO compatibility.

clearerr

Instance method. Clear the error and EOF flags. A no-op.

eof

Instance method. Are we at end of file?

seek OFFSET, WHENCE

Instance method. Seek to a given position in the stream.

tell

Instance method. Return the current position in the stream, as a numeric offset.

setpos POS

Instance method. Set the current position, using the opaque value returned by getpos().

getpos

Instance method. Return the current position in the string, as an opaque object.

sref

Instance method. Return a reference to the underlying scalar.

VERSION

$Id: Scalar.pm,v 1.121 2000/09/05 03:53:58 eryq Exp $

AUTHORS

Principal author

Eryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com).

Other contributors

Thanks to the following individuals for their invaluable contributions (if I've forgotten or misspelled your name, please email me!):

Andy Glew, for contributing getc().

Brandon Browning, for suggesting opened().

David Richter, for finding and fixing the bug in PRINTF().

Eric L. Brine, for his offset-using read() and write() implementations.

Rich (at Annexia), for his patch to massively improve the performance of getline().