SYNOPSIS

Perform I/O on strings, using the basic \s-1OO\s0 interface...

    use 5.005;
    use IO::Scalar;
    $data = "My message:\n";

    ### Open a handle on a string, and append to it:
    $SH = new IO::Scalar \$data;
    $SH->print("Hello");
    $SH->print(", world!\nBye now!\n");
    print "The string is now: ", $data, "\n";

    ### Open a handle on a string, read it line-by-line, then close it:
    $SH = new IO::Scalar \$data;
    while (defined($_ = $SH->getline)) {
        print "Got line: $_";
    }
    $SH->close;

    ### Open a handle on a string, and slurp in all the lines:
    $SH = new IO::Scalar \$data;
    print "All lines:\n", $SH->getlines;

    ### Get the current position (either of two ways):
    $pos = $SH->getpos;
    $offset = $SH->tell;

    ### Set the current position (either of two ways):
    $SH->setpos($pos);
    $SH->seek($offset, 0);

    ### Open an anonymous temporary scalar:
    $SH = new IO::Scalar;
    $SH->print("Hi there!");
    print "I printed: ", ${$SH->sref}, "\n";      ### get at value

Don't like \s-1OO\s0 for your I/O? No problem. Thanks to the magic of an invisible tie(), the following now works out of the box, just as it does with IO::Handle:

use 5.005; use IO::Scalar; $data = "My message:\n";

### Open a handle on a string, and append to it: $SH = new IO::Scalar \$data; print $SH "Hello"; print $SH ", world!\nBye now!\n"; print "The string is now: ", $data, "\n";

### Open a handle on a string, read it line-by-line, then close it: $SH = new IO::Scalar \$data; while (<$SH>) { print "Got line: $_"; } close $SH;

### Open a handle on a string, and slurp in all the lines: $SH = new IO::Scalar \$data; print "All lines:\n", <$SH>;

### Get the current position (WARNING: requires 5.6): $offset = tell $SH;

### Set the current position (WARNING: requires 5.6): seek $SH, $offset, 0;

### Open an anonymous temporary scalar: $SH = new IO::Scalar; print $SH "Hi there!"; print "I printed: ", ${$SH->sref}, "\n"; ### get at value

And for you folks with 1.x code out there: the old tie() style still works, though this is unnecessary and deprecated:

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 "String 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 "Got line: ", $_; }

Stringification works, too!

my $SH = new IO::Scalar \$data; print $SH "Hello, "; print $SH "world!"; print "I printed: $SH\n";

DESCRIPTION

This class is part of the IO::Stringy distribution; see IO::Stringy for change log and general information.

The IO::Scalar class implements objects which behave just like IO::Handle (or FileHandle) objects, except that you may use them to write to (or read from) scalars. These handles are automatically tiehandle'd (though please see \*(L"\s-1WARNINGS\s0\*(R" for information relevant to your Perl version).

Basically, this:

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

Or this:

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

Causes $s to be set to:

"Hello, world!\n"

PUBLIC INTERFACE

Construction

new [\s-1ARGS\s0...]

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

open [\s-1SCALARREF\s0]

Instance method. Open the scalar handle on a new scalar, pointed to by \s-1SCALARREF\s0. If no \s-1SCALARREF\s0 is given, a \*(L"private\*(R" 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 \s-1OO\s0 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 \*(L"\n\*(R".

getlines

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

print \s-1ARGS\s0...

Instance method. Print \s-1ARGS\s0 to the underlying scalar. Warning: this continues to always cause a seek to the end of the string, but if you perform seek()s and tell()s, it is still safer to explicitly seek-to-end before subsequent print()s.

read \s-1BUF\s0, \s-1NBYTES\s0, [\s-1OFFSET\s0]

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

write \s-1BUF\s0, \s-1NBYTES\s0, [\s-1OFFSET\s0]

Instance method. Write some bytes to the scalar.

sysread \s-1BUF\s0, \s-1LEN\s0, [\s-1OFFSET\s0]

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

syswrite \s-1BUF\s0, \s-1NBYTES\s0, [\s-1OFFSET\s0]

Instance method. Write some bytes to the scalar.

Seeking/telling and other attributes

autoflush

Instance method. No-op, provided for \s-1OO\s0 compatibility.

binmode

Instance method. No-op, provided for \s-1OO\s0 compatibility.

clearerr

Instance method. Clear the error and \s-1EOF\s0 flags. A no-op.

eof

Instance method. Are we at end of file?

seek \s-1OFFSET\s0, \s-1WHENCE\s0

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

sysseek \s-1OFFSET\s0, \s-1WHENCE\s0

Instance method. Identical to \*(C`seek OFFSET, WHENCE\*(C', q.v.

tell

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

setpos \s-1POS\s0

Instance method. Set the current position, using the opaque value returned by \*(C`getpos()\*(C'.

getpos

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

sref

Instance method. Return a reference to the underlying scalar.

WARNINGS

Perl's \s-1TIEHANDLE\s0 spec was incomplete prior to 5.005_57; it was missing support for \*(C`seek()\*(C', \*(C`tell()\*(C', and \*(C`eof()\*(C'. Attempting to use these functions with an IO::Scalar will not work prior to 5.005_57. IO::Scalar will not have the relevant methods invoked; and even worse, this kind of bug can lie dormant for a while. If you turn warnings on (via $^W or \*(C`perl -w\*(C'), and you see something like this...

attempt to seek on unopened filehandle

...then you are probably trying to use one of these functions on an IO::Scalar with an old Perl. The remedy is to simply use the \s-1OO\s0 version; e.g.:

$SH->seek(0,0); ### GOOD: will work on any 5.005 seek($SH,0,0); ### WARNING: will only work on 5.005_57 and beyond

VERSION

$Id: Scalar.pm,v 1.6 2005/02/10 21:21:53 dfs Exp $

AUTHORS

Primary Maintainer

David F. Skoll ([email protected]).

Principal author

Eryq ([email protected]). President, ZeeGee Software Inc (http://www.zeegee.com).

Other contributors

The full set of contributors always includes the folks mentioned in \*(L"\s-1CHANGE\s0 \s-1LOG\s0\*(R" in IO::Stringy. But just the same, special thanks to the following individuals for their invaluable contributions (if I've forgotten or misspelled your name, please email me!):

Andy Glew, for contributing \*(C`getc()\*(C'.

Brandon Browning, for suggesting \*(C`opened()\*(C'.

David Richter, for finding and fixing the bug in \*(C`PRINTF()\*(C'.

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

Richard Jones, for his patches to massively improve the performance of \*(C`getline()\*(C' and add \*(C`sysread\*(C' and \*(C`syswrite\*(C'.

B. K. Oxley (binkley), for stringification and inheritance improvements, and sundry good ideas.

Doug Wilson, for the IO::Handle inheritance and automatic tie-ing.

RELATED TO IO::Scalar…

IO::String, which is quite similar but which was designed more-recently and with an IO::Handle-like interface in mind, so you could mix \s-1OO-\s0 and native-filehandle usage without using tied().

Note: as of version 2.x, these classes all work like their IO::Handle counterparts, so we have comparable functionality to IO::String.