3 <TITLE>IO::Scalar 2.105</TITLE>
6 bgcolor="#FFFFFF" link="#CC3366" vlink="#993366" alink="#FF6666">
7 <FONT FACE="sans-serif" SIZE=-1><A HREF="http://www.zeegee.com" TARGET="_top"><IMG SRC="icons/zeegee.gif" ALT="ZeeGee Software" ALIGN="RIGHT" BORDER="0"></A><A NAME="__TOP__"><H1>IO::Scalar 2.105</H1>
9 <LI> <A NAME="menu:NAME"><A HREF="#NAME">NAME</A></A>
10 <LI> <A NAME="menu:SYNOPSIS"><A HREF="#SYNOPSIS">SYNOPSIS</A></A>
11 <LI> <A NAME="menu:DESCRIPTION"><A HREF="#DESCRIPTION">DESCRIPTION</A></A>
12 <LI> <A NAME="menu:PUBLIC_INTERFACE"><A HREF="#PUBLIC_INTERFACE">PUBLIC INTERFACE</A></A>
14 <LI> <A NAME="menu:Construction"><A HREF="#Construction">Construction</A></A>
16 <LI> <A NAME="menu:item:new_ARGS"><A HREF="#item:new_ARGS">new [ARGS...]</A></A>
17 <LI> <A NAME="menu:item:open_SCALARREF"><A HREF="#item:open_SCALARREF">open [SCALARREF]</A></A>
18 <LI> <A NAME="menu:item:opened"><A HREF="#item:opened">opened</A></A>
19 <LI> <A NAME="menu:item:close"><A HREF="#item:close">close</A></A>
21 <LI> <A NAME="menu:Input_and_output"><A HREF="#Input_and_output">Input and output</A></A>
23 <LI> <A NAME="menu:item:flush"><A HREF="#item:flush">flush</A></A>
24 <LI> <A NAME="menu:item:getc"><A HREF="#item:getc">getc</A></A>
25 <LI> <A NAME="menu:item:getline"><A HREF="#item:getline">getline</A></A>
26 <LI> <A NAME="menu:item:getlines"><A HREF="#item:getlines">getlines</A></A>
27 <LI> <A NAME="menu:item:print_ARGS"><A HREF="#item:print_ARGS">print ARGS...</A></A>
28 <LI> <A NAME="menu:item:read_BUF_NBYTES_OFFSET"><A HREF="#item:read_BUF_NBYTES_OFFSET">read BUF, NBYTES, [OFFSET]</A></A>
29 <LI> <A NAME="menu:item:write_BUF_NBYTES_OFFSET"><A HREF="#item:write_BUF_NBYTES_OFFSET">write BUF, NBYTES, [OFFSET]</A></A>
30 <LI> <A NAME="menu:item:sysread_BUF_LEN_OFFSET"><A HREF="#item:sysread_BUF_LEN_OFFSET">sysread BUF, LEN, [OFFSET]</A></A>
31 <LI> <A NAME="menu:item:syswrite_BUF_NBYTES_OFFSET"><A HREF="#item:syswrite_BUF_NBYTES_OFFSET">syswrite BUF, NBYTES, [OFFSET]</A></A>
33 <LI> <A NAME="menu:Seeking_telling_and_other_attributes"><A HREF="#Seeking_telling_and_other_attributes">Seeking/telling and other attributes</A></A>
35 <LI> <A NAME="menu:item:autoflush"><A HREF="#item:autoflush">autoflush</A></A>
36 <LI> <A NAME="menu:item:binmode"><A HREF="#item:binmode">binmode</A></A>
37 <LI> <A NAME="menu:item:clearerr"><A HREF="#item:clearerr">clearerr</A></A>
38 <LI> <A NAME="menu:item:eof"><A HREF="#item:eof">eof</A></A>
39 <LI> <A NAME="menu:item:seek_OFFSET_WHENCE"><A HREF="#item:seek_OFFSET_WHENCE">seek OFFSET, WHENCE</A></A>
40 <LI> <A NAME="menu:item:sysseek_OFFSET_WHENCE"><A HREF="#item:sysseek_OFFSET_WHENCE">sysseek OFFSET, WHENCE</A></A>
41 <LI> <A NAME="menu:item:tell"><A HREF="#item:tell">tell</A></A>
42 <LI> <A NAME="menu:item:setpos_POS"><A HREF="#item:setpos_POS">setpos POS</A></A>
43 <LI> <A NAME="menu:item:getpos"><A HREF="#item:getpos">getpos</A></A>
44 <LI> <A NAME="menu:item:sref"><A HREF="#item:sref">sref</A></A>
47 <LI> <A NAME="menu:WARNINGS"><A HREF="#WARNINGS">WARNINGS</A></A>
48 <LI> <A NAME="menu:VERSION"><A HREF="#VERSION">VERSION</A></A>
49 <LI> <A NAME="menu:AUTHORS"><A HREF="#AUTHORS">AUTHORS</A></A>
51 <LI> <A NAME="menu:Principal_author"><A HREF="#Principal_author">Principal author</A></A>
52 <LI> <A NAME="menu:Other_contributors"><A HREF="#Other_contributors">Other contributors</A></A>
54 <LI> <A NAME="menu:SEE_ALSO"><A HREF="#SEE_ALSO">SEE ALSO</A></A>
59 <A NAME="NAME"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> NAME</H2></A>
62 <P>IO::Scalar - IO:: interface for reading/writing a scalar
67 <A NAME="SYNOPSIS"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> SYNOPSIS</H2></A>
70 <P>Perform I/O on strings, using the basic OO interface...
72 <FONT SIZE=3 FACE="courier"><PRE>
75 $data = "My message:\n";
77 <FONT SIZE=3 FACE="courier"><PRE>
78 ### Open a handle on a string, and append to it:
79 $SH = new IO::Scalar \$data;
80 $SH->print("Hello");
81 $SH->print(", world!\nBye now!\n");
82 print "The string is now: ", $data, "\n";
84 <FONT SIZE=3 FACE="courier"><PRE>
85 ### Open a handle on a string, read it line-by-line, then close it:
86 $SH = new IO::Scalar \$data;
87 while (defined($_ = $SH->getline)) {
88 print "Got line: $_";
92 <FONT SIZE=3 FACE="courier"><PRE>
93 ### Open a handle on a string, and slurp in all the lines:
94 $SH = new IO::Scalar \$data;
95 print "All lines:\n", $SH->getlines;
97 <FONT SIZE=3 FACE="courier"><PRE>
98 ### Get the current position (either of two ways):
99 $pos = $SH->getpos;
100 $offset = $SH->tell;
102 <FONT SIZE=3 FACE="courier"><PRE>
103 ### Set the current position (either of two ways):
104 $SH->setpos($pos);
105 $SH->seek($offset, 0);
107 <FONT SIZE=3 FACE="courier"><PRE>
108 ### Open an anonymous temporary scalar:
109 $SH = new IO::Scalar;
110 $SH->print("Hi there!");
111 print "I printed: ", ${$SH->sref}, "\n"; ### get at value
114 <P>Don't like OO for your I/O? No problem.
115 Thanks to the magic of an invisible tie(), the following now
116 works out of the box, just as it does with IO::Handle:
118 <FONT SIZE=3 FACE="courier"><PRE>
121 $data = "My message:\n";
123 <FONT SIZE=3 FACE="courier"><PRE>
124 ### Open a handle on a string, and append to it:
125 $SH = new IO::Scalar \$data;
126 print $SH "Hello";
127 print $SH ", world!\nBye now!\n";
128 print "The string is now: ", $data, "\n";
130 <FONT SIZE=3 FACE="courier"><PRE>
131 ### Open a handle on a string, read it line-by-line, then close it:
132 $SH = new IO::Scalar \$data;
133 while (<$SH>) {
134 print "Got line: $_";
138 <FONT SIZE=3 FACE="courier"><PRE>
139 ### Open a handle on a string, and slurp in all the lines:
140 $SH = new IO::Scalar \$data;
141 print "All lines:\n", <$SH>;
143 <FONT SIZE=3 FACE="courier"><PRE>
144 ### Get the current position (WARNING: requires 5.6):
147 <FONT SIZE=3 FACE="courier"><PRE>
148 ### Set the current position (WARNING: requires 5.6):
149 seek $SH, $offset, 0;
151 <FONT SIZE=3 FACE="courier"><PRE>
152 ### Open an anonymous temporary scalar:
153 $SH = new IO::Scalar;
154 print $SH "Hi there!";
155 print "I printed: ", ${$SH->sref}, "\n"; ### get at value
158 <P>And for you folks with 1.x code out there: the old tie() style still works,
159 though this is <I>unnecessary and deprecated</I>:
161 <FONT SIZE=3 FACE="courier"><PRE>
164 <FONT SIZE=3 FACE="courier"><PRE>
165 ### Writing to a scalar...
167 tie *OUT, 'IO::Scalar', \$s;
168 print OUT "line 1\nline 2\n", "line 3\n";
169 print "String is now: $s\n"
171 <FONT SIZE=3 FACE="courier"><PRE>
172 ### Reading and writing an anonymous scalar...
173 tie *OUT, 'IO::Scalar';
174 print OUT "line 1\nline 2\n", "line 3\n";
175 tied(OUT)->seek(0,0);
176 while (<OUT>) {
177 print "Got line: ", $_;
181 <P>Stringification works, too!
183 <FONT SIZE=3 FACE="courier"><PRE>
184 my $SH = new IO::Scalar \$data;
185 print $SH "Hello, ";
186 print $SH "world!";
187 print "I printed: $SH\n";
192 <A NAME="DESCRIPTION"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> DESCRIPTION</H2></A>
195 <P>This class is part of the IO::Stringy distribution;
196 see <A HREF="../IO/Stringy.pm.html">IO::Stringy</A> for change log and general information.
199 <P>The IO::Scalar class implements objects which behave just like
200 IO::Handle (or FileHandle) objects, except that you may use them
201 to write to (or read from) scalars. These handles are
202 automatically tiehandle'd (though please see <A HREF="#WARNINGS">WARNINGS</A>
203 for information relevant to your Perl version).
208 <FONT SIZE=3 FACE="courier"><PRE>
210 $SH = new IO::Scalar \$s;
211 $SH->print("Hel", "lo, "); ### OO style
212 $SH->print("world!\n"); ### ditto
217 <FONT SIZE=3 FACE="courier"><PRE>
219 $SH = tie *OUT, 'IO::Scalar', \$s;
220 print OUT "Hel", "lo, "; ### non-OO style
221 print OUT "world!\n"; ### ditto
224 <P>Causes $s to be set to:
226 <FONT SIZE=3 FACE="courier"><PRE>
227 "Hello, world!\n"
232 <A NAME="PUBLIC_INTERFACE"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> PUBLIC INTERFACE</H2></A>
237 <A NAME="Construction"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Construction</H3></A>
242 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:new"><A NAME="item:new_ARGS">new [ARGS...]</A></A></B></DT>
245 Return a new, unattached scalar handle.
246 If any arguments are given, they're sent to open().
248 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:open"><A NAME="item:open_SCALARREF">open [SCALARREF]</A></A></B></DT>
250 <I>Instance method.</I>
251 Open the scalar handle on a new scalar, pointed to by SCALARREF.
252 If no SCALARREF is given, a "private" scalar is created to hold
256 <P>Returns the self object on success, undefined on error.
258 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:opened">opened</A></B></DT>
260 <I>Instance method.</I>
261 Is the scalar handle opened on something?
263 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:close">close</A></B></DT>
265 <I>Instance method.</I>
266 Disassociate the scalar handle from its underlying scalar.
267 Done automatically on destroy.
274 <A NAME="Input_and_output"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Input and output</H3></A>
279 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:flush">flush</A></B></DT>
281 <I>Instance method.</I>
282 No-op, provided for OO compatibility.
284 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:getc">getc</A></B></DT>
286 <I>Instance method.</I>
287 Return the next character, or undef if none remain.
289 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:getline">getline</A></B></DT>
291 <I>Instance method.</I>
292 Return the next line, or undef on end of string.
293 Can safely be called in an array context.
294 Currently, lines are delimited by "\n".
296 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:getlines">getlines</A></B></DT>
298 <I>Instance method.</I>
299 Get all remaining lines.
300 It will croak() if accidentally called in a scalar context.
302 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:print"><A NAME="item:print_ARGS">print ARGS...</A></A></B></DT>
304 <I>Instance method.</I>
305 Print ARGS to the underlying scalar.
308 <P><B>Warning:</B> this continues to always cause a seek to the end
309 of the string, but if you perform seek()s and tell()s, it is
310 still safer to explicitly seek-to-end before subsequent print()s.
312 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:read"><A NAME="item:read_BUF_NBYTES_OFFSET">read BUF, NBYTES, [OFFSET]</A></A></B></DT>
314 <I>Instance method.</I>
315 Read some bytes from the scalar.
316 Returns the number of bytes actually read, 0 on end-of-file, undef on error.
318 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:write"><A NAME="item:write_BUF_NBYTES_OFFSET">write BUF, NBYTES, [OFFSET]</A></A></B></DT>
320 <I>Instance method.</I>
321 Write some bytes to the scalar.
323 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:sysread"><A NAME="item:sysread_BUF_LEN_OFFSET">sysread BUF, LEN, [OFFSET]</A></A></B></DT>
325 <I>Instance method.</I>
326 Read some bytes from the scalar.
327 Returns the number of bytes actually read, 0 on end-of-file, undef on error.
329 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:syswrite"><A NAME="item:syswrite_BUF_NBYTES_OFFSET">syswrite BUF, NBYTES, [OFFSET]</A></A></B></DT>
331 <I>Instance method.</I>
332 Write some bytes to the scalar.
339 <A NAME="Seeking_telling_and_other_attributes"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Seeking/telling and other attributes</H3></A>
344 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:autoflush">autoflush</A></B></DT>
346 <I>Instance method.</I>
347 No-op, provided for OO compatibility.
349 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:binmode">binmode</A></B></DT>
351 <I>Instance method.</I>
352 No-op, provided for OO compatibility.
354 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:clearerr">clearerr</A></B></DT>
356 <I>Instance method.</I> Clear the error and EOF flags. A no-op.
358 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:eof">eof</A></B></DT>
360 <I>Instance method.</I> Are we at end of file?
362 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:seek"><A NAME="item:seek_OFFSET_WHENCE">seek OFFSET, WHENCE</A></A></B></DT>
364 <I>Instance method.</I> Seek to a given position in the stream.
366 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:sysseek"><A NAME="item:sysseek_OFFSET_WHENCE">sysseek OFFSET, WHENCE</A></A></B></DT>
368 <I>Instance method.</I> Identical to <CODE>seek OFFSET, WHENCE</CODE>, <I>q.v.</I>
370 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:tell">tell</A></B></DT>
372 <I>Instance method.</I>
373 Return the current position in the stream, as a numeric offset.
375 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:setpos"><A NAME="item:setpos_POS">setpos POS</A></A></B></DT>
377 <I>Instance method.</I>
378 Set the current position, using the opaque value returned by <CODE>getpos()</CODE>.
380 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:getpos">getpos</A></B></DT>
382 <I>Instance method.</I>
383 Return the current position in the string, as an opaque object.
385 <P><DT><B><A HREF="#__TOP__"><IMG SRC="icons/itembullet.gif" ALT="Top" BORDER="0"></A> <A NAME="item:sref">sref</A></B></DT>
387 <I>Instance method.</I>
388 Return a reference to the underlying scalar.
395 <A NAME="WARNINGS"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> WARNINGS</H2></A>
398 <P>Perl's TIEHANDLE spec was incomplete prior to 5.005_57;
399 it was missing support for <CODE>seek()</CODE>, <CODE>tell()</CODE>, and <CODE>eof()</CODE>.
400 Attempting to use these functions with an IO::Scalar will not work
401 prior to 5.005_57. IO::Scalar will not have the relevant methods
402 invoked; and even worse, this kind of bug can lie dormant for a while.
403 If you turn warnings on (via <CODE>$^W</CODE> or <CODE>perl -w</CODE>),
404 and you see something like this...
406 <FONT SIZE=3 FACE="courier"><PRE>
407 attempt to seek on unopened filehandle
410 <P>...then you are probably trying to use one of these functions
411 on an IO::Scalar with an old Perl. The remedy is to simply
412 use the OO version; e.g.:
414 <FONT SIZE=3 FACE="courier"><PRE>
415 $SH->seek(0,0); ### GOOD: will work on any 5.005
416 seek($SH,0,0); ### WARNING: will only work on 5.005_57 and beyond
421 <A NAME="VERSION"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> VERSION</H2></A>
424 <P>$Id: Scalar.pm,v 2.105 2003/12/21 18:51:45 eryq Exp $
429 <A NAME="AUTHORS"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> AUTHORS</H2></A>
434 <A NAME="Principal_author"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Principal author</H3></A>
437 <P>Eryq (<I><FILE><A HREF="mailto:eryq@zeegee.com">eryq@zeegee.com</A></FILE></I>).
438 President, ZeeGee Software Inc (<I><FILE><A HREF="http://www.zeegee.com">http://www.zeegee.com</A></FILE></I>).
443 <A NAME="Other_contributors"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Other contributors</H3></A>
446 <P>The full set of contributors always includes the folks mentioned
447 in <A HREF="../IO/Stringy.pm.html#CHANGE_LOG">CHANGE LOG</A>. But just the same, special
448 thanks to the following individuals for their invaluable contributions
449 (if I've forgotten or misspelled your name, please email me!):
453 for contributing <CODE>getc()</CODE>.
456 <P><I>Brandon Browning,</I>
457 for suggesting <CODE>opened()</CODE>.
460 <P><I>David Richter,</I>
461 for finding and fixing the bug in <CODE>PRINTF()</CODE>.
464 <P><I>Eric L. Brine,</I>
465 for his offset-using read() and write() implementations.
468 <P><I>Richard Jones,</I>
469 for his patches to massively improve the performance of <CODE>getline()</CODE>
470 and add <CODE>sysread</CODE> and <CODE>syswrite</CODE>.
473 <P><I>B. K. Oxley (binkley),</I>
474 for stringification and inheritance improvements,
475 and sundry good ideas.
478 <P><I>Doug Wilson,</I>
479 for the IO::Handle inheritance and automatic tie-ing.
484 <A NAME="SEE_ALSO"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> SEE ALSO</H2></A>
487 <P><A HREF="../IO/String.pm.html">IO::String</A>, which is quite similar but which was designed
488 more-recently and with an IO::Handle-like interface in mind,
489 so you could mix OO- and native-filehandle usage without using tied().
492 <P><I>Note:</I> as of version 2.x, these classes all work like
493 their IO::Handle counterparts, so we have comparable
494 functionality to IO::String.
497 <ADDRESS><FONT SIZE=-1>
498 Generated Sun Dec 21 13:54:37 2003 by cvu_pod2html