# IO::ScalarArray

- NAME
- SYNOPSIS
- DESCRIPTION
- PUBLIC INTERFACE
  - Construction
  - Input and output
  - Seeking/telling and other attributes
- VERSION
- AUTHOR
  - Principal author
  - Other contributors

## NAME

IO::ScalarArray - IO:: interface for reading/writing an array of scalars

## SYNOPSIS

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: ", $_ }

## 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.

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.

## PUBLIC INTERFACE

### Construction

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

open [ARRAYREF]
  Instance method. Open the array handle on a new array, pointed to by ARRAYREF. If no ARRAYREF is given, a "private" array is created to hold the file data.

  Returns the self object on success, undefined on error.

opened
  Instance method. Is the array handle opened on something?

close
  Instance method. Disassociate the array handle from its underlying array. 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. This does a read(1), which is somewhat costly.

getline
  Instance method. Return the next line, or undef on end of data. 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.

print ARGS...
  Instance method. Print ARGS to the underlying array.

  Currently, this always causes a "seek to the end of the array" and generates a new array entry. This may change in the future.

read BUF, NBYTES, [OFFSET];
  Instance method. Read some bytes from the array. Returns the number of bytes actually read, 0 on end-of-file, undef on error.

write BUF, NBYTES, [OFFSET];
  Instance method. Write some bytes into the array.

### 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 POS,WHENCE
  Instance method. Seek to a given position in the stream. Only a WHENCE of 0 (SEEK_SET) is supported.

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

setpos POS
  Instance method. Seek to a given position in the array, using the opaque getpos() value. Don't expect this to be a number.

getpos
  Instance method. Return the current position in the array, as an opaque value. Don't expect this to be a number.

aref
  Instance method. Return a reference to the underlying array.

## VERSION

$Id: ScalarArray.pm,v 1.117 2000/09/28 06:32:28 eryq Exp $

## AUTHOR

### 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 suggesting `getc()`.

Brandon Browning, for suggesting `opened()`.

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