Add IO extension
[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
56the open mode in either Perl form (">", "+<", etc.) or POSIX form
57("w", "r+", etc.).
58
59=head1 SEE ALSO
60
61L<perlfunc>,
62L<perlop/"I/O Operators">,
63L<"IO::Handle">
64L<"IO::Seekable">
65
66=head1 HISTORY
67
68Derived from FileHandle.pm by Graham Barr <bodg@tiuk.ti.com>
69
70=head1 REVISION
71
72$Revision: 1.3 $
73
74=cut
75
76require 5.000;
77use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD);
78use Carp;
79use Symbol;
80use English;
81use SelectSaver;
82use IO::Handle qw(_open_mode_string);
83use IO::Seekable;
84
85require Exporter;
86require DynaLoader;
87
88@ISA = qw(IO::Handle IO::Seekable Exporter DynaLoader);
89
90$VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
91
92@EXPORT = @IO::Seekable::EXPORT;
93
94################################################
95## If the Fcntl extension is available,
96## export its constants.
97##
98
99sub import {
100 my $pkg = shift;
101 my $callpkg = caller;
102 Exporter::export $pkg, $callpkg;
103 eval {
104 require Fcntl;
105 Exporter::export 'Fcntl', $callpkg;
106 };
107};
108
109
110################################################
111## Constructor
112##
113
114sub new {
115 @_ >= 1 && @_ <= 3 or croak 'usage: new IO::File [FILENAME [,MODE]]';
116 my $class = shift;
117 my $fh = $class->SUPER::new();
118 if (@_) {
119 $fh->open(@_)
120 or return undef;
121 }
122 $fh;
123}
124
125################################################
126## Open
127##
128
129sub open {
130 @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
131 my ($fh, $file) = @_;
132 if (@_ > 2) {
133 my ($mode, $perms) = @_[2, 3];
134 if ($mode =~ /^\d+$/) {
135 defined $perms or $perms = 0666;
136 return sysopen($fh, $file, $mode, $perms);
137 }
138 $file = "./" . $file unless $file =~ m#^/#;
139 $file = _open_mode_string($mode) . " $file\0";
140 }
141 open($fh, $file);
142}
143
1441;