Commit | Line | Data |
8add82fc |
1 | # |
2 | |
3 | package IO::File; |
4 | |
5 | =head1 NAME |
6 | |
7 | IO::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 | |
46 | C<IO::File::new> creates a C<IO::File>, which is a reference to a |
47 | newly created symbol (see the C<Symbol> package). If it receives any |
48 | parameters, they are passed to C<IO::File::open>; if the open fails, |
49 | the C<IO::File> object is destroyed. Otherwise, it is returned to |
50 | the caller. |
51 | |
52 | C<IO::File::open> accepts one parameter or two. With one parameter, |
53 | it is just a front end for the built-in C<open> function. With two |
54 | parameters, the first parameter is a filename that may include |
55 | whitespace or other special characters, and the second parameter is |
223d223e |
56 | the open mode, optionally followed by a file permission value. |
57 | |
58 | If C<IO::File::open> receives a Perl mode string (">", "+<", etc.) |
59 | or a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic |
60 | Perl C<open> operator. |
61 | |
62 | If C<IO::File::open> is given a numeric mode, it passes that mode |
63 | and the optional permissions value to the Perl C<sysopen> operator. |
64 | For convenience, C<IO::File::import> tries to import the O_XXX |
65 | constants from the Fcntl module. If dynamic loading is not available, |
66 | this may fail, but the rest of IO::File will still work. |
8add82fc |
67 | |
68 | =head1 SEE ALSO |
69 | |
70 | L<perlfunc>, |
71 | L<perlop/"I/O Operators">, |
72 | L<"IO::Handle"> |
73 | L<"IO::Seekable"> |
74 | |
75 | =head1 HISTORY |
76 | |
77 | Derived from FileHandle.pm by Graham Barr <bodg@tiuk.ti.com> |
78 | |
79 | =head1 REVISION |
80 | |
81 | $Revision: 1.3 $ |
82 | |
83 | =cut |
84 | |
85 | require 5.000; |
86 | use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD); |
87 | use Carp; |
88 | use Symbol; |
89 | use English; |
90 | use SelectSaver; |
91 | use IO::Handle qw(_open_mode_string); |
92 | use IO::Seekable; |
93 | |
94 | require Exporter; |
95 | require 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 | |
108 | sub 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 | |
123 | sub 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 | |
139 | sub 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 | |
154 | 1; |