5 FileHandle - supply object methods for filehandles
12 if ($fh->open "< file") {
17 $fh = new FileHandle "> FOO";
23 $fh = new FileHandle "file", "r";
26 undef $fh; # automatically closes the file
29 $fh = new FileHandle "file", O_WRONLY|O_APPEND;
32 undef $fh; # automatically closes the file
38 $fh->setvbuf($buffer_var, _IOLBF, 1024);
40 ($readfh, $writefh) = FileHandle::pipe;
46 C<FileHandle::new> creates a C<FileHandle>, which is a reference to a
47 newly created symbol (see the C<Symbol> package). If it receives any
48 parameters, they are passed to C<FileHandle::open>; if the open fails,
49 the C<FileHandle> object is destroyed. Otherwise, it is returned to
52 C<FileHandle::new_from_fd> creates a C<FileHandle> like C<new> does.
53 It requires two parameters, which are passed to C<FileHandle::fdopen>;
54 if the fdopen fails, the C<FileHandle> object is destroyed.
55 Otherwise, it is returned to the caller.
57 C<FileHandle::open> accepts one parameter or two. With one parameter,
58 it is just a front end for the built-in C<open> function. With two
59 parameters, the first parameter is a filename that may include
60 whitespace or other special characters, and the second parameter is
61 the open mode, optionally followed by a file permission value.
63 If C<FileHandle::open> receives a Perl mode string (">", "+<", etc.)
64 or a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic
65 Perl C<open> operator.
67 If C<FileHandle::open> is given a numeric mode, it passes that mode
68 and the optional permissions value to the Perl C<sysopen> operator.
69 For convenience, C<FileHandle::import> tries to import the O_XXX
70 constants from the Fcntl module. If dynamic loading is not available,
71 this may fail, but the rest of FileHandle will still work.
73 C<FileHandle::fdopen> is like C<open> except that its first parameter
74 is not a filename but rather a file handle name, a FileHandle object,
75 or a file descriptor number.
77 If the C functions fgetpos() and fsetpos() are available, then
78 C<FileHandle::getpos> returns an opaque value that represents the
79 current position of the FileHandle, and C<FileHandle::setpos> uses
80 that value to return to a previously visited position.
82 If the C function setvbuf() is available, then C<FileHandle::setvbuf>
83 sets the buffering policy for the FileHandle. The calling sequence
84 for the Perl function is the same as its C counterpart, including the
85 macros C<_IOFBF>, C<_IOLBF>, and C<_IONBF>, except that the buffer
86 parameter specifies a scalar variable to use as a buffer. WARNING: A
87 variable used as a buffer by C<FileHandle::setvbuf> must not be
88 modified in any way until the FileHandle is closed or until
89 C<FileHandle::setvbuf> is called again, or memory corruption may
92 See L<perlfunc> for complete descriptions of each of the following
93 supported C<FileHandle> methods, which are just front ends for the
94 corresponding built-in functions:
105 See L<perlvar> for complete descriptions of each of the following
106 supported C<FileHandle> methods:
109 output_field_separator
110 output_record_separator
111 input_record_separator
114 format_lines_per_page
118 format_line_break_characters
121 Furthermore, for doing normal I/O you might need these:
127 See L<perlfunc/print>.
131 See L<perlfunc/printf>.
135 This works like <$fh> described in L<perlop/"I/O Operators">
136 except that it's more readable and can be safely called in an
137 array context but still returns just one line.
141 This works like <$fh> when called in an array context to
142 read all the remaining lines in a file, except that it's more readable.
143 It will also croak() if accidentally called in a scalar context.
150 L<perlop/"I/O Operators">,
151 L<POSIX/"FileHandle">
155 Due to backwards compatibility, all filehandles resemble objects
156 of class C<FileHandle>, or actually classes derived from that class.
157 They actually aren't. Which means you can't derive your own
158 class from C<FileHandle> and inherit those methods.
163 use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD);
170 @ISA = qw(Exporter DynaLoader);
174 @EXPORT = qw(_IOFBF _IOLBF _IONBF);
178 output_field_separator
179 output_record_separator
180 input_record_separator
183 format_lines_per_page
187 format_line_break_characters
197 ################################################
198 ## If the Fcntl extension is available,
199 ## export its constants.
204 my $callpkg = caller;
205 Exporter::export $pkg, $callpkg;
208 Exporter::export 'Fcntl', $callpkg;
213 ################################################
214 ## Interaction with the XS.
218 bootstrap FileHandle;
221 *constant = sub { undef };
225 if ($AUTOLOAD =~ /::(_?[a-z])/) {
226 $AutoLoader::AUTOLOAD = $AUTOLOAD;
227 goto &AutoLoader::AUTOLOAD
229 my $constname = $AUTOLOAD;
230 $constname =~ s/.*:://;
231 my $val = constant($constname);
232 defined $val or croak "$constname is not a valid FileHandle macro";
233 *$AUTOLOAD = sub { $val };
238 ################################################
239 ## Constructors, destructors.
244 or croak 'usage: new FileHandle [FILENAME [,MODE [,PERMS]]]';
248 FileHandle::open($fh, @_)
255 @_ == 3 or croak 'usage: new_from_fd FileHandle FD, MODE';
258 FileHandle::fdopen($fh, @_)
266 # During global object destruction, this function may be called
267 # on FILEHANDLEs as well as on the GLOBs that contains them.
268 # Thus the following trickery. If only the CORE file operators
269 # could deal with FILEHANDLEs, it wouldn't be necessary...
271 if ($fh =~ /=FILEHANDLE\(/) {
273 close(TMP) if defined fileno(TMP);
276 close($fh) if defined fileno($fh);
280 ################################################
285 @_ and croak 'usage: FileHandle::pipe()';
286 my $readfh = new FileHandle;
287 my $writefh = new FileHandle;
288 pipe($readfh, $writefh)
293 sub _open_mode_string {
295 $mode =~ /^\+?(<|>>?)$/
296 or $mode =~ s/^r(\+?)$/$1</
297 or $mode =~ s/^w(\+?)$/$1>/
298 or $mode =~ s/^a(\+?)$/$1>>/
299 or croak "FileHandle: bad open mode: $mode";
304 @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
305 my ($fh, $file) = @_;
307 my ($mode, $perms) = @_[2, 3];
308 if ($mode =~ /^\d+$/) {
309 defined $perms or $perms = 0666;
310 return sysopen($fh, $file, $mode, $perms);
312 $file = "./" . $file unless $file =~ m#^/#;
313 $file = _open_mode_string($mode) . " $file\0";
319 @_ == 3 or croak 'usage: $fh->fdopen(FD, MODE)';
320 my ($fh, $fd, $mode) = @_;
321 if (ref($fd) =~ /GLOB\(/) {
322 # It's a glob reference; remove the star from its name.
323 ($fd = "".$$fd) =~ s/^\*//;
324 } elsif ($fd =~ m#^\d+$#) {
325 # It's an FD number; prefix with "=".
328 open($fh, _open_mode_string($mode) . '&' . $fd);
332 @_ == 1 or croak 'usage: $fh->close()';
336 ################################################
337 ## Normal I/O functions.
341 @_ == 1 or croak 'usage: $fh->fileno()';
346 @_ == 1 or croak 'usage: $fh->getc()';
351 @_ == 1 or croak 'usage: $fh->gets()';
357 @_ == 1 or croak 'usage: $fh->eof()';
362 @_ == 1 or croak 'usage: $fh->clearerr()';
367 @_ == 3 or croak 'usage: $fh->seek(POS, WHENCE)';
368 seek($_[0], $_[1], $_[2]);
372 @_ == 1 or croak 'usage: $fh->tell()';
377 @_ or croak 'usage: $fh->print([ARGS])';
383 @_ or croak 'usage: $fh->printf([ARGS])';
389 @_ == 1 or croak 'usage: $fh->getline';
391 return scalar <$this>;
395 @_ == 1 or croak 'usage: $fh->getline()';
397 wantarray or croak "Can't call FileHandle::getlines in a scalar context";
401 ################################################
402 ## State modification functions.
406 my $old = new SelectSaver qualify($_[0], caller);
408 $| = @_ > 1 ? $_[1] : 1;
412 sub output_field_separator {
413 my $old = new SelectSaver qualify($_[0], caller);
415 $, = $_[1] if @_ > 1;
419 sub output_record_separator {
420 my $old = new SelectSaver qualify($_[0], caller);
422 $\ = $_[1] if @_ > 1;
426 sub input_record_separator {
427 my $old = new SelectSaver qualify($_[0], caller);
429 $/ = $_[1] if @_ > 1;
433 sub input_line_number {
434 my $old = new SelectSaver qualify($_[0], caller);
436 $. = $_[1] if @_ > 1;
440 sub format_page_number {
441 my $old = new SelectSaver qualify($_[0], caller);
443 $% = $_[1] if @_ > 1;
447 sub format_lines_per_page {
448 my $old = new SelectSaver qualify($_[0], caller);
450 $= = $_[1] if @_ > 1;
454 sub format_lines_left {
455 my $old = new SelectSaver qualify($_[0], caller);
457 $- = $_[1] if @_ > 1;
462 my $old = new SelectSaver qualify($_[0], caller);
464 $~ = qualify($_[1], caller) if @_ > 1;
468 sub format_top_name {
469 my $old = new SelectSaver qualify($_[0], caller);
471 $^ = qualify($_[1], caller) if @_ > 1;
475 sub format_line_break_characters {
476 my $old = new SelectSaver qualify($_[0], caller);
478 $: = $_[1] if @_ > 1;
482 sub format_formfeed {
483 my $old = new SelectSaver qualify($_[0], caller);
485 $^L = $_[1] if @_ > 1;