Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
6.6 KiB

  1. package FileHandle;
  2. use 5.003_11;
  3. use strict;
  4. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  5. $VERSION = "2.00";
  6. require IO::File;
  7. @ISA = qw(IO::File);
  8. @EXPORT = qw(_IOFBF _IOLBF _IONBF);
  9. @EXPORT_OK = qw(
  10. pipe
  11. autoflush
  12. output_field_separator
  13. output_record_separator
  14. input_record_separator
  15. input_line_number
  16. format_page_number
  17. format_lines_per_page
  18. format_lines_left
  19. format_name
  20. format_top_name
  21. format_line_break_characters
  22. format_formfeed
  23. print
  24. printf
  25. getline
  26. getlines
  27. );
  28. #
  29. # Everything we're willing to export, we must first import.
  30. #
  31. import IO::Handle grep { !defined(&$_) } @EXPORT, @EXPORT_OK;
  32. #
  33. # Some people call "FileHandle::function", so all the functions
  34. # that were in the old FileHandle class must be imported, too.
  35. #
  36. {
  37. no strict 'refs';
  38. my %import = (
  39. 'IO::Handle' =>
  40. [qw(DESTROY new_from_fd fdopen close fileno getc ungetc gets
  41. eof flush error clearerr setbuf setvbuf _open_mode_string)],
  42. 'IO::Seekable' =>
  43. [qw(seek tell getpos setpos)],
  44. 'IO::File' =>
  45. [qw(new new_tmpfile open)]
  46. );
  47. for my $pkg (keys %import) {
  48. for my $func (@{$import{$pkg}}) {
  49. my $c = *{"${pkg}::$func"}{CODE}
  50. or die "${pkg}::$func missing";
  51. *$func = $c;
  52. }
  53. }
  54. }
  55. #
  56. # Specialized importer for Fcntl magic.
  57. #
  58. sub import {
  59. my $pkg = shift;
  60. my $callpkg = caller;
  61. require Exporter;
  62. Exporter::export($pkg, $callpkg, @_);
  63. #
  64. # If the Fcntl extension is available,
  65. # export its constants.
  66. #
  67. eval {
  68. require Fcntl;
  69. Exporter::export('Fcntl', $callpkg);
  70. };
  71. }
  72. ################################################
  73. # This is the only exported function we define;
  74. # the rest come from other classes.
  75. #
  76. sub pipe {
  77. my $r = new IO::Handle;
  78. my $w = new IO::Handle;
  79. CORE::pipe($r, $w) or return undef;
  80. ($r, $w);
  81. }
  82. # Rebless standard file handles
  83. bless *STDIN{IO}, "FileHandle" if ref *STDIN{IO} eq "IO::Handle";
  84. bless *STDOUT{IO}, "FileHandle" if ref *STDOUT{IO} eq "IO::Handle";
  85. bless *STDERR{IO}, "FileHandle" if ref *STDERR{IO} eq "IO::Handle";
  86. 1;
  87. __END__
  88. =head1 NAME
  89. FileHandle - supply object methods for filehandles
  90. =head1 SYNOPSIS
  91. use FileHandle;
  92. $fh = new FileHandle;
  93. if ($fh->open("< file")) {
  94. print <$fh>;
  95. $fh->close;
  96. }
  97. $fh = new FileHandle "> FOO";
  98. if (defined $fh) {
  99. print $fh "bar\n";
  100. $fh->close;
  101. }
  102. $fh = new FileHandle "file", "r";
  103. if (defined $fh) {
  104. print <$fh>;
  105. undef $fh; # automatically closes the file
  106. }
  107. $fh = new FileHandle "file", O_WRONLY|O_APPEND;
  108. if (defined $fh) {
  109. print $fh "corge\n";
  110. undef $fh; # automatically closes the file
  111. }
  112. $pos = $fh->getpos;
  113. $fh->setpos($pos);
  114. $fh->setvbuf($buffer_var, _IOLBF, 1024);
  115. ($readfh, $writefh) = FileHandle::pipe;
  116. autoflush STDOUT 1;
  117. =head1 DESCRIPTION
  118. NOTE: This class is now a front-end to the IO::* classes.
  119. C<FileHandle::new> creates a C<FileHandle>, which is a reference to a
  120. newly created symbol (see the C<Symbol> package). If it receives any
  121. parameters, they are passed to C<FileHandle::open>; if the open fails,
  122. the C<FileHandle> object is destroyed. Otherwise, it is returned to
  123. the caller.
  124. C<FileHandle::new_from_fd> creates a C<FileHandle> like C<new> does.
  125. It requires two parameters, which are passed to C<FileHandle::fdopen>;
  126. if the fdopen fails, the C<FileHandle> object is destroyed.
  127. Otherwise, it is returned to the caller.
  128. C<FileHandle::open> accepts one parameter or two. With one parameter,
  129. it is just a front end for the built-in C<open> function. With two
  130. parameters, the first parameter is a filename that may include
  131. whitespace or other special characters, and the second parameter is
  132. the open mode, optionally followed by a file permission value.
  133. If C<FileHandle::open> receives a Perl mode string (">", "+<", etc.)
  134. or a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic
  135. Perl C<open> operator.
  136. If C<FileHandle::open> is given a numeric mode, it passes that mode
  137. and the optional permissions value to the Perl C<sysopen> operator.
  138. For convenience, C<FileHandle::import> tries to import the O_XXX
  139. constants from the Fcntl module. If dynamic loading is not available,
  140. this may fail, but the rest of FileHandle will still work.
  141. C<FileHandle::fdopen> is like C<open> except that its first parameter
  142. is not a filename but rather a file handle name, a FileHandle object,
  143. or a file descriptor number.
  144. If the C functions fgetpos() and fsetpos() are available, then
  145. C<FileHandle::getpos> returns an opaque value that represents the
  146. current position of the FileHandle, and C<FileHandle::setpos> uses
  147. that value to return to a previously visited position.
  148. If the C function setvbuf() is available, then C<FileHandle::setvbuf>
  149. sets the buffering policy for the FileHandle. The calling sequence
  150. for the Perl function is the same as its C counterpart, including the
  151. macros C<_IOFBF>, C<_IOLBF>, and C<_IONBF>, except that the buffer
  152. parameter specifies a scalar variable to use as a buffer. WARNING: A
  153. variable used as a buffer by C<FileHandle::setvbuf> must not be
  154. modified in any way until the FileHandle is closed or until
  155. C<FileHandle::setvbuf> is called again, or memory corruption may
  156. result!
  157. See L<perlfunc> for complete descriptions of each of the following
  158. supported C<FileHandle> methods, which are just front ends for the
  159. corresponding built-in functions:
  160. close
  161. fileno
  162. getc
  163. gets
  164. eof
  165. clearerr
  166. seek
  167. tell
  168. See L<perlvar> for complete descriptions of each of the following
  169. supported C<FileHandle> methods:
  170. autoflush
  171. output_field_separator
  172. output_record_separator
  173. input_record_separator
  174. input_line_number
  175. format_page_number
  176. format_lines_per_page
  177. format_lines_left
  178. format_name
  179. format_top_name
  180. format_line_break_characters
  181. format_formfeed
  182. Furthermore, for doing normal I/O you might need these:
  183. =over
  184. =item $fh->print
  185. See L<perlfunc/print>.
  186. =item $fh->printf
  187. See L<perlfunc/printf>.
  188. =item $fh->getline
  189. This works like <$fh> described in L<perlop/"I/O Operators">
  190. except that it's more readable and can be safely called in an
  191. array context but still returns just one line.
  192. =item $fh->getlines
  193. This works like <$fh> when called in an array context to
  194. read all the remaining lines in a file, except that it's more readable.
  195. It will also croak() if accidentally called in a scalar context.
  196. =back
  197. There are many other functions available since FileHandle is descended
  198. from IO::File, IO::Seekable, and IO::Handle. Please see those
  199. respective pages for documentation on more functions.
  200. =head1 SEE ALSO
  201. The B<IO> extension,
  202. L<perlfunc>,
  203. L<perlop/"I/O Operators">.
  204. =cut