.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "IO::Handle 3pm" .TH IO::Handle 3pm 2023-11-28 "perl v5.38.2" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME IO::Handle \- supply object methods for I/O handles .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use IO::Handle; \& \& my $io = IO::Handle\->new(); \& if ($io\->fdopen(fileno(STDIN),"r")) { \& print $io\->getline; \& $io\->close; \& } \& \& my $io = IO::Handle\->new(); \& if ($io\->fdopen(fileno(STDOUT),"w")) { \& $io\->print("Some text\en"); \& } \& \& # setvbuf is not available by default on Perls 5.8.0 and later. \& use IO::Handle \*(Aq_IOLBF\*(Aq; \& $io\->setvbuf(my $buffer_var, _IOLBF, 1024); \& \& undef $io; # automatically closes the file if it\*(Aqs open \& \& autoflush STDOUT 1; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" \&\f(CW\*(C`IO::Handle\*(C'\fR is the base class for all other IO handle classes. It is not intended that objects of \f(CW\*(C`IO::Handle\*(C'\fR would be created directly, but instead \f(CW\*(C`IO::Handle\*(C'\fR is inherited from by several other classes in the IO hierarchy. .PP If you are reading this documentation, looking for a replacement for the \f(CW\*(C`FileHandle\*(C'\fR package, then I suggest you read the documentation for \f(CW\*(C`IO::File\*(C'\fR too. .SH CONSTRUCTOR .IX Header "CONSTRUCTOR" .IP "new ()" 4 .IX Item "new ()" Creates a new \f(CW\*(C`IO::Handle\*(C'\fR object. .IP "new_from_fd ( FD, MODE )" 4 .IX Item "new_from_fd ( FD, MODE )" Creates an \f(CW\*(C`IO::Handle\*(C'\fR like \f(CW\*(C`new\*(C'\fR does. It requires two parameters, which are passed to the method \f(CW\*(C`fdopen\*(C'\fR; if the fdopen fails, the object is destroyed. Otherwise, it is returned to the caller. .SH METHODS .IX Header "METHODS" See perlfunc for complete descriptions of each of the following supported \f(CW\*(C`IO::Handle\*(C'\fR methods, which are just front ends for the corresponding built-in functions: .PP .Vb 10 \& $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 ) .Ve .PP See perlvar for complete descriptions of each of the following supported \f(CW\*(C`IO::Handle\*(C'\fR 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 \f(CW$io\fR\->autoflush will actually turn ON autoflush by default). .PP .Vb 7 \& $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]) $. .Ve .PP The following methods are not supported on a per-filehandle basis. .PP .Vb 4 \& 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] ) $\e \& \& IO::Handle\->input_record_separator( [STR] ) $/ .Ve .PP Furthermore, for doing normal I/O you might need these: .ie n .IP "$io\->fdopen ( FD, MODE )" 4 .el .IP "\f(CW$io\fR\->fdopen ( FD, MODE )" 4 .IX Item "$io->fdopen ( FD, MODE )" \&\f(CW\*(C`fdopen\*(C'\fR is like an ordinary \f(CW\*(C`open\*(C'\fR except that its first parameter is not a filename but rather a file handle name, an IO::Handle object, or a file descriptor number. (For the documentation of the \f(CW\*(C`open\*(C'\fR method, see IO::File.) .ie n .IP $io\->opened 4 .el .IP \f(CW$io\fR\->opened 4 .IX Item "$io->opened" Returns true if the object is currently a valid file descriptor, false otherwise. .ie n .IP $io\->getline 4 .el .IP \f(CW$io\fR\->getline 4 .IX Item "$io->getline" This works like <$io> described in "I/O Operators" in perlop except that it's more readable and can be safely called in a list context but still returns just one line. If used as the conditional within a \f(CW\*(C`while\*(C'\fR or C\-style \f(CW\*(C`for\*(C'\fR loop, however, you will need to emulate the functionality of <$io> with \f(CW\*(C`defined($_ = $io\->getline)\*(C'\fR. .ie n .IP $io\->getlines 4 .el .IP \f(CW$io\fR\->getlines 4 .IX Item "$io->getlines" This works like <$io> when called in a list context to read all the remaining lines in a file, except that it's more readable. It will also \fBcroak()\fR if accidentally called in a scalar context. .ie n .IP "$io\->ungetc ( ORD )" 4 .el .IP "\f(CW$io\fR\->ungetc ( ORD )" 4 .IX Item "$io->ungetc ( ORD )" 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. .ie n .IP "$io\->write ( BUF, LEN [, OFFSET ] )" 4 .el .IP "\f(CW$io\fR\->write ( BUF, LEN [, OFFSET ] )" 4 .IX Item "$io->write ( BUF, LEN [, OFFSET ] )" This \f(CW\*(C`write\*(C'\fR is somewhat like \f(CW\*(C`write\*(C'\fR found in C, in that it is the opposite of read. The wrapper for the perl \f(CW\*(C`write\*(C'\fR function is called \f(CW\*(C`format_write\*(C'\fR. However, whilst the C \f(CW\*(C`write\*(C'\fR function returns the number of bytes written, this \f(CW\*(C`write\*(C'\fR function simply returns true if successful (like \f(CW\*(C`print\*(C'\fR). A more C\-like \f(CW\*(C`write\*(C'\fR is \f(CW\*(C`syswrite\*(C'\fR (see above). .ie n .IP $io\->error 4 .el .IP \f(CW$io\fR\->error 4 .IX Item "$io->error" Returns a true value if the given handle has experienced any errors since it was opened or since the last call to \f(CW\*(C`clearerr\*(C'\fR, or if the handle is invalid. It only returns false for a valid handle with no outstanding errors. .ie n .IP $io\->clearerr 4 .el .IP \f(CW$io\fR\->clearerr 4 .IX Item "$io->clearerr" Clear the given handle's error indicator. Returns \-1 if the handle is invalid, 0 otherwise. .ie n .IP $io\->sync 4 .el .IP \f(CW$io\fR\->sync 4 .IX Item "$io->sync" \&\f(CW\*(C`sync\*(C'\fR synchronizes a file's in-memory state with that on the physical medium. \f(CW\*(C`sync\*(C'\fR does not operate at the perlio api level, but operates on the file descriptor (similar to sysread, sysseek and systell). 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. \f(CW\*(C`sync\*(C'\fR is not implemented on all platforms. Returns "0 but true" on success, \f(CW\*(C`undef\*(C'\fR on error, \f(CW\*(C`undef\*(C'\fR for an invalid handle. See \fBfsync\fR\|(3c). .ie n .IP $io\->flush 4 .el .IP \f(CW$io\fR\->flush 4 .IX Item "$io->flush" \&\f(CW\*(C`flush\*(C'\fR 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. Returns "0 but true" on success, \f(CW\*(C`undef\*(C'\fR on error. .ie n .IP "$io\->printflush ( ARGS )" 4 .el .IP "\f(CW$io\fR\->printflush ( ARGS )" 4 .IX Item "$io->printflush ( ARGS )" Turns on autoflush, print ARGS and then restores the autoflush status of the \&\f(CW\*(C`IO::Handle\*(C'\fR object. Returns the return value from print. .ie n .IP "$io\->blocking ( [ BOOL ] )" 4 .el .IP "\f(CW$io\fR\->blocking ( [ BOOL ] )" 4 .IX Item "$io->blocking ( [ BOOL ] )" If called with an argument \f(CW\*(C`blocking\*(C'\fR will turn on non-blocking IO if \&\f(CW\*(C`BOOL\*(C'\fR is false, and turn it off if \f(CW\*(C`BOOL\*(C'\fR is true. .Sp \&\f(CW\*(C`blocking\*(C'\fR will return the value of the previous setting, or the current setting if \f(CW\*(C`BOOL\*(C'\fR is not given. .Sp If an error occurs \f(CW\*(C`blocking\*(C'\fR will return undef and \f(CW$!\fR will be set. .PP If the C functions \fBsetbuf()\fR and/or \fBsetvbuf()\fR are available, then \&\f(CW\*(C`IO::Handle::setbuf\*(C'\fR and \f(CW\*(C`IO::Handle::setvbuf\*(C'\fR 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 \f(CW\*(C`_IOFBF\*(C'\fR, \&\f(CW\*(C`_IOLBF\*(C'\fR, and \f(CW\*(C`_IONBF\*(C'\fR for \fBsetvbuf()\fR\-\-except that the buffer parameter specifies a scalar variable to use as a buffer. You should only change the buffer before any I/O, or immediately after calling flush. .PP WARNING: The \fBIO::Handle::setvbuf()\fR is not available by default on Perls 5.8.0 and later because \fBsetvbuf()\fR is rather specific to using the stdio library, while Perl prefers the new perlio subsystem instead. .PP WARNING: A variable used as a buffer by \f(CW\*(C`setbuf\*(C'\fR or \f(CW\*(C`setvbuf\*(C'\fR \fBmust not be modified\fR in any way until the IO::Handle is closed or \f(CW\*(C`setbuf\*(C'\fR or \&\f(CW\*(C`setvbuf\*(C'\fR is called again, or memory corruption may result! Remember that the order of global destruction is undefined, so even if your buffer variable remains in scope until program termination, it may be undefined before the file IO::Handle is closed. Note that you need to import the constants \f(CW\*(C`_IOFBF\*(C'\fR, \f(CW\*(C`_IOLBF\*(C'\fR, and \f(CW\*(C`_IONBF\*(C'\fR explicitly. Like C, setbuf returns nothing. setvbuf returns "0 but true", on success, \f(CW\*(C`undef\*(C'\fR on failure. .PP Lastly, there is a special method for working under \fB\-T\fR and setuid/gid scripts: .ie n .IP $io\->untaint 4 .el .IP \f(CW$io\fR\->untaint 4 .IX Item "$io->untaint" 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. Returns 0 on success, \-1 if setting the taint-clean flag failed. (eg invalid handle) .SH NOTE .IX Header "NOTE" An \f(CW\*(C`IO::Handle\*(C'\fR object is a reference to a symbol/GLOB reference (see the Symbol package). Some modules that inherit from \f(CW\*(C`IO::Handle\*(C'\fR 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 \f(CW\*(C`timeout\*(C'\fR variable in 'io_socket_timeout'. .SH "SEE ALSO" .IX Header "SEE ALSO" perlfunc, "I/O Operators" in perlop, IO::File .SH BUGS .IX Header "BUGS" Due to backwards compatibility, all filehandles resemble objects of class \f(CW\*(C`IO::Handle\*(C'\fR, or actually classes derived from that class. They actually aren't. Which means you can't derive your own class from \f(CW\*(C`IO::Handle\*(C'\fR and inherit those methods. .SH HISTORY .IX Header "HISTORY" Derived from FileHandle.pm by Graham Barr <\fIgbarr@pobox.com\fR>