home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / IO / Handle.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  11.7 KB  |  520 lines

  1.  
  2. package IO::Handle;
  3.  
  4. =head1 NAME
  5.  
  6. IO::Handle - supply object methods for I/O handles
  7.  
  8. =head1 SYNOPSIS
  9.  
  10.     use IO::Handle;
  11.  
  12.     $fh = new IO::Handle;
  13.     if ($fh->fdopen(fileno(STDIN),"r")) {
  14.         print $fh->getline;
  15.         $fh->close;
  16.     }
  17.  
  18.     $fh = new IO::Handle;
  19.     if ($fh->fdopen(fileno(STDOUT),"w")) {
  20.         $fh->print("Some text\n");
  21.     }
  22.  
  23.     use IO::Handle '_IOLBF';
  24.     $fh->setvbuf($buffer_var, _IOLBF, 1024);
  25.  
  26.     undef $fh;       # automatically closes the file if it's open
  27.  
  28.     autoflush STDOUT 1;
  29.  
  30. =head1 DESCRIPTION
  31.  
  32. C<IO::Handle> is the base class for all other IO handle classes. It is
  33. not intended that objects of C<IO::Handle> would be created directly,
  34. but instead C<IO::Handle> is inherited from by several other classes
  35. in the IO hierarchy.
  36.  
  37. If you are reading this documentation, looking for a replacement for
  38. the C<FileHandle> package, then I suggest you read the documentation
  39. for C<IO::File>
  40.  
  41. A C<IO::Handle> object is a reference to a symbol (see the C<Symbol> package)
  42.  
  43. =head1 CONSTRUCTOR
  44.  
  45. =over 4
  46.  
  47. =item new ()
  48.  
  49. Creates a new C<IO::Handle> object.
  50.  
  51. =item new_from_fd ( FD, MODE )
  52.  
  53. Creates a C<IO::Handle> like C<new> does.
  54. It requires two parameters, which are passed to the method C<fdopen>;
  55. if the fdopen fails, the object is destroyed. Otherwise, it is returned
  56. to the caller.
  57.  
  58. =back
  59.  
  60. =head1 METHODS
  61.  
  62. See L<perlfunc> for complete descriptions of each of the following
  63. supported C<IO::Handle> methods, which are just front ends for the
  64. corresponding built-in functions:
  65.  
  66.     close
  67.     fileno
  68.     getc
  69.     eof
  70.     read
  71.     truncate
  72.     stat
  73.     print
  74.     printf
  75.     sysread
  76.     syswrite
  77.  
  78. See L<perlvar> for complete descriptions of each of the following
  79. supported C<IO::Handle> methods:
  80.  
  81.     autoflush
  82.     output_field_separator
  83.     output_record_separator
  84.     input_record_separator
  85.     input_line_number
  86.     format_page_number
  87.     format_lines_per_page
  88.     format_lines_left
  89.     format_name
  90.     format_top_name
  91.     format_line_break_characters
  92.     format_formfeed
  93.     format_write
  94.  
  95. Furthermore, for doing normal I/O you might need these:
  96.  
  97. =over 
  98.  
  99. =item $fh->fdopen ( FD, MODE )
  100.  
  101. C<fdopen> is like an ordinary C<open> except that its first parameter
  102. is not a filename but rather a file handle name, a IO::Handle object,
  103. or a file descriptor number.
  104.  
  105. =item $fh->opened
  106.  
  107. Returns true if the object is currently a valid file descriptor.
  108.  
  109. =item $fh->getline
  110.  
  111. This works like <$fh> described in L<perlop/"I/O Operators">
  112. except that it's more readable and can be safely called in an
  113. array context but still returns just one line.
  114.  
  115. =item $fh->getlines
  116.  
  117. This works like <$fh> when called in an array context to
  118. read all the remaining lines in a file, except that it's more readable.
  119. It will also croak() if accidentally called in a scalar context.
  120.  
  121. =item $fh->ungetc ( ORD )
  122.  
  123. Pushes a character with the given ordinal value back onto the given
  124. handle's input stream.
  125.  
  126. =item $fh->write ( BUF, LEN [, OFFSET }\] )
  127.  
  128. This C<write> is like C<write> found in C, that is it is the
  129. opposite of read. The wrapper for the perl C<write> function is
  130. called C<format_write>.
  131.  
  132. =item $fh->flush
  133.  
  134. Flush the given handle's buffer.
  135.  
  136. =item $fh->error
  137.  
  138. Returns a true value if the given handle has experienced any errors
  139. since it was opened or since the last call to C<clearerr>.
  140.  
  141. =item $fh->clearerr
  142.  
  143. Clear the given handle's error indicator.
  144.  
  145. =back
  146.  
  147. If the C functions setbuf() and/or setvbuf() are available, then
  148. C<IO::Handle::setbuf> and C<IO::Handle::setvbuf> set the buffering
  149. policy for an IO::Handle.  The calling sequences for the Perl functions
  150. are the same as their C counterparts--including the constants C<_IOFBF>,
  151. C<_IOLBF>, and C<_IONBF> for setvbuf()--except that the buffer parameter
  152. specifies a scalar variable to use as a buffer.  WARNING: A variable
  153. used as a buffer by C<setbuf> or C<setvbuf> must not be modified in any
  154. way until the IO::Handle is closed or C<setbuf> or C<setvbuf> is called
  155. again, or memory corruption may result!  Note that you need to import
  156. the constants C<_IOFBF>, C<_IOLBF>, and C<_IONBF> explicitly.
  157.  
  158. Lastly, there is a special method for working under B<-T> and setuid/gid
  159. scripts:
  160.  
  161. =over
  162.  
  163. =item $fh->untaint
  164.  
  165. Marks the object as taint-clean, and as such data read from it will also
  166. be considered taint-clean. Note that this is a very trusting action to
  167. take, and appropriate consideration for the data source and potential
  168. vulnerability should be kept in mind.
  169.  
  170. =back
  171.  
  172. =head1 NOTE
  173.  
  174. A C<IO::Handle> object is a GLOB reference. Some modules that
  175. inherit from C<IO::Handle> may want to keep object related variables
  176. in the hash table part of the GLOB. In an attempt to prevent modules
  177. trampling on each other I propose the that any such module should prefix
  178. its variables with its own name separated by _'s. For example the IO::Socket
  179. module keeps a C<timeout> variable in 'io_socket_timeout'.
  180.  
  181. =head1 SEE ALSO
  182.  
  183. L<perlfunc>, 
  184. L<perlop/"I/O Operators">,
  185. L<IO::File>
  186.  
  187. =head1 BUGS
  188.  
  189. Due to backwards compatibility, all filehandles resemble objects
  190. of class C<IO::Handle>, or actually classes derived from that class.
  191. They actually aren't.  Which means you can't derive your own 
  192. class from C<IO::Handle> and inherit those methods.
  193.  
  194. =head1 HISTORY
  195.  
  196. Derived from FileHandle.pm by Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>
  197.  
  198. =cut
  199.  
  200. require 5.000;
  201. use strict;
  202. use vars qw($VERSION $XS_VERSION @EXPORT_OK $AUTOLOAD @ISA);
  203. use Carp;
  204. use Symbol;
  205. use SelectSaver;
  206.  
  207. require Exporter;
  208. @ISA = qw(Exporter);
  209.  
  210. $VERSION = "1.1504";
  211. $XS_VERSION = "1.15";
  212.  
  213. @EXPORT_OK = qw(
  214.     autoflush
  215.     output_field_separator
  216.     output_record_separator
  217.     input_record_separator
  218.     input_line_number
  219.     format_page_number
  220.     format_lines_per_page
  221.     format_lines_left
  222.     format_name
  223.     format_top_name
  224.     format_line_break_characters
  225.     format_formfeed
  226.     format_write
  227.  
  228.     print
  229.     printf
  230.     getline
  231.     getlines
  232.  
  233.     SEEK_SET
  234.     SEEK_CUR
  235.     SEEK_END
  236.     _IOFBF
  237.     _IOLBF
  238.     _IONBF
  239. );
  240.  
  241.  
  242.  
  243. require DynaLoader;
  244. @IO::ISA = qw(DynaLoader);
  245. bootstrap IO $XS_VERSION;
  246.  
  247. sub AUTOLOAD {
  248.     if ($AUTOLOAD =~ /::(_?[a-z])/) {
  249.     $AutoLoader::AUTOLOAD = $AUTOLOAD;
  250.     goto &AutoLoader::AUTOLOAD
  251.     }
  252.     my $constname = $AUTOLOAD;
  253.     $constname =~ s/.*:://;
  254.     my $val = constant($constname);
  255.     defined $val or croak "$constname is not a valid IO::Handle macro";
  256.     no strict 'refs';
  257.     *$AUTOLOAD = sub { $val };
  258.     goto &$AUTOLOAD;
  259. }
  260.  
  261.  
  262.  
  263. sub new {
  264.     my $class = ref($_[0]) || $_[0] || "IO::Handle";
  265.     @_ == 1 or croak "usage: new $class";
  266.     my $fh = gensym;
  267.     bless $fh, $class;
  268. }
  269.  
  270. sub new_from_fd {
  271.     my $class = ref($_[0]) || $_[0] || "IO::Handle";
  272.     @_ == 3 or croak "usage: new_from_fd $class FD, MODE";
  273.     my $fh = gensym;
  274.     shift;
  275.     IO::Handle::fdopen($fh, @_)
  276.     or return undef;
  277.     bless $fh, $class;
  278. }
  279.  
  280. sub DESTROY {}
  281.  
  282.  
  283.  
  284. sub _open_mode_string {
  285.     my ($mode) = @_;
  286.     $mode =~ /^\+?(<|>>?)$/
  287.       or $mode =~ s/^r(\+?)$/$1</
  288.       or $mode =~ s/^w(\+?)$/$1>/
  289.       or $mode =~ s/^a(\+?)$/$1>>/
  290.       or croak "IO::Handle: bad open mode: $mode";
  291.     $mode;
  292. }
  293.  
  294. sub fdopen {
  295.     @_ == 3 or croak 'usage: $fh->fdopen(FD, MODE)';
  296.     my ($fh, $fd, $mode) = @_;
  297.     local(*GLOB);
  298.  
  299.     if (ref($fd) && "".$fd =~ /GLOB\(/o) {
  300.     my $n = qualify(*GLOB);
  301.     *GLOB = *{*$fd};
  302.     $fd =  $n;
  303.     } elsif ($fd =~ m#^\d+$#) {
  304.     $fd = "=$fd";
  305.     }
  306.  
  307.     open($fh, _open_mode_string($mode) . '&' . $fd)
  308.     ? $fh : undef;
  309. }
  310.  
  311. sub close {
  312.     @_ == 1 or croak 'usage: $fh->close()';
  313.     my($fh) = @_;
  314.  
  315.     close($fh);
  316. }
  317.  
  318.  
  319.  
  320. sub opened {
  321.     @_ == 1 or croak 'usage: $fh->opened()';
  322.     defined fileno($_[0]);
  323. }
  324.  
  325. sub fileno {
  326.     @_ == 1 or croak 'usage: $fh->fileno()';
  327.     fileno($_[0]);
  328. }
  329.  
  330. sub getc {
  331.     @_ == 1 or croak 'usage: $fh->getc()';
  332.     getc($_[0]);
  333. }
  334.  
  335. sub eof {
  336.     @_ == 1 or croak 'usage: $fh->eof()';
  337.     eof($_[0]);
  338. }
  339.  
  340. sub print {
  341.     @_ or croak 'usage: $fh->print([ARGS])';
  342.     my $this = shift;
  343.     print $this @_;
  344. }
  345.  
  346. sub printf {
  347.     @_ >= 2 or croak 'usage: $fh->printf(FMT,[ARGS])';
  348.     my $this = shift;
  349.     printf $this @_;
  350. }
  351.  
  352. sub getline {
  353.     @_ == 1 or croak 'usage: $fh->getline';
  354.     my $this = shift;
  355.     return scalar <$this>;
  356.  
  357. *gets = \&getline;  # deprecated
  358.  
  359. sub getlines {
  360.     @_ == 1 or croak 'usage: $fh->getline()';
  361.     wantarray or
  362.     croak 'Can\'t call $fh->getlines in a scalar context, use $fh->getline';
  363.     my $this = shift;
  364.     return <$this>;
  365. }
  366.  
  367. sub truncate {
  368.     @_ == 2 or croak 'usage: $fh->truncate(LEN)';
  369.     truncate($_[0], $_[1]);
  370. }
  371.  
  372. sub read {
  373.     @_ == 3 || @_ == 4 or croak '$fh->read(BUF, LEN [, OFFSET])';
  374.     read($_[0], $_[1], $_[2], $_[3] || 0);
  375. }
  376.  
  377. sub sysread {
  378.     @_ == 3 || @_ == 4 or croak '$fh->sysread(BUF, LEN [, OFFSET])';
  379.     sysread($_[0], $_[1], $_[2], $_[3] || 0);
  380. }
  381.  
  382. sub write {
  383.     @_ == 3 || @_ == 4 or croak '$fh->write(BUF, LEN [, OFFSET])';
  384.     local($\) = "";
  385.     print { $_[0] } substr($_[1], $_[3] || 0, $_[2]);
  386. }
  387.  
  388. sub syswrite {
  389.     @_ == 3 || @_ == 4 or croak '$fh->syswrite(BUF, LEN [, OFFSET])';
  390.     syswrite($_[0], $_[1], $_[2], $_[3] || 0);
  391. }
  392.  
  393. sub stat {
  394.     @_ == 1 or croak 'usage: $fh->stat()';
  395.     stat($_[0]);
  396. }
  397.  
  398.  
  399. sub autoflush {
  400.     my $old = new SelectSaver qualify($_[0], caller);
  401.     my $prev = $|;
  402.     $| = @_ > 1 ? $_[1] : 1;
  403.     $prev;
  404. }
  405.  
  406. sub output_field_separator {
  407.     my $old = new SelectSaver qualify($_[0], caller);
  408.     my $prev = $,;
  409.     $, = $_[1] if @_ > 1;
  410.     $prev;
  411. }
  412.  
  413. sub output_record_separator {
  414.     my $old = new SelectSaver qualify($_[0], caller);
  415.     my $prev = $\;
  416.     $\ = $_[1] if @_ > 1;
  417.     $prev;
  418. }
  419.  
  420. sub input_record_separator {
  421.     my $old = new SelectSaver qualify($_[0], caller);
  422.     my $prev = $/;
  423.     $/ = $_[1] if @_ > 1;
  424.     $prev;
  425. }
  426.  
  427. sub input_line_number {
  428.     my $old = new SelectSaver qualify($_[0], caller);
  429.     my $prev = $.;
  430.     $. = $_[1] if @_ > 1;
  431.     $prev;
  432. }
  433.  
  434. sub format_page_number {
  435.     my $old = new SelectSaver qualify($_[0], caller);
  436.     my $prev = $%;
  437.     $% = $_[1] if @_ > 1;
  438.     $prev;
  439. }
  440.  
  441. sub format_lines_per_page {
  442.     my $old = new SelectSaver qualify($_[0], caller);
  443.     my $prev = $=;
  444.     $= = $_[1] if @_ > 1;
  445.     $prev;
  446. }
  447.  
  448. sub format_lines_left {
  449.     my $old = new SelectSaver qualify($_[0], caller);
  450.     my $prev = $-;
  451.     $- = $_[1] if @_ > 1;
  452.     $prev;
  453. }
  454.  
  455. sub format_name {
  456.     my $old = new SelectSaver qualify($_[0], caller);
  457.     my $prev = $~;
  458.     $~ = qualify($_[1], caller) if @_ > 1;
  459.     $prev;
  460. }
  461.  
  462. sub format_top_name {
  463.     my $old = new SelectSaver qualify($_[0], caller);
  464.     my $prev = $^;
  465.     $^ = qualify($_[1], caller) if @_ > 1;
  466.     $prev;
  467. }
  468.  
  469. sub format_line_break_characters {
  470.     my $old = new SelectSaver qualify($_[0], caller);
  471.     my $prev = $:;
  472.     $: = $_[1] if @_ > 1;
  473.     $prev;
  474. }
  475.  
  476. sub format_formfeed {
  477.     my $old = new SelectSaver qualify($_[0], caller);
  478.     my $prev = $^L;
  479.     $^L = $_[1] if @_ > 1;
  480.     $prev;
  481. }
  482.  
  483. sub formline {
  484.     my $fh = shift;
  485.     my $picture = shift;
  486.     local($^A) = $^A;
  487.     local($\) = "";
  488.     formline($picture, @_);
  489.     print $fh $^A;
  490. }
  491.  
  492. sub format_write {
  493.     @_ < 3 || croak 'usage: $fh->write( [FORMAT_NAME] )';
  494.     if (@_ == 2) {
  495.     my ($fh, $fmt) = @_;
  496.     my $oldfmt = $fh->format_name($fmt);
  497.     write($fh);
  498.     $fh->format_name($oldfmt);
  499.     } else {
  500.     write($_[0]);
  501.     }
  502. }
  503.  
  504. sub fcntl {
  505.     @_ == 3 || croak 'usage: $fh->fcntl( OP, VALUE );';
  506.     my ($fh, $op, $val) = @_;
  507.     my $r = fcntl($fh, $op, $val);
  508.     defined $r && $r eq "0 but true" ? 0 : $r;
  509. }
  510.  
  511. sub ioctl {
  512.     @_ == 3 || croak 'usage: $fh->ioctl( OP, VALUE );';
  513.     my ($fh, $op, $val) = @_;
  514.     my $r = ioctl($fh, $op, $val);
  515.     defined $r && $r eq "0 but true" ? 0 : $r;
  516. }
  517.  
  518. 1;
  519.