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 in either Perl form (">", "+<", etc.) or POSIX form
64 C<FileHandle::fdopen> is like C<open> except that its first parameter
65 is not a filename but rather a file handle name, a FileHandle object,
66 or a file descriptor number.
68 If the C functions fgetpos() and fsetpos() are available, then
69 C<FileHandle::getpos> returns an opaque value that represents the
70 current position of the FileHandle, and C<FileHandle::setpos> uses
71 that value to return to a previously visited position.
73 If the C function setvbuf() is available, then C<FileHandle::setvbuf>
74 sets the buffering policy for the FileHandle. The calling sequence
75 for the Perl function is the same as its C counterpart, including the
76 macros C<_IOFBF>, C<_IOLBF>, and C<_IONBF>, except that the buffer
77 parameter specifies a scalar variable to use as a buffer. WARNING: A
78 variable used as a buffer by C<FileHandle::setvbuf> must not be
79 modified in any way until the FileHandle is closed or until
80 C<FileHandle::setvbuf> is called again, or memory corruption may
83 See L<perlfunc> for complete descriptions of each of the following
84 supported C<FileHandle> methods, which are just front ends for the
85 corresponding built-in functions:
96 See L<perlvar> for complete descriptions of each of the following
97 supported C<FileHandle> methods:
100 output_field_separator
101 output_record_separator
102 input_record_separator
105 format_lines_per_page
109 format_line_break_characters
112 Furthermore, for doing normal I/O you might need these:
118 See L<perlfunc/print>.
122 See L<perlfunc/printf>.
126 This works like <$fh> described in L<perlop/"I/O Operators">
127 except that it's more readable and can be safely called in an
128 array context but still returns just one line.
132 This works like <$fh> when called in an array context to
133 read all the remaining lines in a file, except that it's more readable.
134 It will also croak() if accidentally called in a scalar context.
141 L<perlop/"I/O Operators">,
142 L<POSIX/"FileHandle">
146 Due to backwards compatibility, all filehandles resemble objects
147 of class C<FileHandle>, or actually classes derived from that class.
148 They actually aren't. Which means you can't derive your own
149 class from C<FileHandle> and inherit those methods.
154 use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD);
161 @ISA = qw(Exporter DynaLoader);
165 @EXPORT = qw(_IOFBF _IOLBF _IONBF);
169 output_field_separator
170 output_record_separator
171 input_record_separator
174 format_lines_per_page
178 format_line_break_characters
188 ################################################
189 ## If the Fcntl extension is available,
190 ## export its constants.
195 my $callpkg = caller;
196 Exporter::export $pkg, $callpkg;
199 Exporter::export 'Fcntl', $callpkg;
204 ################################################
205 ## Interaction with the XS.
209 bootstrap FileHandle;
212 *constant = sub { undef };
216 if ($AUTOLOAD =~ /::(_?[a-z])/) {
217 $AutoLoader::AUTOLOAD = $AUTOLOAD;
218 goto &AutoLoader::AUTOLOAD
220 my $constname = $AUTOLOAD;
221 $constname =~ s/.*:://;
222 my $val = constant($constname);
223 defined $val or croak "$constname is not a valid FileHandle macro";
224 *$AUTOLOAD = sub { $val };
229 ################################################
230 ## Constructors, destructors.
234 @_ >= 1 && @_ <= 3 or croak 'usage: new FileHandle [FILENAME [,MODE]]';
238 FileHandle::open($fh, @_)
245 @_ == 3 or croak 'usage: new_from_fd FileHandle FD, MODE';
248 FileHandle::fdopen($fh, @_)
258 ################################################
263 @_ and croak 'usage: FileHandle::pipe()';
264 my $readfh = new FileHandle;
265 my $writefh = new FileHandle;
266 pipe($readfh, $writefh)
271 sub _open_mode_string {
273 $mode =~ /^\+?(<|>>?)$/
274 or $mode =~ s/^r(\+?)$/$1</
275 or $mode =~ s/^w(\+?)$/$1>/
276 or $mode =~ s/^a(\+?)$/$1>>/
277 or croak "FileHandle: bad open mode: $mode";
282 @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
283 my ($fh, $file) = @_;
285 my ($mode, $perms) = @_[2, 3];
286 if ($mode =~ /^\d+$/) {
287 defined $perms or $perms = 0666;
288 return sysopen($fh, $file, $mode, $perms);
290 $file = "./" . $file unless $file =~ m#^/#;
291 $file = _open_mode_string($mode) . " $file\0";
297 @_ == 3 or croak 'usage: $fh->fdopen(FD, MODE)';
298 my ($fh, $fd, $mode) = @_;
299 if (ref($fd) =~ /GLOB\(/) {
300 # It's a glob reference; remove the star from its name.
301 ($fd = "".$$fd) =~ s/^\*//;
302 } elsif ($fd =~ m#^\d+$#) {
303 # It's an FD number; prefix with "=".
306 open($fh, _open_mode_string($mode) . '&' . $fd);
310 @_ == 1 or croak 'usage: $fh->close()';
314 ################################################
315 ## Normal I/O functions.
319 @_ == 1 or croak 'usage: $fh->fileno()';
324 @_ == 1 or croak 'usage: $fh->getc()';
329 @_ == 1 or croak 'usage: $fh->gets()';
335 @_ == 1 or croak 'usage: $fh->eof()';
340 @_ == 1 or croak 'usage: $fh->clearerr()';
345 @_ == 3 or croak 'usage: $fh->seek(POS, WHENCE)';
346 seek($_[0], $_[1], $_[2]);
350 @_ == 1 or croak 'usage: $fh->tell()';
355 @_ or croak 'usage: $fh->print([ARGS])';
361 @_ or croak 'usage: $fh->printf([ARGS])';
367 @_ == 1 or croak 'usage: $fh->getline';
369 return scalar <$this>;
373 @_ == 1 or croak 'usage: $fh->getline()';
375 wantarray or croak "Can't call FileHandle::getlines in a scalar context";
379 ################################################
380 ## State modification functions.
384 my $old = new SelectSaver qualify($_[0], caller);
386 $| = @_ > 1 ? $_[1] : 1;
390 sub output_field_separator {
391 my $old = new SelectSaver qualify($_[0], caller);
393 $, = $_[1] if @_ > 1;
397 sub output_record_separator {
398 my $old = new SelectSaver qualify($_[0], caller);
400 $\ = $_[1] if @_ > 1;
404 sub input_record_separator {
405 my $old = new SelectSaver qualify($_[0], caller);
407 $/ = $_[1] if @_ > 1;
411 sub input_line_number {
412 my $old = new SelectSaver qualify($_[0], caller);
414 $. = $_[1] if @_ > 1;
418 sub format_page_number {
419 my $old = new SelectSaver qualify($_[0], caller);
421 $% = $_[1] if @_ > 1;
425 sub format_lines_per_page {
426 my $old = new SelectSaver qualify($_[0], caller);
428 $= = $_[1] if @_ > 1;
432 sub format_lines_left {
433 my $old = new SelectSaver qualify($_[0], caller);
435 $- = $_[1] if @_ > 1;
440 my $old = new SelectSaver qualify($_[0], caller);
442 $~ = qualify($_[1], caller) if @_ > 1;
446 sub format_top_name {
447 my $old = new SelectSaver qualify($_[0], caller);
449 $^ = qualify($_[1], caller) if @_ > 1;
453 sub format_line_break_characters {
454 my $old = new SelectSaver qualify($_[0], caller);
456 $: = $_[1] if @_ > 1;
460 sub format_formfeed {
461 my $old = new SelectSaver qualify($_[0], caller);
463 $^L = $_[1] if @_ > 1;