m2t2 broke CPAN.pm :-(
[p5sagit/p5-mst-13.2.git] / ext / IO / lib / IO / Handle.pm
CommitLineData
774d564b 1
8add82fc 2package IO::Handle;
3
4=head1 NAME
5
27d4819a 6IO::Handle - supply object methods for I/O handles
8add82fc 7
8=head1 SYNOPSIS
9
10 use IO::Handle;
11
12 $fh = new IO::Handle;
774d564b 13 if ($fh->fdopen(fileno(STDIN),"r")) {
14 print $fh->getline;
8add82fc 15 $fh->close;
16 }
17
774d564b 18 $fh = new IO::Handle;
19 if ($fh->fdopen(fileno(STDOUT),"w")) {
20 $fh->print("Some text\n");
8add82fc 21 }
22
8add82fc 23 $fh->setvbuf($buffer_var, _IOLBF, 1024);
24
774d564b 25 undef $fh; # automatically closes the file if it's open
26
8add82fc 27 autoflush STDOUT 1;
28
29=head1 DESCRIPTION
30
774d564b 31C<IO::Handle> is the base class for all other IO handle classes. It is
32not intended that objects of C<IO::Handle> would be created directly,
33but instead C<IO::Handle> is inherited from by several other classes
34in the IO hierarchy.
35
36If you are reading this documentation, looking for a replacement for
37the C<FileHandle> package, then I suggest you read the documentation
38for C<IO::File>
39
27d4819a 40A C<IO::Handle> object is a reference to a symbol (see the C<Symbol> package)
8add82fc 41
27d4819a 42=head1 CONSTRUCTOR
43
44=over 4
45
46=item new ()
8add82fc 47
27d4819a 48Creates a new C<IO::Handle> object.
8add82fc 49
27d4819a 50=item new_from_fd ( FD, MODE )
51
52Creates a C<IO::Handle> like C<new> does.
53It requires two parameters, which are passed to the method C<fdopen>;
54if the fdopen fails, the object is destroyed. Otherwise, it is returned
55to the caller.
56
57=back
58
59=head1 METHODS
8add82fc 60
8add82fc 61See L<perlfunc> for complete descriptions of each of the following
62supported C<IO::Handle> methods, which are just front ends for the
63corresponding built-in functions:
a6006777 64
8add82fc 65 close
66 fileno
67 getc
8add82fc 68 eof
69 read
70 truncate
71 stat
27d4819a 72 print
73 printf
74 sysread
75 syswrite
8add82fc 76
77See L<perlvar> for complete descriptions of each of the following
78supported C<IO::Handle> methods:
79
80 autoflush
81 output_field_separator
82 output_record_separator
83 input_record_separator
84 input_line_number
85 format_page_number
86 format_lines_per_page
87 format_lines_left
88 format_name
89 format_top_name
90 format_line_break_characters
91 format_formfeed
92 format_write
93
94Furthermore, for doing normal I/O you might need these:
95
96=over
97
948ecc40 98=item $fh->fdopen ( FD, MODE )
99
100C<fdopen> is like an ordinary C<open> except that its first parameter
101is not a filename but rather a file handle name, a IO::Handle object,
102or a file descriptor number.
103
104=item $fh->opened
105
106Returns true if the object is currently a valid file descriptor.
107
8add82fc 108=item $fh->getline
109
110This works like <$fh> described in L<perlop/"I/O Operators">
111except that it's more readable and can be safely called in an
112array context but still returns just one line.
113
114=item $fh->getlines
115
116This works like <$fh> when called in an array context to
117read all the remaining lines in a file, except that it's more readable.
118It will also croak() if accidentally called in a scalar context.
119
948ecc40 120=item $fh->ungetc ( ORD )
27d4819a 121
948ecc40 122Pushes a character with the given ordinal value back onto the given
123handle's input stream.
27d4819a 124
125=item $fh->write ( BUF, LEN [, OFFSET }\] )
126
948ecc40 127This C<write> is like C<write> found in C, that is it is the
27d4819a 128opposite of read. The wrapper for the perl C<write> function is
129called C<format_write>.
130
948ecc40 131=item $fh->flush
27d4819a 132
948ecc40 133Flush the given handle's buffer.
134
135=item $fh->error
136
137Returns a true value if the given handle has experienced any errors
138since it was opened or since the last call to C<clearerr>.
139
140=item $fh->clearerr
141
142Clear the given handle's error indicator.
27d4819a 143
8add82fc 144=back
145
948ecc40 146If the C functions setbuf() and/or setvbuf() are available, then
147C<IO::Handle::setbuf> and C<IO::Handle::setvbuf> set the buffering
148policy for an IO::Handle. The calling sequences for the Perl functions
149are the same as their C counterparts--including the constants C<_IOFBF>,
150C<_IOLBF>, and C<_IONBF> for setvbuf()--except that the buffer parameter
151specifies a scalar variable to use as a buffer. WARNING: A variable
152used as a buffer by C<setbuf> or C<setvbuf> must not be modified in any
153way until the IO::Handle is closed or C<setbuf> or C<setvbuf> is called
154again, or memory corruption may result!
155
156Lastly, there is a special method for working under B<-T> and setuid/gid
157scripts:
515e7bd7 158
159=over
160
161=item $fh->untaint
162
163Marks the object as taint-clean, and as such data read from it will also
164be considered taint-clean. Note that this is a very trusting action to
165take, and appropriate consideration for the data source and potential
166vulnerability should be kept in mind.
167
168=back
169
27d4819a 170=head1 NOTE
8add82fc 171
27d4819a 172A C<IO::Handle> object is a GLOB reference. Some modules that
8add82fc 173inherit from C<IO::Handle> may want to keep object related variables
174in the hash table part of the GLOB. In an attempt to prevent modules
175trampling on each other I propose the that any such module should prefix
176its variables with its own name separated by _'s. For example the IO::Socket
177module keeps a C<timeout> variable in 'io_socket_timeout'.
178
179=head1 SEE ALSO
180
181L<perlfunc>,
182L<perlop/"I/O Operators">,
774d564b 183L<IO::File>
8add82fc 184
185=head1 BUGS
186
187Due to backwards compatibility, all filehandles resemble objects
188of class C<IO::Handle>, or actually classes derived from that class.
189They actually aren't. Which means you can't derive your own
190class from C<IO::Handle> and inherit those methods.
191
192=head1 HISTORY
193
27d4819a 194Derived from FileHandle.pm by Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>
8add82fc 195
196=cut
197
198require 5.000;
7a4c00b4 199use strict;
774d564b 200use vars qw($VERSION $XS_VERSION @EXPORT_OK $AUTOLOAD @ISA);
8add82fc 201use Carp;
202use Symbol;
203use SelectSaver;
204
205require Exporter;
206@ISA = qw(Exporter);
207
948ecc40 208$VERSION = "1.1504";
774d564b 209$XS_VERSION = "1.15";
8add82fc 210
211@EXPORT_OK = qw(
212 autoflush
213 output_field_separator
214 output_record_separator
215 input_record_separator
216 input_line_number
217 format_page_number
218 format_lines_per_page
219 format_lines_left
220 format_name
221 format_top_name
222 format_line_break_characters
223 format_formfeed
224 format_write
225
226 print
227 printf
228 getline
229 getlines
230
231 SEEK_SET
232 SEEK_CUR
233 SEEK_END
234 _IOFBF
235 _IOLBF
236 _IONBF
8add82fc 237);
238
239
240################################################
241## Interaction with the XS.
242##
243
244require DynaLoader;
245@IO::ISA = qw(DynaLoader);
774d564b 246bootstrap IO $XS_VERSION;
8add82fc 247
248sub AUTOLOAD {
249 if ($AUTOLOAD =~ /::(_?[a-z])/) {
250 $AutoLoader::AUTOLOAD = $AUTOLOAD;
251 goto &AutoLoader::AUTOLOAD
252 }
253 my $constname = $AUTOLOAD;
254 $constname =~ s/.*:://;
255 my $val = constant($constname);
256 defined $val or croak "$constname is not a valid IO::Handle macro";
7a4c00b4 257 no strict 'refs';
8add82fc 258 *$AUTOLOAD = sub { $val };
259 goto &$AUTOLOAD;
260}
261
262
263################################################
264## Constructors, destructors.
265##
266
267sub new {
27d4819a 268 my $class = ref($_[0]) || $_[0] || "IO::Handle";
269 @_ == 1 or croak "usage: new $class";
8add82fc 270 my $fh = gensym;
271 bless $fh, $class;
272}
273
274sub new_from_fd {
27d4819a 275 my $class = ref($_[0]) || $_[0] || "IO::Handle";
276 @_ == 3 or croak "usage: new_from_fd $class FD, MODE";
8add82fc 277 my $fh = gensym;
c927212d 278 shift;
8add82fc 279 IO::Handle::fdopen($fh, @_)
280 or return undef;
281 bless $fh, $class;
8add82fc 282}
283
98d4926f 284#
285# There is no need for DESTROY to do anything, because when the
286# last reference to an IO object is gone, Perl automatically
287# closes its associated files (if any). However, to avoid any
288# attempts to autoload DESTROY, we here define it to do nothing.
289#
290sub DESTROY {}
7a4c00b4 291
8add82fc 292
293################################################
294## Open and close.
295##
296
297sub _open_mode_string {
298 my ($mode) = @_;
299 $mode =~ /^\+?(<|>>?)$/
300 or $mode =~ s/^r(\+?)$/$1</
301 or $mode =~ s/^w(\+?)$/$1>/
302 or $mode =~ s/^a(\+?)$/$1>>/
303 or croak "IO::Handle: bad open mode: $mode";
304 $mode;
305}
306
307sub fdopen {
308 @_ == 3 or croak 'usage: $fh->fdopen(FD, MODE)';
309 my ($fh, $fd, $mode) = @_;
310 local(*GLOB);
311
312 if (ref($fd) && "".$fd =~ /GLOB\(/o) {
313 # It's a glob reference; Alias it as we cannot get name of anon GLOBs
314 my $n = qualify(*GLOB);
315 *GLOB = *{*$fd};
316 $fd = $n;
317 } elsif ($fd =~ m#^\d+$#) {
318 # It's an FD number; prefix with "=".
319 $fd = "=$fd";
320 }
321
322 open($fh, _open_mode_string($mode) . '&' . $fd)
323 ? $fh : undef;
324}
325
326sub close {
327 @_ == 1 or croak 'usage: $fh->close()';
328 my($fh) = @_;
8add82fc 329
774d564b 330 close($fh);
8add82fc 331}
332
333################################################
334## Normal I/O functions.
335##
336
8add82fc 337# flock
8add82fc 338# select
8add82fc 339
340sub opened {
341 @_ == 1 or croak 'usage: $fh->opened()';
342 defined fileno($_[0]);
343}
344
345sub fileno {
346 @_ == 1 or croak 'usage: $fh->fileno()';
347 fileno($_[0]);
348}
349
350sub getc {
351 @_ == 1 or croak 'usage: $fh->getc()';
352 getc($_[0]);
353}
354
8add82fc 355sub eof {
356 @_ == 1 or croak 'usage: $fh->eof()';
357 eof($_[0]);
358}
359
360sub print {
361 @_ or croak 'usage: $fh->print([ARGS])';
362 my $this = shift;
363 print $this @_;
364}
365
366sub printf {
367 @_ >= 2 or croak 'usage: $fh->printf(FMT,[ARGS])';
368 my $this = shift;
369 printf $this @_;
370}
371
372sub getline {
373 @_ == 1 or croak 'usage: $fh->getline';
374 my $this = shift;
375 return scalar <$this>;
376}
377
f86702cc 378*gets = \&getline; # deprecated
379
8add82fc 380sub getlines {
381 @_ == 1 or croak 'usage: $fh->getline()';
8add82fc 382 wantarray or
27d4819a 383 croak 'Can\'t call $fh->getlines in a scalar context, use $fh->getline';
384 my $this = shift;
8add82fc 385 return <$this>;
386}
387
388sub truncate {
389 @_ == 2 or croak 'usage: $fh->truncate(LEN)';
390 truncate($_[0], $_[1]);
391}
392
393sub read {
394 @_ == 3 || @_ == 4 or croak '$fh->read(BUF, LEN [, OFFSET])';
395 read($_[0], $_[1], $_[2], $_[3] || 0);
396}
397
27d4819a 398sub sysread {
399 @_ == 3 || @_ == 4 or croak '$fh->sysread(BUF, LEN [, OFFSET])';
400 sysread($_[0], $_[1], $_[2], $_[3] || 0);
401}
402
8add82fc 403sub write {
404 @_ == 3 || @_ == 4 or croak '$fh->write(BUF, LEN [, OFFSET])';
405 local($\) = "";
406 print { $_[0] } substr($_[1], $_[3] || 0, $_[2]);
407}
408
27d4819a 409sub syswrite {
410 @_ == 3 || @_ == 4 or croak '$fh->syswrite(BUF, LEN [, OFFSET])';
5f05dabc 411 syswrite($_[0], $_[1], $_[2], $_[3] || 0);
27d4819a 412}
413
8add82fc 414sub stat {
415 @_ == 1 or croak 'usage: $fh->stat()';
416 stat($_[0]);
417}
418
419################################################
420## State modification functions.
421##
422
423sub autoflush {
424 my $old = new SelectSaver qualify($_[0], caller);
425 my $prev = $|;
426 $| = @_ > 1 ? $_[1] : 1;
427 $prev;
428}
429
430sub output_field_separator {
431 my $old = new SelectSaver qualify($_[0], caller);
432 my $prev = $,;
433 $, = $_[1] if @_ > 1;
434 $prev;
435}
436
437sub output_record_separator {
438 my $old = new SelectSaver qualify($_[0], caller);
439 my $prev = $\;
440 $\ = $_[1] if @_ > 1;
441 $prev;
442}
443
444sub input_record_separator {
445 my $old = new SelectSaver qualify($_[0], caller);
446 my $prev = $/;
447 $/ = $_[1] if @_ > 1;
448 $prev;
449}
450
451sub input_line_number {
452 my $old = new SelectSaver qualify($_[0], caller);
453 my $prev = $.;
454 $. = $_[1] if @_ > 1;
455 $prev;
456}
457
458sub format_page_number {
459 my $old = new SelectSaver qualify($_[0], caller);
460 my $prev = $%;
461 $% = $_[1] if @_ > 1;
462 $prev;
463}
464
465sub format_lines_per_page {
466 my $old = new SelectSaver qualify($_[0], caller);
467 my $prev = $=;
468 $= = $_[1] if @_ > 1;
469 $prev;
470}
471
472sub format_lines_left {
473 my $old = new SelectSaver qualify($_[0], caller);
474 my $prev = $-;
475 $- = $_[1] if @_ > 1;
476 $prev;
477}
478
479sub format_name {
480 my $old = new SelectSaver qualify($_[0], caller);
481 my $prev = $~;
482 $~ = qualify($_[1], caller) if @_ > 1;
483 $prev;
484}
485
486sub format_top_name {
487 my $old = new SelectSaver qualify($_[0], caller);
488 my $prev = $^;
489 $^ = qualify($_[1], caller) if @_ > 1;
490 $prev;
491}
492
493sub format_line_break_characters {
494 my $old = new SelectSaver qualify($_[0], caller);
495 my $prev = $:;
496 $: = $_[1] if @_ > 1;
497 $prev;
498}
499
500sub format_formfeed {
501 my $old = new SelectSaver qualify($_[0], caller);
502 my $prev = $^L;
503 $^L = $_[1] if @_ > 1;
504 $prev;
505}
506
507sub formline {
508 my $fh = shift;
509 my $picture = shift;
510 local($^A) = $^A;
511 local($\) = "";
512 formline($picture, @_);
513 print $fh $^A;
514}
515
516sub format_write {
517 @_ < 3 || croak 'usage: $fh->write( [FORMAT_NAME] )';
518 if (@_ == 2) {
519 my ($fh, $fmt) = @_;
520 my $oldfmt = $fh->format_name($fmt);
521 write($fh);
522 $fh->format_name($oldfmt);
523 } else {
524 write($_[0]);
525 }
526}
527
27d4819a 528sub fcntl {
529 @_ == 3 || croak 'usage: $fh->fcntl( OP, VALUE );';
530 my ($fh, $op, $val) = @_;
531 my $r = fcntl($fh, $op, $val);
532 defined $r && $r eq "0 but true" ? 0 : $r;
533}
534
535sub ioctl {
536 @_ == 3 || croak 'usage: $fh->ioctl( OP, VALUE );';
537 my ($fh, $op, $val) = @_;
538 my $r = ioctl($fh, $op, $val);
539 defined $r && $r eq "0 but true" ? 0 : $r;
540}
8add82fc 541
5421;