MAN page from CentOS 8 perl-IO-1.40-452.module_el8.4.0+646+45e06e4a.x86_64.rpm


Section: Perl Programmers Reference Guide (3pm)
Updated: 2019-10-24


IO::Handle - supply object methods for I/O handles 


    use IO::Handle;    $io = IO::Handle->new();    if ($io->fdopen(fileno(STDIN),"r")) {        print $io->getline;        $io->close;    }    $io = IO::Handle->new();    if ($io->fdopen(fileno(STDOUT),"w")) {        $io->print("Some text\n");    }    # setvbuf is not available by default on Perls 5.8.0 and later.    use IO::Handle '_IOLBF';    $io->setvbuf($buffer_var, _IOLBF, 1024);    undef $io;       # automatically closes the file if it's open    autoflush STDOUT 1;


"IO::Handle" is the base class for all other IO handle classes. It isnot intended that objects of "IO::Handle" would be created directly,but instead "IO::Handle" is inherited from by several other classesin the IO hierarchy.

If you are reading this documentation, looking for a replacement forthe "FileHandle" package, then I suggest you read the documentationfor "IO::File" too. 


new ()
Creates a new "IO::Handle" object.
new_from_fd ( FD, MODE )
Creates an "IO::Handle" like "new" does.It requires two parameters, which are passed to the method "fdopen";if the fdopen fails, the object is destroyed. Otherwise, it is returnedto the caller.


See perlfunc for complete descriptions of each of the followingsupported "IO::Handle" methods, which are just front ends for thecorresponding built-in functions:

    $io->close    $io->eof    $io->fcntl( FUNCTION, SCALAR )    $io->fileno    $io->format_write( [FORMAT_NAME] )    $io->getc    $io->ioctl( FUNCTION, SCALAR )    $io->read ( BUF, LEN, [OFFSET] )    $io->print ( ARGS )    $io->printf ( FMT, [ARGS] )    $io->say ( ARGS )    $io->stat    $io->sysread ( BUF, LEN, [OFFSET] )    $io->syswrite ( BUF, [LEN, [OFFSET]] )    $io->truncate ( LEN )

See perlvar for complete descriptions of each of the followingsupported "IO::Handle" methods. All of them return the previousvalue of the attribute and takes an optional single argument that whengiven will set the value. If no argument is given the previous valueis unchanged (except for $io->autoflush will actually turn ONautoflush by default).

    $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])                    $.

The following methods are not supported on a per-filehandle basis.

    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] )      $\    IO::Handle->input_record_separator( [STR] )       $/

Furthermore, for doing normal I/O you might need these:

$io->fdopen ( FD, MODE )
"fdopen" is like an ordinary "open" except that its first parameteris not a filename but rather a file handle name, an IO::Handle object,or a file descriptor number. (For the documentation of the "open"method, see IO::File.)
Returns true if the object is currently a valid file descriptor, falseotherwise.
This works like <$io> described in ``I/O Operators'' in perlopexcept that it's more readable and can be safely called in alist context but still returns just one line. If used as the conditionalwithin a "while" or C-style "for" loop, however, you will need toemulate the functionality of <$io> with "defined($_ = $io->getline)".
This works like <$io> when called in a list context to read allthe remaining lines in a file, except that it's more readable.It will also croak() if accidentally called in a scalar context.
$io->ungetc ( ORD )
Pushes a character with the given ordinal value back onto the givenhandle's input stream. Only one character of pushback per handle isguaranteed.
$io->write ( BUF, LEN [, OFFSET ] )
This "write" is somewhat like "write" found in C, in that it is theopposite of read. The wrapper for the perl "write" function iscalled "format_write". However, whilst the C "write" function returnsthe number of bytes written, this "write" function simply returns trueif successful (like "print"). A more C-like "write" is "syswrite"(see above).
Returns a true value if the given handle has experienced any errorssince it was opened or since the last call to "clearerr", or if thehandle is invalid. It only returns false for a valid handle with nooutstanding errors.
Clear the given handle's error indicator. Returns -1 if the handle isinvalid, 0 otherwise.
"sync" synchronizes a file's in-memory state with that on thephysical medium. "sync" does not operate at the perlio api level, butoperates on the file descriptor (similar to sysread, sysseek andsystell). This means that any data held at the perlio api level will notbe synchronized. To synchronize data that is buffered at the perlio apilevel you must use the flush method. "sync" is not implemented on allplatforms. Returns ``0 but true'' on success, "undef" on error, "undef"for an invalid handle. See fsync(3c).
"flush" causes perl to flush any buffered data at the perlio api level.Any unread data in the buffer will be discarded, and any unwritten datawill be written to the underlying file descriptor. Returns ``0 but true''on success, "undef" on error.
$io->printflush ( ARGS )
Turns on autoflush, print ARGS and then restores the autoflush status of the"IO::Handle" object. Returns the return value from print.
$io->blocking ( [ BOOL ] )
If called with an argument "blocking" will turn on non-blocking IO if"BOOL" is false, and turn it off if "BOOL" is true.

"blocking" will return the value of the previous setting, or thecurrent setting if "BOOL" is not given.

If an error occurs "blocking" will return undef and $! will be set.

If the C functions setbuf() and/or setvbuf() are available, then"IO::Handle::setbuf" and "IO::Handle::setvbuf" set the bufferingpolicy for an IO::Handle. The calling sequences for the Perl functionsare the same as their C counterparts---including the constants "_IOFBF","_IOLBF", and "_IONBF" for setvbuf()--except that the buffer parameterspecifies a scalar variable to use as a buffer. You should onlychange the buffer before any I/O, or immediately after calling flush.

WARNING: The IO::Handle::setvbuf() is not available by default onPerls 5.8.0 and later because setvbuf() is rather specific to usingthe stdio library, while Perl prefers the new perlio subsystem instead.

WARNING: A variable used as a buffer by "setbuf" or "setvbuf" must notbe modified in any way until the IO::Handle is closed or "setbuf" or"setvbuf" is called again, or memory corruption may result! Remember thatthe order of global destruction is undefined, so even if your buffervariable remains in scope until program termination, it may be undefinedbefore the file IO::Handle is closed. Note that you need to import theconstants "_IOFBF", "_IOLBF", and "_IONBF" explicitly. Like C, setbufreturns nothing. setvbuf returns ``0 but true'', on success, "undef" onfailure.

Lastly, there is a special method for working under -T and setuid/gidscripts:

Marks the object as taint-clean, and as such data read from it will alsobe considered taint-clean. Note that this is a very trusting action totake, and appropriate consideration for the data source and potentialvulnerability should be kept in mind. Returns 0 on success, -1 if settingthe taint-clean flag failed. (eg invalid handle)


An "IO::Handle" object is a reference to a symbol/GLOB reference (seethe "Symbol" package). Some modules thatinherit from "IO::Handle" may want to keep object related variablesin the hash table part of the GLOB. In an attempt to prevent modulestrampling on each other I propose the that any such module should prefixits variables with its own name separated by _'s. For example the IO::Socketmodule keeps a "timeout" variable in 'io_socket_timeout'. 


perlfunc, ``I/O Operators'' in perlop,IO::File 


Due to backwards compatibility, all filehandles resemble objectsof class "IO::Handle", or actually classes derived from that class.They actually aren't. Which means you can't derive your own class from "IO::Handle" and inherit those methods. 


Derived from by Graham Barr <>




This document was created byman2html,using the manual pages.