5 use vars qw($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.
47 for my $f (qw(DESTROY new_from_fd fdopen close fileno getc ungetc gets eof
48 setbuf setvbuf _open_mode_string)) {
49 *{$f} = \&{"IO::Handle::$f"} or die "$f missing";
51 for my $f (qw(seek tell fgetpos fsetpos fflush ferror clearerr)) {
52 *{$f} = \&{"IO::Seekable::$f"} or die "$f missing";
54 for my $f (qw(new new_tmpfile open)) {
55 *{$f} = \&{"IO::File::$f"} or die "$f missing";
60 # Specialized importer for Fcntl magic.
65 Exporter::export $pkg, $callpkg, @_;
68 # If the Fcntl extension is available,
69 # export its constants.
73 Exporter::export 'Fcntl', $callpkg;
77 ################################################
78 # This is the only exported function we define;
79 # the rest come from other classes.
83 my $r = new IO::Handle;
84 my $w = new IO::Handle;
85 CORE::pipe($r, $w) or return undef;
95 FileHandle - supply object methods for filehandles
101 $fh = new FileHandle;
102 if ($fh->open "< file") {
107 $fh = new FileHandle "> FOO";
113 $fh = new FileHandle "file", "r";
116 undef $fh; # automatically closes the file
119 $fh = new FileHandle "file", O_WRONLY|O_APPEND;
122 undef $fh; # automatically closes the file
128 $fh->setvbuf($buffer_var, _IOLBF, 1024);
130 ($readfh, $writefh) = FileHandle::pipe;
136 NOTE: This class is now a front-end to the IO::* classes.
138 C<FileHandle::new> creates a C<FileHandle>, which is a reference to a
139 newly created symbol (see the C<Symbol> package). If it receives any
140 parameters, they are passed to C<FileHandle::open>; if the open fails,
141 the C<FileHandle> object is destroyed. Otherwise, it is returned to
144 C<FileHandle::new_from_fd> creates a C<FileHandle> like C<new> does.
145 It requires two parameters, which are passed to C<FileHandle::fdopen>;
146 if the fdopen fails, the C<FileHandle> object is destroyed.
147 Otherwise, it is returned to the caller.
149 C<FileHandle::open> accepts one parameter or two. With one parameter,
150 it is just a front end for the built-in C<open> function. With two
151 parameters, the first parameter is a filename that may include
152 whitespace or other special characters, and the second parameter is
153 the open mode, optionally followed by a file permission value.
155 If C<FileHandle::open> receives a Perl mode string (">", "+<", etc.)
156 or a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic
157 Perl C<open> operator.
159 If C<FileHandle::open> is given a numeric mode, it passes that mode
160 and the optional permissions value to the Perl C<sysopen> operator.
161 For convenience, C<FileHandle::import> tries to import the O_XXX
162 constants from the Fcntl module. If dynamic loading is not available,
163 this may fail, but the rest of FileHandle will still work.
165 C<FileHandle::fdopen> is like C<open> except that its first parameter
166 is not a filename but rather a file handle name, a FileHandle object,
167 or a file descriptor number.
169 If the C functions fgetpos() and fsetpos() are available, then
170 C<FileHandle::getpos> returns an opaque value that represents the
171 current position of the FileHandle, and C<FileHandle::setpos> uses
172 that value to return to a previously visited position.
174 If the C function setvbuf() is available, then C<FileHandle::setvbuf>
175 sets the buffering policy for the FileHandle. The calling sequence
176 for the Perl function is the same as its C counterpart, including the
177 macros C<_IOFBF>, C<_IOLBF>, and C<_IONBF>, except that the buffer
178 parameter specifies a scalar variable to use as a buffer. WARNING: A
179 variable used as a buffer by C<FileHandle::setvbuf> must not be
180 modified in any way until the FileHandle is closed or until
181 C<FileHandle::setvbuf> is called again, or memory corruption may
184 See L<perlfunc> for complete descriptions of each of the following
185 supported C<FileHandle> methods, which are just front ends for the
186 corresponding built-in functions:
197 See L<perlvar> for complete descriptions of each of the following
198 supported C<FileHandle> methods:
201 output_field_separator
202 output_record_separator
203 input_record_separator
206 format_lines_per_page
210 format_line_break_characters
213 Furthermore, for doing normal I/O you might need these:
219 See L<perlfunc/print>.
223 See L<perlfunc/printf>.
227 This works like <$fh> described in L<perlop/"I/O Operators">
228 except that it's more readable and can be safely called in an
229 array context but still returns just one line.
233 This works like <$fh> when called in an array context to
234 read all the remaining lines in a file, except that it's more readable.
235 It will also croak() if accidentally called in a scalar context.
243 L<perlop/"I/O Operators">.