5 IO::File - supply object methods for filehandles
12 if ($fh->open "< file") {
17 $fh = new IO::File "> FOO";
23 $fh = new IO::File "file", "r";
26 undef $fh; # automatically closes the file
29 $fh = new IO::File "file", O_WRONLY|O_APPEND;
32 undef $fh; # automatically closes the file
38 $fh->setvbuf($buffer_var, _IOLBF, 1024);
44 C<IO::File> inherits from C<IO::Handle> and C<IO::Seekable>. It extends
45 these classes with methods that are specific to file handles.
53 Creates a C<IO::File>. If it receives any parameters, they are passed to
54 the method C<open>; if the open fails, the object is destroyed. Otherwise,
55 it is returned to the caller.
63 =item open( FILENAME [,MODE [,PERMS]] )
65 C<open> accepts one, two or three parameters. With one parameter,
66 it is just a front end for the built-in C<open> function. With two
67 parameters, the first parameter is a filename that may include
68 whitespace or other special characters, and the second parameter is
69 the open mode, optionally followed by a file permission value.
71 If C<IO::File::open> receives a Perl mode string ("E<gt>", "+E<lt>", etc.)
72 or a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic
73 Perl C<open> operator.
75 If C<IO::File::open> is given a numeric mode, it passes that mode
76 and the optional permissions value to the Perl C<sysopen> operator.
77 For convenience, C<IO::File::import> tries to import the O_XXX
78 constants from the Fcntl module. If dynamic loading is not available,
79 this may fail, but the rest of IO::File will still work.
86 L<perlop/"I/O Operators">,
92 Derived from FileHandle.pm by Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>.
101 use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD);
105 use IO::Handle qw(_open_mode_string);
111 @ISA = qw(IO::Handle IO::Seekable Exporter DynaLoader);
113 $VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/);
115 @EXPORT = @IO::Seekable::EXPORT;
119 my $callpkg = caller;
120 Exporter::export $pkg, $callpkg, @_;
123 # If the Fcntl extension is available,
124 # export its constants for sysopen().
128 Exporter::export 'Fcntl', $callpkg, '/^O_/';
133 ################################################
139 my $class = ref($type) || $type || "IO::File";
141 or croak "usage: new $class [FILENAME [,MODE [,PERMS]]]";
142 my $fh = $class->SUPER::new();
150 ################################################
155 @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
156 my ($fh, $file) = @_;
158 my ($mode, $perms) = @_[2, 3];
159 if ($mode =~ /^\d+$/) {
160 defined $perms or $perms = 0666;
161 return sysopen($fh, $file, $mode, $perms);
163 $file = "./" . $file unless $file =~ m#^/#;
164 $file = _open_mode_string($mode) . " $file\0";