?? handle.pm
字號:
package IO::Handle;=head1 NAMEIO::Handle - supply object methods for I/O handles=head1 SYNOPSIS use IO::Handle; $io = new IO::Handle; if ($io->fdopen(fileno(STDIN),"r")) { print $io->getline; $io->close; } $io = new IO::Handle; if ($io->fdopen(fileno(STDOUT),"w")) { $io->print("Some text\n"); } use IO::Handle '_IOLBF'; $io->setvbuf($buffer_var, _IOLBF, 1024); undef $io; # automatically closes the file if it's open autoflush STDOUT 1;=head1 DESCRIPTIONC<IO::Handle> is the base class for all other IO handle classes. It isnot intended that objects of C<IO::Handle> would be created directly,but instead C<IO::Handle> is inherited from by several other classesin the IO hierarchy.If you are reading this documentation, looking for a replacement forthe C<FileHandle> package, then I suggest you read the documentationfor C<IO::File> too.=head1 CONSTRUCTOR=over 4=item new ()Creates a new C<IO::Handle> object.=item new_from_fd ( FD, MODE )Creates a C<IO::Handle> like C<new> does.It requires two parameters, which are passed to the method C<fdopen>;if the fdopen fails, the object is destroyed. Otherwise, it is returnedto the caller.=back=head1 METHODSSee L<perlfunc> for complete descriptions of each of the followingsupported C<IO::Handle> methods, which are just front ends for thecorresponding built-in functions: $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 )See L<perlvar> for complete descriptions of each of the followingsupported C<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:=over =item $io->fdopen ( FD, MODE )C<fdopen> is like an ordinary C<open> except that its first parameteris not a filename but rather a file handle name, a IO::Handle object,or a file descriptor number.=item $io->openedReturns true if the object is currently a valid file descriptor, falseotherwise.=item $io->getlineThis works like <$io> described in L<perlop/"I/O Operators">except that it's more readable and can be safely called in alist context but still returns just one line.=item $io->getlinesThis 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.=item $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.=item $io->write ( BUF, LEN [, OFFSET ] )This C<write> is like C<write> found in C, that is it is theopposite of read. The wrapper for the perl C<write> function iscalled C<format_write>.=item $io->errorReturns a true value if the given handle has experienced any errorssince it was opened or since the last call to C<clearerr>, or if thehandle is invalid. It only returns false for a valid handle with nooutstanding errors.=item $io->clearerrClear the given handle's error indicator. Returns -1 if the handle isinvalid, 0 otherwise.=item $io->syncC<sync> synchronizes a file's in-memory state with that on thephysical medium. C<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. C<sync> is not implemented on allplatforms. Returns "0 but true" on success, C<undef> on error, C<undef>for an invalid handle. See L<fsync(3c)>.=item $io->flushC<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, C<undef> on error.=item $io->printflush ( ARGS )Turns on autoflush, print ARGS and then restores the autoflush status of theC<IO::Handle> object. Returns the return value from print.=item $io->blocking ( [ BOOL ] )If called with an argument C<blocking> will turn on non-blocking IO ifC<BOOL> is false, and turn it off if C<BOOL> is true.C<blocking> will return the value of the previous setting, or thecurrent setting if C<BOOL> is not given. If an error occurs C<blocking> will return undef and C<$!> will be set.=backIf the C functions setbuf() and/or setvbuf() are available, thenC<IO::Handle::setbuf> and C<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 C<_IOFBF>,C<_IOLBF>, and C<_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: A variable used as a buffer by C<setbuf> or C<setvbuf> B<must notbe modified> in any way until the IO::Handle is closed or C<setbuf> orC<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 C<_IOFBF>, C<_IOLBF>, and C<_IONBF> explicitly. Like C, setbufreturns nothing. setvbuf returns "0 but true", on success, C<undef> onfailure.Lastly, there is a special method for working under B<-T> and setuid/gidscripts:=over=item $io->untaintMarks 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)=back=head1 NOTEA C<IO::Handle> object is a reference to a symbol/GLOB reference (seethe C<Symbol> package). Some modules thatinherit from C<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 C<timeout> variable in 'io_socket_timeout'.=head1 SEE ALSOL<perlfunc>, L<perlop/"I/O Operators">,L<IO::File>=head1 BUGSDue to backwards compatibility, all filehandles resemble objectsof class C<IO::Handle>, or actually classes derived from that class.They actually aren't. Which means you can't derive your own class from C<IO::Handle> and inherit those methods.=head1 HISTORYDerived from FileHandle.pm by Graham Barr E<lt>F<gbarr@pobox.com>E<gt>=cutrequire 5.005_64;use strict;our($VERSION, @EXPORT_OK, @ISA);use Carp;use Symbol;use SelectSaver;use IO (); # Load the XS modulerequire Exporter;@ISA = qw(Exporter);$VERSION = "1.21";@EXPORT_OK = qw( autoflush output_field_separator output_record_separator input_record_separator input_line_number format_page_number format_lines_per_page format_lines_left format_name format_top_name format_line_break_characters format_formfeed format_write print printf getline getlines printflush flush SEEK_SET SEEK_CUR SEEK_END _IOFBF _IOLBF _IONBF);################################################## Constructors, destructors.##sub new { my $class = ref($_[0]) || $_[0] || "IO::Handle"; @_ == 1 or croak "usage: new $class"; my $io = gensym; bless $io, $class;}sub new_from_fd { my $class = ref($_[0]) || $_[0] || "IO::Handle"; @_ == 3 or croak "usage: new_from_fd $class FD, MODE";
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -