home *** CD-ROM | disk | FTP | other *** search
Wrap
<HTML> <HEAD> <TITLE>IO::Handle - supply object methods for I/O handles</TITLE> <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css"> <LINK REV="made" HREF="mailto:"> </HEAD> <BODY> <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%> <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc"> <STRONG><P CLASS=block> IO::Handle - supply object methods for I/O handles</P></STRONG> </TD></TR> </TABLE> <A NAME="__index__"></A> <!-- INDEX BEGIN --> <UL> <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI> <LI><A HREF="#synopsis">SYNOPSIS</A></LI> <LI><A HREF="#description">DESCRIPTION</A></LI> <LI><A HREF="#constructor">CONSTRUCTOR</A></LI> <LI><A HREF="#methods">METHODS</A></LI> <LI><A HREF="#note">NOTE</A></LI> <LI><A HREF="#see also">SEE ALSO</A></LI> <LI><A HREF="#bugs">BUGS</A></LI> <LI><A HREF="#history">HISTORY</A></LI> </UL> <!-- INDEX END --> <HR> <P> <H1><A NAME="name">NAME</A></H1> <P>IO::Handle - supply object methods for I/O handles</P> <P> <HR> <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1> <UL> <LI>Linux</LI> <LI>Solaris</LI> <LI>Windows</LI> </UL> <HR> <H1><A NAME="synopsis">SYNOPSIS</A></H1> <PRE> use IO::Handle;</PRE> <PRE> $io = new IO::Handle; if ($io->fdopen(fileno(STDIN),"r")) { print $io->getline; $io->close; }</PRE> <PRE> $io = new IO::Handle; if ($io->fdopen(fileno(STDOUT),"w")) { $io->print("Some text\n"); }</PRE> <PRE> use IO::Handle '_IOLBF'; $io->setvbuf($buffer_var, _IOLBF, 1024);</PRE> <PRE> undef $io; # automatically closes the file if it's open</PRE> <PRE> autoflush STDOUT 1;</PRE> <P> <HR> <H1><A NAME="description">DESCRIPTION</A></H1> <P><CODE>IO::Handle</CODE> is the base class for all other IO handle classes. It is not intended that objects of <CODE>IO::Handle</CODE> would be created directly, but instead <CODE>IO::Handle</CODE> is inherited from by several other classes in the IO hierarchy.</P> <P>If you are reading this documentation, looking for a replacement for the <CODE>FileHandle</CODE> package, then I suggest you read the documentation for <CODE>IO::File</CODE> too.</P> <P> <HR> <H1><A NAME="constructor">CONSTRUCTOR</A></H1> <DL> <DT><STRONG><A NAME="item_new">new ()</A></STRONG><BR> <DD> Creates a new <CODE>IO::Handle</CODE> object. <P></P> <DT><STRONG><A NAME="item_new_from_fd">new_from_fd ( FD, MODE )</A></STRONG><BR> <DD> Creates a <CODE>IO::Handle</CODE> like <A HREF="#item_new"><CODE>new</CODE></A> does. It requires two parameters, which are passed to the method <A HREF="#item_fdopen"><CODE>fdopen</CODE></A>; if the fdopen fails, the object is destroyed. Otherwise, it is returned to the caller. <P></P></DL> <P> <HR> <H1><A NAME="methods">METHODS</A></H1> <P>See <A HREF="../../lib/Pod/perlfunc.html">the perlfunc manpage</A> for complete descriptions of each of the following supported <CODE>IO::Handle</CODE> methods, which are just front ends for the corresponding built-in functions:</P> <PRE> $io->close $io->eof $io->fileno $io->format_write( [FORMAT_NAME] ) $io->getc $io->read ( BUF, LEN, [OFFSET] ) $io->print ( ARGS ) $io->printf ( FMT, [ARGS] ) $io->stat $io->sysread ( BUF, LEN, [OFFSET] ) $io->syswrite ( BUF, LEN, [OFFSET] ) $io->truncate ( LEN )</PRE> <P>See <A HREF="../../lib/Pod/perlvar.html">the perlvar manpage</A> for complete descriptions of each of the following supported <CODE>IO::Handle</CODE> methods. All of them return the previous value of the attribute and takes an optional single argument that when given will set the value. If no argument is given the previous value is unchanged (except for $io->autoflush will actually turn ON autoflush by default).</P> <PRE> $io->autoflush ( [BOOL] ) $| $io->format_page_number( [NUM] ) $% $io->format_lines_per_page( [NUM] ) $= $io->format_lines_left( [NUM] ) $- $io->format_name( [STR] ) $~ $io->format_top_name( [STR] ) $^ $io->input_line_number( [NUM]) $.</PRE> <P>The following methods are not supported on a per-filehandle basis.</P> <PRE> IO::Handle->format_line_break_characters( [STR] ) $: IO::Handle->format_formfeed( [STR]) $^L IO::Handle->output_field_separator( [STR] ) $, IO::Handle->output_record_separator( [STR] ) $\</PRE> <PRE> IO::Handle->input_record_separator( [STR] ) $/</PRE> <P>Furthermore, for doing normal I/O you might need these:</P> <DL> <DT><STRONG><A NAME="item_fdopen">$io->fdopen ( FD, MODE )</A></STRONG><BR> <DD> <A HREF="#item_fdopen"><CODE>fdopen</CODE></A> is like an ordinary <A HREF="../../lib/Pod/perlfunc.html#item_open"><CODE>open</CODE></A> except that its first parameter is not a filename but rather a file handle name, a IO::Handle object, or a file descriptor number. <P></P> <DT><STRONG><A NAME="item_opened">$io->opened</A></STRONG><BR> <DD> Returns true if the object is currently a valid file descriptor. <P></P> <DT><STRONG><A NAME="item_getline">$io->getline</A></STRONG><BR> <DD> This works like <$io> described in <A HREF="../../lib/Pod/perlop.html#i/o operators">I/O Operators in the perlop manpage</A> except that it's more readable and can be safely called in an array context but still returns just one line. <P></P> <DT><STRONG><A NAME="item_getlines">$io->getlines</A></STRONG><BR> <DD> This works like <$io> when called in an array context to read all the remaining lines in a file, except that it's more readable. It will also <CODE>croak()</CODE> if accidentally called in a scalar context. <P></P> <DT><STRONG><A NAME="item_ungetc">$io->ungetc ( ORD )</A></STRONG><BR> <DD> Pushes a character with the given ordinal value back onto the given handle's input stream. Only one character of pushback per handle is guaranteed. <P></P> <DT><STRONG><A NAME="item_write">$io->write ( BUF, LEN [, OFFSET ] )</A></STRONG><BR> <DD> This <A HREF="#item_write"><CODE>write</CODE></A> is like <A HREF="#item_write"><CODE>write</CODE></A> found in C, that is it is the opposite of read. The wrapper for the perl <A HREF="#item_write"><CODE>write</CODE></A> function is called <CODE>format_write</CODE>. <P></P> <DT><STRONG><A NAME="item_error">$io->error</A></STRONG><BR> <DD> Returns a true value if the given handle has experienced any errors since it was opened or since the last call to <A HREF="#item_clearerr"><CODE>clearerr</CODE></A>. <P></P> <DT><STRONG><A NAME="item_clearerr">$io->clearerr</A></STRONG><BR> <DD> Clear the given handle's error indicator. <P></P> <DT><STRONG><A NAME="item_sync">$io->sync</A></STRONG><BR> <DD> <A HREF="#item_sync"><CODE>sync</CODE></A> synchronizes a file's in-memory state with that on the physical medium. <A HREF="#item_sync"><CODE>sync</CODE></A> does not operate at the perlio api level, but operates on the file descriptor, this means that any data held at the perlio api level will not be synchronized. To synchronize data that is buffered at the perlio api level you must use the flush method. <A HREF="#item_sync"><CODE>sync</CODE></A> is not implemented on all platforms. See <EM>fsync(3c)</EM>. <P></P> <DT><STRONG><A NAME="item_flush">$io->flush</A></STRONG><BR> <DD> <A HREF="#item_flush"><CODE>flush</CODE></A> causes perl to flush any buffered data at the perlio api level. Any unread data in the buffer will be discarded, and any unwritten data will be written to the underlying file descriptor. <P></P> <DT><STRONG><A NAME="item_printflush">$io->printflush ( ARGS )</A></STRONG><BR> <DD> Turns on autoflush, print ARGS and then restores the autoflush status of the <CODE>IO::Handle</CODE> object. <P></P> <DT><STRONG><A NAME="item_blocking">$io->blocking ( [ BOOL ] )</A></STRONG><BR> <DD> If called with an argument <A HREF="#item_blocking"><CODE>blocking</CODE></A> will turn on non-blocking IO if <CODE>BOOL</CODE> is false, and turn it off if <CODE>BOOL</CODE> is true. <P><A HREF="#item_blocking"><CODE>blocking</CODE></A> will return the value of the previous setting, or the current setting if <CODE>BOOL</CODE> is not given.</P> <P>If an error occurs <A HREF="#item_blocking"><CODE>blocking</CODE></A> will return undef and <CODE>$!</CODE> will be set.</P> <P></P></DL> <P>If the C functions <CODE>setbuf()</CODE> and/or <CODE>setvbuf()</CODE> are available, then <CODE>IO::Handle::setbuf</CODE> and <CODE>IO::Handle::setvbuf</CODE> set the buffering policy for an IO::Handle. The calling sequences for the Perl functions are the same as their C counterparts--including the constants <CODE>_IOFBF</CODE>, <CODE>_IOLBF</CODE>, and <CODE>_IONBF</CODE> for setvbuf()--except that the buffer parameter specifies a scalar variable to use as a buffer. WARNING: A variable used as a buffer by <CODE>setbuf</CODE> or <CODE>setvbuf</CODE> must not be modified in any way until the IO::Handle is closed or <CODE>setbuf</CODE> or <CODE>setvbuf</CODE> is called again, or memory corruption may result! Note that you need to import the constants <CODE>_IOFBF</CODE>, <CODE>_IOLBF</CODE>, and <CODE>_IONBF</CODE> explicitly.</P> <P>Lastly, there is a special method for working under <STRONG>-T</STRONG> and setuid/gid scripts:</P> <DL> <DT><STRONG><A NAME="item_untaint">$io->untaint</A></STRONG><BR> <DD> Marks the object as taint-clean, and as such data read from it will also be considered taint-clean. Note that this is a very trusting action to take, and appropriate consideration for the data source and potential vulnerability should be kept in mind. <P></P></DL> <P> <HR> <H1><A NAME="note">NOTE</A></H1> <P>A <CODE>IO::Handle</CODE> object is a reference to a symbol/GLOB reference (see the <CODE>Symbol</CODE> package). Some modules that inherit from <CODE>IO::Handle</CODE> may want to keep object related variables in the hash table part of the GLOB. In an attempt to prevent modules trampling on each other I propose the that any such module should prefix its variables with its own name separated by _'s. For example the IO::Socket module keeps a <CODE>timeout</CODE> variable in 'io_socket_timeout'.</P> <P> <HR> <H1><A NAME="see also">SEE ALSO</A></H1> <P><A HREF="../../lib/Pod/perlfunc.html">the perlfunc manpage</A>, <A HREF="../../lib/Pod/perlop.html#i/o operators">I/O Operators in the perlop manpage</A>, <A HREF="../../lib/IO/File.html">the IO::File manpage</A></P> <P> <HR> <H1><A NAME="bugs">BUGS</A></H1> <P>Due to backwards compatibility, all filehandles resemble objects of class <CODE>IO::Handle</CODE>, or actually classes derived from that class. They actually aren't. Which means you can't derive your own class from <CODE>IO::Handle</CODE> and inherit those methods.</P> <P> <HR> <H1><A NAME="history">HISTORY</A></H1> <P>Derived from FileHandle.pm by Graham Barr <<EM><A HREF="mailto:gbarr@pobox.com">gbarr@pobox.com</A></EM>></P> <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%> <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc"> <STRONG><P CLASS=block> IO::Handle - supply object methods for I/O handles</P></STRONG> </TD></TR> </TABLE> </BODY> </HTML>