IO::Scalar(3)
πΈ NAME πΈ
bestie, welcome to IO::Scalar(3) β straight out of the User Contributed Perl Documentation. This is the IO:: interface for reading/writing a scalar. Yes, it gives you filehandle energy, but the βfileβ is literally a string. iconic. π
β¨ SYNOPSIS β¨
okay so first up: you can do I/O on strings using the basic OO 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 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 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. ngl, it's giving legacy era β here's the vibe anyway:
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! obsessed π
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 WARNINGS 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 [ARGS...]β Class method. Return a new, unattached scalar handle. If any arguments are given, they're sent toopen().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.filenoβ Instance method. No-op, returnsundef.getcβ Instance method. Return the next character, orundefif none remain.getlineβ Instance method. Return the next line, orundefon 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 willcroak()if accidentally called in a scalar context.print ARGS...β Instance method. Print ARGS to the underlying scalar.
β οΈ Warning: this continues to always cause a seek to the end of the string, but if you performseek()s andtell()s, it is still safer to explicitly seek-to-end before subsequentprint()s.
read BUF, NBYTES, [OFFSET]β Instance method. Read some bytes from the scalar. Returns the number of bytes actually read,0on end-of-file,undefon error.write BUF, NBYTES, [OFFSET]β Instance method. Write some bytes to the scalar.sysread BUF, LEN, [OFFSET]β Instance method. Read some bytes from the scalar. Returns the number of bytes actually read,0on end-of-file,undefon error.syswrite 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.sysseek OFFSET, WHENCEβ Instance method. Identical toseek OFFSET, WHENCE, q.v.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 bygetpos().getposβ Instance method. Return the current position in the string, as an opaque object.srefβ Instance method. Return a reference to the underlying scalar.
π 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 bye bestie π this page is from perl v5.42.0, dated 2020-06-04, and it's IO::Scalar(3). periodt. β¨
π¦ 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