IO::ScalarArray(3)
π IO::ScalarArray(3) π
okay bestie, welcome to IO::ScalarArray(3) from the User Contributed Perl Documentation π
this is the whole page, and yes, weβre keeping every deet.
β¨ NAME β¨
IO::ScalarArray β IO:: interface for reading/writing an array of scalars π
π¦ SYNOPSIS π¦
Perform I/O on strings, using the basic OO interfaceβ¦
use IO::ScalarArray;
@data = ("My mes", "sage:\n");
### Open a handle on an array, and append to it:
$AH = new IO::ScalarArray \@data;
$AH->print("Hello");
$AH->print(", world!\nBye now!\n");
print "The array is now: ", @data, "\n";
### Open a handle on an array, read it line-by-line, then close it:
$AH = new IO::ScalarArray \@data;
while (defined($_ = $AH->getline)) {
print "Got line: $_";
}
$AH->close;
### Open a handle on an array, and slurp in all the lines:
$AH = new IO::ScalarArray \@data;
print "All lines:\n", $AH->getlines;
### Get the current position (either of two ways):
$pos = $AH->getpos;
$offset = $AH->tell;
### Set the current position (either of two ways):
$AH->setpos($pos);
$AH->seek($offset, 0);
### Open an anonymous temporary array:
$AH = new IO::ScalarArray;
$AH->print("Hi there!");
print "I printed: ", @{$AH->aref}, "\n"; ### get at value
Donβt like OO for your I/O? No problem, bestie π«Ά thanks to the magic of an invisible tie(), the following now works out of the box, just as it does with IO::Handle:
use IO::ScalarArray;
@data = ("My mes", "sage:\n");
### Open a handle on an array, and append to it:
$AH = new IO::ScalarArray \@data;
print $AH "Hello";
print $AH ", world!\nBye now!\n";
print "The array is now: ", @data, "\n";
### Open a handle on a string, read it line-by-line, then close it:
$AH = new IO::ScalarArray \@data;
while (<$AH>) {
print "Got line: $_";
}
close $AH;
### Open a handle on a string, and slurp in all the lines:
$AH = new IO::ScalarArray \@data;
print "All lines:\n", <$AH>;
### Get the current position (WARNING: requires 5.6):
$offset = tell $AH;
### Set the current position (WARNING: requires 5.6):
seek $AH, $offset, 0;
### Open an anonymous temporary scalar:
$AH = new IO::ScalarArray;
print $AH "Hi there!";
print "I printed: ", @{$AH->aref}, "\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::ScalarArray;
### Writing to a scalar...
my @a;
tie *OUT, 'IO::ScalarArray', \@a;
print OUT "line 1\nline 2\n", "line 3\n";
print "Array is now: ", @a, "\n"
### Reading and writing an anonymous scalar...
tie *OUT, 'IO::ScalarArray';
print OUT "line 1\nline 2\n", "line 3\n";
tied(OUT)->seek(0,0);
while (<OUT>) {
print "Got line: ", $_;
}
πΈ DESCRIPTION πΈ
This class is part of the IO::Stringy distribution; see IO::Stringy for change log and general information π
The IO::ScalarArray class implements objects which behave just like IO::Handle (or FileHandle) objects, except that you may use them to write to (or read from) arrays of scalars. Logically, an array of scalars defines an in-core "file" whose contents are the concatenation of the scalars in the array. The handles created by this class are automatically "tiehandle"d (though please see "WARNINGS" for information relevant to your Perl version).
For writing large amounts of data with individual print() statements, this class is likely to be more efficient than IO::Scalar π₯
Basically, this:
my @a;
$AH = new IO::ScalarArray \@a;
$AH->print("Hel", "lo, "); ### OO style
$AH->print("world!\n"); ### ditto
Or this:
my @a;
$AH = new IO::ScalarArray \@a;
print $AH "Hel", "lo, "; ### non-OO style
print $AH "world!\n"; ### ditto
Causes @a to be set to the following array of 3 strings:
( "Hel" ,
"lo, " ,
"world!\n" )
See IO::Scalar and compare with this class π¦
π PUBLIC INTERFACE π
π Construction
new [ARGS...]β Class method. Return a new, unattached array handle. If any arguments are given, theyβre sent toopen().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.filenoβ Instance method. No-op, returns undef.getcβ Instance method. Return the next character, or undef if none remain. This does aread(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 willcroak()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,0on 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 of0(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 opaquegetpos()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.
π AUTHOR π
Eryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com).
π§ CONTRIBUTORS π§
Dianne Skoll (dfs@roaringpenguin.com).
π COPYRIGHT & LICENSE π
Copyright (c) 1997 Erik (Eryq) Dorfman, ZeeGee Software, Inc. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay thatβs the whole page, bestie π«Ά footer deets for the sign-off: perl v5.42.0, dated 2020-06-04. bye and slay β¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-IO-stringy 2.113-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms