X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/5f05dabc4054964aa3b10f44f8468547f051cdf8..aa4b50cc509e728b57bbbb5cf5f166f90aae89a4:/lib/FileHandle.pm diff --git a/lib/FileHandle.pm b/lib/FileHandle.pm index e2ce83d..133221b 100644 --- a/lib/FileHandle.pm +++ b/lib/FileHandle.pm @@ -1,10 +1,10 @@ package FileHandle; -use 5.003_11; +use 5.006; use strict; -use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); +our($VERSION, @ISA, @EXPORT, @EXPORT_OK); -$VERSION = "2.00"; +$VERSION = "2.03"; require IO::File; @ISA = qw(IO::File); @@ -36,7 +36,7 @@ require IO::File; # # Everything we're willing to export, we must first import. # -import IO::Handle grep { !defined(&$_) } @EXPORT, @EXPORT_OK; +IO::Handle->import( grep { !defined(&$_) } @EXPORT, @EXPORT_OK ); # # Some people call "FileHandle::function", so all the functions @@ -44,15 +44,22 @@ import IO::Handle grep { !defined(&$_) } @EXPORT, @EXPORT_OK; # { no strict 'refs'; - for my $f (qw(DESTROY new_from_fd fdopen close fileno getc ungetc gets eof - setbuf setvbuf _open_mode_string)) { - *{$f} = \&{"IO::Handle::$f"} or die "$f missing"; - } - for my $f (qw(seek tell fgetpos fsetpos fflush ferror clearerr)) { - *{$f} = \&{"IO::Seekable::$f"} or die "$f missing"; - } - for my $f (qw(new new_tmpfile open)) { - *{$f} = \&{"IO::File::$f"} or die "$f missing"; + + my %import = ( + 'IO::Handle' => + [qw(DESTROY new_from_fd fdopen close fileno getc ungetc gets + eof flush error clearerr setbuf setvbuf _open_mode_string)], + 'IO::Seekable' => + [qw(seek tell getpos setpos)], + 'IO::File' => + [qw(new new_tmpfile open)] + ); + for my $pkg (keys %import) { + for my $func (@{$import{$pkg}}) { + my $c = *{"${pkg}::$func"}{CODE} + or die "${pkg}::$func missing"; + *$func = $c; + } } } @@ -62,7 +69,8 @@ import IO::Handle grep { !defined(&$_) } @EXPORT, @EXPORT_OK; sub import { my $pkg = shift; my $callpkg = caller; - Exporter::export $pkg, $callpkg, @_; + require Exporter; + Exporter::export($pkg, $callpkg, @_); # # If the Fcntl extension is available, @@ -70,7 +78,7 @@ sub import { # eval { require Fcntl; - Exporter::export 'Fcntl', $callpkg; + Exporter::export('Fcntl', $callpkg); }; } @@ -80,12 +88,17 @@ sub import { # sub pipe { - my $r = new IO::Handle; - my $w = new IO::Handle; + my $r = IO::Handle->new; + my $w = IO::Handle->new; CORE::pipe($r, $w) or return undef; ($r, $w); } +# Rebless standard file handles +bless *STDIN{IO}, "FileHandle" if ref *STDIN{IO} eq "IO::Handle"; +bless *STDOUT{IO}, "FileHandle" if ref *STDOUT{IO} eq "IO::Handle"; +bless *STDERR{IO}, "FileHandle" if ref *STDERR{IO} eq "IO::Handle"; + 1; __END__ @@ -98,32 +111,32 @@ FileHandle - supply object methods for filehandles use FileHandle; - $fh = new FileHandle; - if ($fh->open "< file") { + $fh = FileHandle->new; + if ($fh->open("< file")) { print <$fh>; $fh->close; } - $fh = new FileHandle "> FOO"; + $fh = FileHandle->new("> FOO"); if (defined $fh) { print $fh "bar\n"; $fh->close; } - $fh = new FileHandle "file", "r"; + $fh = FileHandle->new("file", "r"); if (defined $fh) { print <$fh>; undef $fh; # automatically closes the file } - $fh = new FileHandle "file", O_WRONLY|O_APPEND; + $fh = FileHandle->new("file", O_WRONLY|O_APPEND); if (defined $fh) { print $fh "corge\n"; undef $fh; # automatically closes the file } $pos = $fh->getpos; - $fh->setpos $pos; + $fh->setpos($pos); $fh->setvbuf($buffer_var, _IOLBF, 1024); @@ -184,7 +197,7 @@ result! See L for complete descriptions of each of the following supported C methods, which are just front ends for the corresponding built-in functions: - + close fileno getc @@ -212,7 +225,7 @@ supported C methods: Furthermore, for doing normal I/O you might need these: -=over +=over 4 =item $fh->print @@ -225,17 +238,21 @@ See L. =item $fh->getline This works like <$fh> described in L -except that it's more readable and can be safely called in an -array context but still returns just one line. +except that it's more readable and can be safely called in a +list context but still returns just one line. =item $fh->getlines -This works like <$fh> when called in an array context to +This works like <$fh> when called in a list context to read all the remaining lines in a file, except that it's more readable. It will also croak() if accidentally called in a scalar context. =back +There are many other functions available since FileHandle is descended +from IO::File, IO::Seekable, and IO::Handle. Please see those +respective pages for documentation on more functions. + =head1 SEE ALSO The B extension,