[inseparable changes from patch from perl5.003_11 to perl5.003_12]
[p5sagit/p5-mst-13.2.git] / ext / IO / lib / IO / File.pm
CommitLineData
8add82fc 1package IO::File;
2
3=head1 NAME
4
5IO::File - supply object methods for filehandles
6
7=head1 SYNOPSIS
8
9 use IO::File;
10
11 $fh = new IO::File;
12 if ($fh->open "< file") {
13 print <$fh>;
14 $fh->close;
15 }
16
17 $fh = new IO::File "> FOO";
18 if (defined $fh) {
19 print $fh "bar\n";
20 $fh->close;
21 }
22
23 $fh = new IO::File "file", "r";
24 if (defined $fh) {
25 print <$fh>;
26 undef $fh; # automatically closes the file
27 }
28
29 $fh = new IO::File "file", O_WRONLY|O_APPEND;
30 if (defined $fh) {
31 print $fh "corge\n";
32 undef $fh; # automatically closes the file
33 }
34
35 $pos = $fh->getpos;
36 $fh->setpos $pos;
37
38 $fh->setvbuf($buffer_var, _IOLBF, 1024);
39
40 autoflush STDOUT 1;
41
42=head1 DESCRIPTION
43
55497cff 44C<IO::File> inherits from C<IO::Handle> and C<IO::Seekable>. It extends
27d4819a 45these classes with methods that are specific to file handles.
8add82fc 46
27d4819a 47=head1 CONSTRUCTOR
48
49=over 4
50
51=item new ([ ARGS ] )
52
53Creates a C<IO::File>. If it receives any parameters, they are passed to
54the method C<open>; if the open fails, the object is destroyed. Otherwise,
55it is returned to the caller.
56
57=back
58
59=head1 METHODS
60
61=over 4
62
63=item open( FILENAME [,MODE [,PERMS]] )
64
65C<open> accepts one, two or three parameters. With one parameter,
8add82fc 66it is just a front end for the built-in C<open> function. With two
67parameters, the first parameter is a filename that may include
68whitespace or other special characters, and the second parameter is
223d223e 69the open mode, optionally followed by a file permission value.
70
27d4819a 71If C<IO::File::open> receives a Perl mode string ("E<gt>", "+E<lt>", etc.)
223d223e 72or a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic
73Perl C<open> operator.
74
75If C<IO::File::open> is given a numeric mode, it passes that mode
76and the optional permissions value to the Perl C<sysopen> operator.
77For convenience, C<IO::File::import> tries to import the O_XXX
78constants from the Fcntl module. If dynamic loading is not available,
79this may fail, but the rest of IO::File will still work.
8add82fc 80
27d4819a 81=back
82
8add82fc 83=head1 SEE ALSO
84
85L<perlfunc>,
86L<perlop/"I/O Operators">,
27d4819a 87L<IO::Handle>
88L<IO::Seekable>
8add82fc 89
90=head1 HISTORY
91
27d4819a 92Derived from FileHandle.pm by Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>.
8add82fc 93
94=head1 REVISION
95
27d4819a 96$Revision: 1.5 $
8add82fc 97
98=cut
99
100require 5.000;
101use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD);
102use Carp;
103use Symbol;
8add82fc 104use SelectSaver;
105use IO::Handle qw(_open_mode_string);
106use IO::Seekable;
107
108require Exporter;
109require DynaLoader;
110
111@ISA = qw(IO::Handle IO::Seekable Exporter DynaLoader);
112
27d4819a 113$VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/);
8add82fc 114
115@EXPORT = @IO::Seekable::EXPORT;
116
8add82fc 117sub import {
118 my $pkg = shift;
119 my $callpkg = caller;
1bea6b6d 120 Exporter::export $pkg, $callpkg, @_;
121
122 #
123 # If the Fcntl extension is available,
124 # export its constants for sysopen().
125 #
8add82fc 126 eval {
127 require Fcntl;
1bea6b6d 128 Exporter::export 'Fcntl', $callpkg, '/^O_/';
8add82fc 129 };
1bea6b6d 130}
8add82fc 131
132
133################################################
134## Constructor
135##
136
137sub new {
27d4819a 138 my $type = shift;
139 my $class = ref($type) || $type || "IO::File";
140 @_ >= 0 && @_ <= 3
141 or croak "usage: new $class [FILENAME [,MODE [,PERMS]]]";
8add82fc 142 my $fh = $class->SUPER::new();
143 if (@_) {
144 $fh->open(@_)
145 or return undef;
146 }
147 $fh;
148}
149
150################################################
151## Open
152##
153
154sub open {
155 @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
156 my ($fh, $file) = @_;
157 if (@_ > 2) {
158 my ($mode, $perms) = @_[2, 3];
159 if ($mode =~ /^\d+$/) {
160 defined $perms or $perms = 0666;
161 return sysopen($fh, $file, $mode, $perms);
162 }
163 $file = "./" . $file unless $file =~ m#^/#;
164 $file = _open_mode_string($mode) . " $file\0";
165 }
166 open($fh, $file);
167}
168
1691;