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