5 our($VERSION, @ISA, @EXPORT, @EXPORT_OK);
12 @EXPORT = qw(_IOFBF _IOLBF _IONBF);
18 output_field_separator
19 output_record_separator
20 input_record_separator
27 format_line_break_characters
37 # Everything we're willing to export, we must first import.
39 import IO::Handle grep { !defined(&$_) } @EXPORT, @EXPORT_OK;
42 # Some people call "FileHandle::function", so all the functions
43 # that were in the old FileHandle class must be imported, too.
50 [qw(DESTROY new_from_fd fdopen close fileno getc ungetc gets
51 eof flush error clearerr setbuf setvbuf _open_mode_string)],
53 [qw(seek tell getpos setpos)],
55 [qw(new new_tmpfile open)]
57 for my $pkg (keys %import) {
58 for my $func (@{$import{$pkg}}) {
59 my $c = *{"${pkg}::$func"}{CODE}
60 or die "${pkg}::$func missing";
67 # Specialized importer for Fcntl magic.
73 Exporter::export($pkg, $callpkg, @_);
76 # If the Fcntl extension is available,
77 # export its constants.
81 Exporter::export('Fcntl', $callpkg);
85 ################################################
86 # This is the only exported function we define;
87 # the rest come from other classes.
91 my $r = new IO::Handle;
92 my $w = new IO::Handle;
93 CORE::pipe($r, $w) or return undef;
97 # Rebless standard file handles
98 bless *STDIN{IO}, "FileHandle" if ref *STDIN{IO} eq "IO::Handle";
99 bless *STDOUT{IO}, "FileHandle" if ref *STDOUT{IO} eq "IO::Handle";
100 bless *STDERR{IO}, "FileHandle" if ref *STDERR{IO} eq "IO::Handle";
108 FileHandle - supply object methods for filehandles
114 $fh = new FileHandle;
115 if ($fh->open("< file")) {
120 $fh = new FileHandle "> FOO";
126 $fh = new FileHandle "file", "r";
129 undef $fh; # automatically closes the file
132 $fh = new FileHandle "file", O_WRONLY|O_APPEND;
135 undef $fh; # automatically closes the file
141 $fh->setvbuf($buffer_var, _IOLBF, 1024);
143 ($readfh, $writefh) = FileHandle::pipe;
149 NOTE: This class is now a front-end to the IO::* classes.
151 C<FileHandle::new> creates a C<FileHandle>, which is a reference to a
152 newly created symbol (see the C<Symbol> package). If it receives any
153 parameters, they are passed to C<FileHandle::open>; if the open fails,
154 the C<FileHandle> object is destroyed. Otherwise, it is returned to
157 C<FileHandle::new_from_fd> creates a C<FileHandle> like C<new> does.
158 It requires two parameters, which are passed to C<FileHandle::fdopen>;
159 if the fdopen fails, the C<FileHandle> object is destroyed.
160 Otherwise, it is returned to the caller.
162 C<FileHandle::open> accepts one parameter or two. With one parameter,
163 it is just a front end for the built-in C<open> function. With two
164 parameters, the first parameter is a filename that may include
165 whitespace or other special characters, and the second parameter is
166 the open mode, optionally followed by a file permission value.
168 If C<FileHandle::open> receives a Perl mode string (">", "+<", etc.)
169 or a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic
170 Perl C<open> operator.
172 If C<FileHandle::open> is given a numeric mode, it passes that mode
173 and the optional permissions value to the Perl C<sysopen> operator.
174 For convenience, C<FileHandle::import> tries to import the O_XXX
175 constants from the Fcntl module. If dynamic loading is not available,
176 this may fail, but the rest of FileHandle will still work.
178 C<FileHandle::fdopen> is like C<open> except that its first parameter
179 is not a filename but rather a file handle name, a FileHandle object,
180 or a file descriptor number.
182 If the C functions fgetpos() and fsetpos() are available, then
183 C<FileHandle::getpos> returns an opaque value that represents the
184 current position of the FileHandle, and C<FileHandle::setpos> uses
185 that value to return to a previously visited position.
187 If the C function setvbuf() is available, then C<FileHandle::setvbuf>
188 sets the buffering policy for the FileHandle. The calling sequence
189 for the Perl function is the same as its C counterpart, including the
190 macros C<_IOFBF>, C<_IOLBF>, and C<_IONBF>, except that the buffer
191 parameter specifies a scalar variable to use as a buffer. WARNING: A
192 variable used as a buffer by C<FileHandle::setvbuf> must not be
193 modified in any way until the FileHandle is closed or until
194 C<FileHandle::setvbuf> is called again, or memory corruption may
197 See L<perlfunc> for complete descriptions of each of the following
198 supported C<FileHandle> methods, which are just front ends for the
199 corresponding built-in functions:
210 See L<perlvar> for complete descriptions of each of the following
211 supported C<FileHandle> methods:
214 output_field_separator
215 output_record_separator
216 input_record_separator
219 format_lines_per_page
223 format_line_break_characters
226 Furthermore, for doing normal I/O you might need these:
232 See L<perlfunc/print>.
236 See L<perlfunc/printf>.
240 This works like <$fh> described in L<perlop/"I/O Operators">
241 except that it's more readable and can be safely called in a
242 list context but still returns just one line.
246 This works like <$fh> when called in a list context to
247 read all the remaining lines in a file, except that it's more readable.
248 It will also croak() if accidentally called in a scalar context.
252 There are many other functions available since FileHandle is descended
253 from IO::File, IO::Seekable, and IO::Handle. Please see those
254 respective pages for documentation on more functions.
260 L<perlop/"I/O Operators">.