Copyright (C) 2000-2012 |
Manpages IO::ScalarSection: User Contributed Perl Documentation (3)Updated: perl v5.6.1 Index Return to Main Contents NAMEIO::Scalar - IO:: interface for reading/writing a scalarSYNOPSISPerform 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 valueDon't like OO 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 valueAnd 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"; DESCRIPTIONThis class is part of the IO::Stringy distribution; see the IO::Stringy manpage 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 the section on "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"); ### dittoOr this:
my $s; $SH = tie *OUT, 'IO::Scalar', \$s; print OUT "Hel", "lo, "; ### non-OO style print OUT "world!\n"; ### dittoCauses $s to be set to:
"Hello, world!\n" PUBLIC INTERFACEConstruction
Input and output
Seeking/telling and other attributes
WARNINGSPerl's TIEHANDLE spec was incomplete prior to 5.005_57; it was missing support for "seek()", "tell()", and "eof()". 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 "perl -w"), 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 OO 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 2.104 2001/08/09 08:04:44 eryq Exp $AUTHORSPrincipal authorEryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com).Other contributorsThe full set of contributors always includes the folks mentioned in the CHANGE LOG entry in the IO::Stringy manpage. 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 "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. Richard Jones, for his patches to massively improve the performance of "getline()" and add "sysread" and "syswrite". B. K. Oxley (binkley), for stringification and inheritance improvements, and sundry good ideas. Doug Wilson, for the IO::Handle inheritance and automatic tie-ing. SEE ALSOthe IO::String manpage, which is quite similar but which was designed more-recently and with an IO::Handle-like interface in mind, so you could mix OO- 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.
Index
This document was created by man2html, using the manual pages. Time: 21:52:10 GMT, April 26, 2024 |