File manager - Edit - /home/justdoit/brinkshipping.co.uk/IO.zip
Back
PK d�\�[f� � Dir.pmnu �[��� # IO::Dir.pm # # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. package IO::Dir; use 5.006; use strict; use Carp; use Symbol; use Exporter; use IO::File; our(@ISA, $VERSION, @EXPORT_OK); use Tie::Hash; use File::stat; use File::Spec; @ISA = qw(Tie::Hash Exporter); $VERSION = "1.10"; $VERSION = eval $VERSION; @EXPORT_OK = qw(DIR_UNLINK); sub DIR_UNLINK () { 1 } sub new { @_ >= 1 && @_ <= 2 or croak 'usage: IO::Dir->new([DIRNAME])'; my $class = shift; my $dh = gensym; if (@_) { IO::Dir::open($dh, $_[0]) or return undef; } bless $dh, $class; } sub DESTROY { my ($dh) = @_; local($., $@, $!, $^E, $?); no warnings 'io'; closedir($dh); } sub open { @_ == 2 or croak 'usage: $dh->open(DIRNAME)'; my ($dh, $dirname) = @_; return undef unless opendir($dh, $dirname); # a dir name should always have a ":" in it; assume dirname is # in current directory $dirname = ':' . $dirname if ( ($^O eq 'MacOS') && ($dirname !~ /:/) ); ${*$dh}{io_dir_path} = $dirname; 1; } sub close { @_ == 1 or croak 'usage: $dh->close()'; my ($dh) = @_; closedir($dh); } sub read { @_ == 1 or croak 'usage: $dh->read()'; my ($dh) = @_; readdir($dh); } sub seek { @_ == 2 or croak 'usage: $dh->seek(POS)'; my ($dh,$pos) = @_; seekdir($dh,$pos); } sub tell { @_ == 1 or croak 'usage: $dh->tell()'; my ($dh) = @_; telldir($dh); } sub rewind { @_ == 1 or croak 'usage: $dh->rewind()'; my ($dh) = @_; rewinddir($dh); } sub TIEHASH { my($class,$dir,$options) = @_; my $dh = $class->new($dir) or return undef; $options ||= 0; ${*$dh}{io_dir_unlink} = $options & DIR_UNLINK; $dh; } sub FIRSTKEY { my($dh) = @_; $dh->rewind; scalar $dh->read; } sub NEXTKEY { my($dh) = @_; scalar $dh->read; } sub EXISTS { my($dh,$key) = @_; -e File::Spec->catfile(${*$dh}{io_dir_path}, $key); } sub FETCH { my($dh,$key) = @_; &lstat(File::Spec->catfile(${*$dh}{io_dir_path}, $key)); } sub STORE { my($dh,$key,$data) = @_; my($atime,$mtime) = ref($data) ? @$data : ($data,$data); my $file = File::Spec->catfile(${*$dh}{io_dir_path}, $key); unless(-e $file) { my $io = IO::File->new($file,O_CREAT | O_RDWR); $io->close if $io; } utime($atime,$mtime, $file); } sub DELETE { my($dh,$key) = @_; # Only unlink if unlink-ing is enabled return 0 unless ${*$dh}{io_dir_unlink}; my $file = File::Spec->catfile(${*$dh}{io_dir_path}, $key); -d $file ? rmdir($file) : unlink($file); } 1; __END__ =head1 NAME IO::Dir - supply object methods for directory handles =head1 SYNOPSIS use IO::Dir; $d = IO::Dir->new("."); if (defined $d) { while (defined($_ = $d->read)) { something($_); } $d->rewind; while (defined($_ = $d->read)) { something_else($_); } undef $d; } tie %dir, 'IO::Dir', "."; foreach (keys %dir) { print $_, " " , $dir{$_}->size,"\n"; } =head1 DESCRIPTION The C<IO::Dir> package provides two interfaces to perl's directory reading routines. The first interface is an object approach. C<IO::Dir> provides an object constructor and methods, which are just wrappers around perl's built in directory reading routines. =over 4 =item new ( [ DIRNAME ] ) C<new> is the constructor for C<IO::Dir> objects. It accepts one optional argument which, if given, C<new> will pass to C<open> =back The following methods are wrappers for the directory related functions built into perl (the trailing 'dir' has been removed from the names). See L<perlfunc> for details of these functions. =over 4 =item open ( DIRNAME ) =item read () =item seek ( POS ) =item tell () =item rewind () =item close () =back C<IO::Dir> also provides an interface to reading directories via a tied hash. The tied hash extends the interface beyond just the directory reading routines by the use of C<lstat>, from the C<File::stat> package, C<unlink>, C<rmdir> and C<utime>. =over 4 =item tie %hash, 'IO::Dir', DIRNAME [, OPTIONS ] =back The keys of the hash will be the names of the entries in the directory. Reading a value from the hash will be the result of calling C<File::stat::lstat>. Deleting an element from the hash will delete the corresponding file or subdirectory, provided that C<DIR_UNLINK> is included in the C<OPTIONS>. Assigning to an entry in the hash will cause the time stamps of the file to be modified. If the file does not exist then it will be created. Assigning a single integer to a hash element will cause both the access and modification times to be changed to that value. Alternatively a reference to an array of two values can be passed. The first array element will be used to set the access time and the second element will be used to set the modification time. =head1 SEE ALSO L<File::stat> =head1 AUTHOR Graham Barr. Currently maintained by the Perl Porters. Please report all bugs to <perlbug@perl.org>. =head1 COPYRIGHT Copyright (c) 1997-2003 Graham Barr <gbarr@pobox.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut PK d�\�& & File.pmnu �[��� # package IO::File; =head1 NAME IO::File - supply object methods for filehandles =head1 SYNOPSIS use IO::File; $fh = IO::File->new(); if ($fh->open("< file")) { print <$fh>; $fh->close; } $fh = IO::File->new("> file"); if (defined $fh) { print $fh "bar\n"; $fh->close; } $fh = IO::File->new("file", "r"); if (defined $fh) { print <$fh>; undef $fh; # automatically closes the file } $fh = IO::File->new("file", O_WRONLY|O_APPEND); if (defined $fh) { print $fh "corge\n"; $pos = $fh->getpos; $fh->setpos($pos); undef $fh; # automatically closes the file } autoflush STDOUT 1; =head1 DESCRIPTION C<IO::File> inherits from C<IO::Handle> and C<IO::Seekable>. It extends these classes with methods that are specific to file handles. =head1 CONSTRUCTOR =over 4 =item new ( FILENAME [,MODE [,PERMS]] ) Creates an C<IO::File>. If it receives any parameters, they are passed to the method C<open>; if the open fails, the object is destroyed. Otherwise, it is returned to the caller. =item new_tmpfile Creates an C<IO::File> opened for read/write on a newly created temporary file. On systems where this is possible, the temporary file is anonymous (i.e. it is unlinked after creation, but held open). If the temporary file cannot be created or opened, the C<IO::File> object is destroyed. Otherwise, it is returned to the caller. =back =head1 METHODS =over 4 =item open( FILENAME [,MODE [,PERMS]] ) =item open( FILENAME, IOLAYERS ) C<open> accepts one, two or three parameters. With one parameter, it is just a front end for the built-in C<open> function. With two or three parameters, the first parameter is a filename that may include whitespace or other special characters, and the second parameter is the open mode, optionally followed by a file permission value. If C<IO::File::open> receives a Perl mode string ("E<gt>", "+E<lt>", etc.) or an ANSI C fopen() mode string ("w", "r+", etc.), it uses the basic Perl C<open> operator (but protects any special characters). If C<IO::File::open> is given a numeric mode, it passes that mode and the optional permissions value to the Perl C<sysopen> operator. The permissions default to 0666. If C<IO::File::open> is given a mode that includes the C<:> character, it passes all the three arguments to the three-argument C<open> operator. For convenience, C<IO::File> exports the O_XXX constants from the Fcntl module, if this module is available. =item binmode( [LAYER] ) C<binmode> sets C<binmode> on the underlying C<IO> object, as documented in C<perldoc -f binmode>. C<binmode> accepts one optional parameter, which is the layer to be passed on to the C<binmode> call. =back =head1 NOTE Some operating systems may perform C<IO::File::new()> or C<IO::File::open()> on a directory without errors. This behavior is not portable and not suggested for use. Using C<opendir()> and C<readdir()> or C<IO::Dir> are suggested instead. =head1 SEE ALSO L<perlfunc>, L<perlop/"I/O Operators">, L<IO::Handle>, L<IO::Seekable>, L<IO::Dir> =head1 HISTORY Derived from FileHandle.pm by Graham Barr E<lt>F<gbarr@pobox.com>E<gt>. =cut use 5.006_001; use strict; our($VERSION, @EXPORT, @EXPORT_OK, @ISA); use Carp; use Symbol; use SelectSaver; use IO::Seekable; require Exporter; @ISA = qw(IO::Handle IO::Seekable Exporter); $VERSION = "1.16"; @EXPORT = @IO::Seekable::EXPORT; eval { # Make all Fcntl O_XXX constants available for importing require Fcntl; my @O = grep /^O_/, @Fcntl::EXPORT; Fcntl->import(@O); # first we import what we want to export push(@EXPORT, @O); }; ################################################ ## Constructor ## sub new { my $type = shift; my $class = ref($type) || $type || "IO::File"; @_ >= 0 && @_ <= 3 or croak "usage: $class->new([FILENAME [,MODE [,PERMS]]])"; my $fh = $class->SUPER::new(); if (@_) { $fh->open(@_) or return undef; } $fh; } ################################################ ## Open ## sub open { @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])'; my ($fh, $file) = @_; if (@_ > 2) { my ($mode, $perms) = @_[2, 3]; if ($mode =~ /^\d+$/) { defined $perms or $perms = 0666; return sysopen($fh, $file, $mode, $perms); } elsif ($mode =~ /:/) { return open($fh, $mode, $file) if @_ == 3; croak 'usage: $fh->open(FILENAME, IOLAYERS)'; } else { return open($fh, IO::Handle::_open_mode_string($mode), $file); } } open($fh, $file); } ################################################ ## Binmode ## sub binmode { ( @_ == 1 or @_ == 2 ) or croak 'usage $fh->binmode([LAYER])'; my($fh, $layer) = @_; return binmode $$fh unless $layer; return binmode $$fh, $layer; } 1; PK d�\3Y�8� � Select.pmnu �[��� # IO::Select.pm # # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. package IO::Select; use strict; use warnings::register; use vars qw($VERSION @ISA); require Exporter; $VERSION = "1.22"; @ISA = qw(Exporter); # This is only so we can do version checking sub VEC_BITS () {0} sub FD_COUNT () {1} sub FIRST_FD () {2} sub new { my $self = shift; my $type = ref($self) || $self; my $vec = bless [undef,0], $type; $vec->add(@_) if @_; $vec; } sub add { shift->_update('add', @_); } sub remove { shift->_update('remove', @_); } sub exists { my $vec = shift; my $fno = $vec->_fileno(shift); return undef unless defined $fno; $vec->[$fno + FIRST_FD]; } sub _fileno { my($self, $f) = @_; return unless defined $f; $f = $f->[0] if ref($f) eq 'ARRAY'; ($f =~ /^\d+$/) ? $f : fileno($f); } sub _update { my $vec = shift; my $add = shift eq 'add'; my $bits = $vec->[VEC_BITS]; $bits = '' unless defined $bits; my $count = 0; my $f; foreach $f (@_) { my $fn = $vec->_fileno($f); if ($add) { next unless defined $fn; my $i = $fn + FIRST_FD; if (defined $vec->[$i]) { $vec->[$i] = $f; # if array rest might be different, so we update next; } $vec->[FD_COUNT]++; vec($bits, $fn, 1) = 1; $vec->[$i] = $f; } else { # remove if ( ! defined $fn ) { # remove if fileno undef'd $fn = 0; for my $fe (@{$vec}[FIRST_FD .. $#$vec]) { if (defined($fe) && $fe == $f) { $vec->[FD_COUNT]--; $fe = undef; vec($bits, $fn, 1) = 0; last; } ++$fn; } } else { my $i = $fn + FIRST_FD; next unless defined $vec->[$i]; $vec->[FD_COUNT]--; vec($bits, $fn, 1) = 0; $vec->[$i] = undef; } } $count++; } $vec->[VEC_BITS] = $vec->[FD_COUNT] ? $bits : undef; $count; } sub can_read { my $vec = shift; my $timeout = shift; my $r = $vec->[VEC_BITS]; defined($r) && (select($r,undef,undef,$timeout) > 0) ? handles($vec, $r) : (); } sub can_write { my $vec = shift; my $timeout = shift; my $w = $vec->[VEC_BITS]; defined($w) && (select(undef,$w,undef,$timeout) > 0) ? handles($vec, $w) : (); } sub has_exception { my $vec = shift; my $timeout = shift; my $e = $vec->[VEC_BITS]; defined($e) && (select(undef,undef,$e,$timeout) > 0) ? handles($vec, $e) : (); } sub has_error { warnings::warn("Call to deprecated method 'has_error', use 'has_exception'") if warnings::enabled(); goto &has_exception; } sub count { my $vec = shift; $vec->[FD_COUNT]; } sub bits { my $vec = shift; $vec->[VEC_BITS]; } sub as_string # for debugging { my $vec = shift; my $str = ref($vec) . ": "; my $bits = $vec->bits; my $count = $vec->count; $str .= defined($bits) ? unpack("b*", $bits) : "undef"; $str .= " $count"; my @handles = @$vec; splice(@handles, 0, FIRST_FD); for (@handles) { $str .= " " . (defined($_) ? "$_" : "-"); } $str; } sub _max { my($a,$b,$c) = @_; $a > $b ? $a > $c ? $a : $c : $b > $c ? $b : $c; } sub select { shift if defined $_[0] && !ref($_[0]); my($r,$w,$e,$t) = @_; my @result = (); my $rb = defined $r ? $r->[VEC_BITS] : undef; my $wb = defined $w ? $w->[VEC_BITS] : undef; my $eb = defined $e ? $e->[VEC_BITS] : undef; if(select($rb,$wb,$eb,$t) > 0) { my @r = (); my @w = (); my @e = (); my $i = _max(defined $r ? scalar(@$r)-1 : 0, defined $w ? scalar(@$w)-1 : 0, defined $e ? scalar(@$e)-1 : 0); for( ; $i >= FIRST_FD ; $i--) { my $j = $i - FIRST_FD; push(@r, $r->[$i]) if defined $rb && defined $r->[$i] && vec($rb, $j, 1); push(@w, $w->[$i]) if defined $wb && defined $w->[$i] && vec($wb, $j, 1); push(@e, $e->[$i]) if defined $eb && defined $e->[$i] && vec($eb, $j, 1); } @result = (\@r, \@w, \@e); } @result; } sub handles { my $vec = shift; my $bits = shift; my @h = (); my $i; my $max = scalar(@$vec) - 1; for ($i = FIRST_FD; $i <= $max; $i++) { next unless defined $vec->[$i]; push(@h, $vec->[$i]) if !defined($bits) || vec($bits, $i - FIRST_FD, 1); } @h; } 1; __END__ =head1 NAME IO::Select - OO interface to the select system call =head1 SYNOPSIS use IO::Select; $s = IO::Select->new(); $s->add(\*STDIN); $s->add($some_handle); @ready = $s->can_read($timeout); @ready = IO::Select->new(@handles)->can_read(0); =head1 DESCRIPTION The C<IO::Select> package implements an object approach to the system C<select> function call. It allows the user to see what IO handles, see L<IO::Handle>, are ready for reading, writing or have an exception pending. =head1 CONSTRUCTOR =over 4 =item new ( [ HANDLES ] ) The constructor creates a new object and optionally initialises it with a set of handles. =back =head1 METHODS =over 4 =item add ( HANDLES ) Add the list of handles to the C<IO::Select> object. It is these values that will be returned when an event occurs. C<IO::Select> keeps these values in a cache which is indexed by the C<fileno> of the handle, so if more than one handle with the same C<fileno> is specified then only the last one is cached. Each handle can be an C<IO::Handle> object, an integer or an array reference where the first element is an C<IO::Handle> or an integer. =item remove ( HANDLES ) Remove all the given handles from the object. This method also works by the C<fileno> of the handles. So the exact handles that were added need not be passed, just handles that have an equivalent C<fileno> =item exists ( HANDLE ) Returns a true value (actually the handle itself) if it is present. Returns undef otherwise. =item handles Return an array of all registered handles. =item can_read ( [ TIMEOUT ] ) Return an array of handles that are ready for reading. C<TIMEOUT> is the maximum amount of time to wait before returning an empty list, in seconds, possibly fractional. If C<TIMEOUT> is not given and any handles are registered then the call will block. =item can_write ( [ TIMEOUT ] ) Same as C<can_read> except check for handles that can be written to. =item has_exception ( [ TIMEOUT ] ) Same as C<can_read> except check for handles that have an exception condition, for example pending out-of-band data. =item count () Returns the number of handles that the object will check for when one of the C<can_> methods is called or the object is passed to the C<select> static method. =item bits() Return the bit string suitable as argument to the core select() call. =item select ( READ, WRITE, EXCEPTION [, TIMEOUT ] ) C<select> is a static method, that is you call it with the package name like C<new>. C<READ>, C<WRITE> and C<EXCEPTION> are either C<undef> or C<IO::Select> objects. C<TIMEOUT> is optional and has the same effect as for the core select call. The result will be an array of 3 elements, each a reference to an array which will hold the handles that are ready for reading, writing and have exceptions respectively. Upon error an empty list is returned. =back =head1 EXAMPLE Here is a short example which shows how C<IO::Select> could be used to write a server which communicates with several sockets while also listening for more connections on a listen socket use IO::Select; use IO::Socket; $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => 8080); $sel = IO::Select->new( $lsn ); while(@ready = $sel->can_read) { foreach $fh (@ready) { if($fh == $lsn) { # Create a new socket $new = $lsn->accept; $sel->add($new); } else { # Process socket # Maybe we have finished with the socket $sel->remove($fh); $fh->close; } } } =head1 AUTHOR Graham Barr. Currently maintained by the Perl Porters. Please report all bugs to <perlbug@perl.org>. =head1 COPYRIGHT Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut PK d�\ ��)C C Handle.pmnu �[��� package IO::Handle; =head1 NAME IO::Handle - supply object methods for I/O handles =head1 SYNOPSIS use IO::Handle; $io = IO::Handle->new(); if ($io->fdopen(fileno(STDIN),"r")) { print $io->getline; $io->close; } $io = IO::Handle->new(); if ($io->fdopen(fileno(STDOUT),"w")) { $io->print("Some text\n"); } # setvbuf is not available by default on Perls 5.8.0 and later. use IO::Handle '_IOLBF'; $io->setvbuf($buffer_var, _IOLBF, 1024); undef $io; # automatically closes the file if it's open autoflush STDOUT 1; =head1 DESCRIPTION C<IO::Handle> is the base class for all other IO handle classes. It is not intended that objects of C<IO::Handle> would be created directly, but instead C<IO::Handle> is inherited from by several other classes in the IO hierarchy. If you are reading this documentation, looking for a replacement for the C<FileHandle> package, then I suggest you read the documentation for C<IO::File> too. =head1 CONSTRUCTOR =over 4 =item new () Creates a new C<IO::Handle> object. =item new_from_fd ( FD, MODE ) Creates an C<IO::Handle> like C<new> does. It requires two parameters, which are passed to the method C<fdopen>; if the fdopen fails, the object is destroyed. Otherwise, it is returned to the caller. =back =head1 METHODS See L<perlfunc> for complete descriptions of each of the following supported C<IO::Handle> methods, which are just front ends for the corresponding built-in functions: $io->close $io->eof $io->fcntl( FUNCTION, SCALAR ) $io->fileno $io->format_write( [FORMAT_NAME] ) $io->getc $io->ioctl( FUNCTION, SCALAR ) $io->read ( BUF, LEN, [OFFSET] ) $io->print ( ARGS ) $io->printf ( FMT, [ARGS] ) $io->say ( ARGS ) $io->stat $io->sysread ( BUF, LEN, [OFFSET] ) $io->syswrite ( BUF, [LEN, [OFFSET]] ) $io->truncate ( LEN ) See L<perlvar> for complete descriptions of each of the following supported C<IO::Handle> methods. All of them return the previous value of the attribute and takes an optional single argument that when given will set the value. If no argument is given the previous value is unchanged (except for $io->autoflush will actually turn ON autoflush by default). $io->autoflush ( [BOOL] ) $| $io->format_page_number( [NUM] ) $% $io->format_lines_per_page( [NUM] ) $= $io->format_lines_left( [NUM] ) $- $io->format_name( [STR] ) $~ $io->format_top_name( [STR] ) $^ $io->input_line_number( [NUM]) $. The following methods are not supported on a per-filehandle basis. IO::Handle->format_line_break_characters( [STR] ) $: IO::Handle->format_formfeed( [STR]) $^L IO::Handle->output_field_separator( [STR] ) $, IO::Handle->output_record_separator( [STR] ) $\ IO::Handle->input_record_separator( [STR] ) $/ Furthermore, for doing normal I/O you might need these: =over 4 =item $io->fdopen ( FD, MODE ) C<fdopen> is like an ordinary C<open> except that its first parameter is not a filename but rather a file handle name, an IO::Handle object, or a file descriptor number. (For the documentation of the C<open> method, see L<IO::File>.) =item $io->opened Returns true if the object is currently a valid file descriptor, false otherwise. =item $io->getline This works like <$io> described in L<perlop/"I/O Operators"> except that it's more readable and can be safely called in a list context but still returns just one line. If used as the conditional within a C<while> or C-style C<for> loop, however, you will need to emulate the functionality of <$io> with C<< defined($_ = $io->getline) >>. =item $io->getlines This works like <$io> when called in a list context to read all the remaining lines in a file, except that it's more readable. It will also croak() if accidentally called in a scalar context. =item $io->ungetc ( ORD ) Pushes a character with the given ordinal value back onto the given handle's input stream. Only one character of pushback per handle is guaranteed. =item $io->write ( BUF, LEN [, OFFSET ] ) This C<write> is somewhat like C<write> found in C, in that it is the opposite of read. The wrapper for the perl C<write> function is called C<format_write>. However, whilst the C C<write> function returns the number of bytes written, this C<write> function simply returns true if successful (like C<print>). A more C-like C<write> is C<syswrite> (see above). =item $io->error Returns a true value if the given handle has experienced any errors since it was opened or since the last call to C<clearerr>, or if the handle is invalid. It only returns false for a valid handle with no outstanding errors. =item $io->clearerr Clear the given handle's error indicator. Returns -1 if the handle is invalid, 0 otherwise. =item $io->sync C<sync> synchronizes a file's in-memory state with that on the physical medium. C<sync> does not operate at the perlio api level, but operates on the file descriptor (similar to sysread, sysseek and systell). This means that any data held at the perlio api level will not be synchronized. To synchronize data that is buffered at the perlio api level you must use the flush method. C<sync> is not implemented on all platforms. Returns "0 but true" on success, C<undef> on error, C<undef> for an invalid handle. See L<fsync(3c)>. =item $io->flush C<flush> causes perl to flush any buffered data at the perlio api level. Any unread data in the buffer will be discarded, and any unwritten data will be written to the underlying file descriptor. Returns "0 but true" on success, C<undef> on error. =item $io->printflush ( ARGS ) Turns on autoflush, print ARGS and then restores the autoflush status of the C<IO::Handle> object. Returns the return value from print. =item $io->blocking ( [ BOOL ] ) If called with an argument C<blocking> will turn on non-blocking IO if C<BOOL> is false, and turn it off if C<BOOL> is true. C<blocking> will return the value of the previous setting, or the current setting if C<BOOL> is not given. If an error occurs C<blocking> will return undef and C<$!> will be set. =back If the C functions setbuf() and/or setvbuf() are available, then C<IO::Handle::setbuf> and C<IO::Handle::setvbuf> set the buffering policy for an IO::Handle. The calling sequences for the Perl functions are the same as their C counterparts--including the constants C<_IOFBF>, C<_IOLBF>, and C<_IONBF> for setvbuf()--except that the buffer parameter specifies a scalar variable to use as a buffer. You should only change the buffer before any I/O, or immediately after calling flush. WARNING: The IO::Handle::setvbuf() is not available by default on Perls 5.8.0 and later because setvbuf() is rather specific to using the stdio library, while Perl prefers the new perlio subsystem instead. WARNING: A variable used as a buffer by C<setbuf> or C<setvbuf> B<must not be modified> in any way until the IO::Handle is closed or C<setbuf> or C<setvbuf> is called again, or memory corruption may result! Remember that the order of global destruction is undefined, so even if your buffer variable remains in scope until program termination, it may be undefined before the file IO::Handle is closed. Note that you need to import the constants C<_IOFBF>, C<_IOLBF>, and C<_IONBF> explicitly. Like C, setbuf returns nothing. setvbuf returns "0 but true", on success, C<undef> on failure. Lastly, there is a special method for working under B<-T> and setuid/gid scripts: =over 4 =item $io->untaint Marks the object as taint-clean, and as such data read from it will also be considered taint-clean. Note that this is a very trusting action to take, and appropriate consideration for the data source and potential vulnerability should be kept in mind. Returns 0 on success, -1 if setting the taint-clean flag failed. (eg invalid handle) =back =head1 NOTE An C<IO::Handle> object is a reference to a symbol/GLOB reference (see the C<Symbol> package). Some modules that inherit from C<IO::Handle> may want to keep object related variables in the hash table part of the GLOB. In an attempt to prevent modules trampling on each other I propose the that any such module should prefix its variables with its own name separated by _'s. For example the IO::Socket module keeps a C<timeout> variable in 'io_socket_timeout'. =head1 SEE ALSO L<perlfunc>, L<perlop/"I/O Operators">, L<IO::File> =head1 BUGS Due to backwards compatibility, all filehandles resemble objects of class C<IO::Handle>, or actually classes derived from that class. They actually aren't. Which means you can't derive your own class from C<IO::Handle> and inherit those methods. =head1 HISTORY Derived from FileHandle.pm by Graham Barr E<lt>F<gbarr@pobox.com>E<gt> =cut use 5.006_001; use strict; our($VERSION, @EXPORT_OK, @ISA); use Carp; use Symbol; use SelectSaver; use IO (); # Load the XS module require Exporter; @ISA = qw(Exporter); $VERSION = "1.36"; $VERSION = eval $VERSION; @EXPORT_OK = qw( autoflush output_field_separator output_record_separator input_record_separator input_line_number format_page_number format_lines_per_page format_lines_left format_name format_top_name format_line_break_characters format_formfeed format_write print printf say getline getlines printflush flush SEEK_SET SEEK_CUR SEEK_END _IOFBF _IOLBF _IONBF ); ################################################ ## Constructors, destructors. ## sub new { my $class = ref($_[0]) || $_[0] || "IO::Handle"; if (@_ != 1) { # Since perl will automatically require IO::File if needed, but # also initialises IO::File's @ISA as part of the core we must # ensure IO::File is loaded if IO::Handle is. This avoids effect- # ively "half-loading" IO::File. if ($] > 5.013 && $class eq 'IO::File' && !$INC{"IO/File.pm"}) { require IO::File; shift; return IO::File::->new(@_); } croak "usage: $class->new()"; } my $io = gensym; bless $io, $class; } sub new_from_fd { my $class = ref($_[0]) || $_[0] || "IO::Handle"; @_ == 3 or croak "usage: $class->new_from_fd(FD, MODE)"; my $io = gensym; shift; IO::Handle::fdopen($io, @_) or return undef; bless $io, $class; } # # There is no need for DESTROY to do anything, because when the # last reference to an IO object is gone, Perl automatically # closes its associated files (if any). However, to avoid any # attempts to autoload DESTROY, we here define it to do nothing. # sub DESTROY {} ################################################ ## Open and close. ## sub _open_mode_string { my ($mode) = @_; $mode =~ /^\+?(<|>>?)$/ or $mode =~ s/^r(\+?)$/$1</ or $mode =~ s/^w(\+?)$/$1>/ or $mode =~ s/^a(\+?)$/$1>>/ or croak "IO::Handle: bad open mode: $mode"; $mode; } sub fdopen { @_ == 3 or croak 'usage: $io->fdopen(FD, MODE)'; my ($io, $fd, $mode) = @_; local(*GLOB); if (ref($fd) && "".$fd =~ /GLOB\(/o) { # It's a glob reference; Alias it as we cannot get name of anon GLOBs my $n = qualify(*GLOB); *GLOB = *{*$fd}; $fd = $n; } elsif ($fd =~ m#^\d+$#) { # It's an FD number; prefix with "=". $fd = "=$fd"; } open($io, _open_mode_string($mode) . '&' . $fd) ? $io : undef; } sub close { @_ == 1 or croak 'usage: $io->close()'; my($io) = @_; close($io); } ################################################ ## Normal I/O functions. ## # flock # select sub opened { @_ == 1 or croak 'usage: $io->opened()'; defined fileno($_[0]); } sub fileno { @_ == 1 or croak 'usage: $io->fileno()'; fileno($_[0]); } sub getc { @_ == 1 or croak 'usage: $io->getc()'; getc($_[0]); } sub eof { @_ == 1 or croak 'usage: $io->eof()'; eof($_[0]); } sub print { @_ or croak 'usage: $io->print(ARGS)'; my $this = shift; print $this @_; } sub printf { @_ >= 2 or croak 'usage: $io->printf(FMT,[ARGS])'; my $this = shift; printf $this @_; } sub say { @_ or croak 'usage: $io->say(ARGS)'; my $this = shift; local $\ = "\n"; print $this @_; } # Special XS wrapper to make them inherit lexical hints from the caller. _create_getline_subs( <<'END' ) or die $@; sub getline { @_ == 1 or croak 'usage: $io->getline()'; my $this = shift; return scalar <$this>; } sub getlines { @_ == 1 or croak 'usage: $io->getlines()'; wantarray or croak 'Can\'t call $io->getlines in a scalar context, use $io->getline'; my $this = shift; return <$this>; } 1; # return true for error checking END *gets = \&getline; # deprecated sub truncate { @_ == 2 or croak 'usage: $io->truncate(LEN)'; truncate($_[0], $_[1]); } sub read { @_ == 3 || @_ == 4 or croak 'usage: $io->read(BUF, LEN [, OFFSET])'; read($_[0], $_[1], $_[2], $_[3] || 0); } sub sysread { @_ == 3 || @_ == 4 or croak 'usage: $io->sysread(BUF, LEN [, OFFSET])'; sysread($_[0], $_[1], $_[2], $_[3] || 0); } sub write { @_ >= 2 && @_ <= 4 or croak 'usage: $io->write(BUF [, LEN [, OFFSET]])'; local($\) = ""; $_[2] = length($_[1]) unless defined $_[2]; print { $_[0] } substr($_[1], $_[3] || 0, $_[2]); } sub syswrite { @_ >= 2 && @_ <= 4 or croak 'usage: $io->syswrite(BUF [, LEN [, OFFSET]])'; if (defined($_[2])) { syswrite($_[0], $_[1], $_[2], $_[3] || 0); } else { syswrite($_[0], $_[1]); } } sub stat { @_ == 1 or croak 'usage: $io->stat()'; stat($_[0]); } ################################################ ## State modification functions. ## sub autoflush { my $old = new SelectSaver qualify($_[0], caller); my $prev = $|; $| = @_ > 1 ? $_[1] : 1; $prev; } sub output_field_separator { carp "output_field_separator is not supported on a per-handle basis" if ref($_[0]); my $prev = $,; $, = $_[1] if @_ > 1; $prev; } sub output_record_separator { carp "output_record_separator is not supported on a per-handle basis" if ref($_[0]); my $prev = $\; $\ = $_[1] if @_ > 1; $prev; } sub input_record_separator { carp "input_record_separator is not supported on a per-handle basis" if ref($_[0]); my $prev = $/; $/ = $_[1] if @_ > 1; $prev; } sub input_line_number { local $.; () = tell qualify($_[0], caller) if ref($_[0]); my $prev = $.; $. = $_[1] if @_ > 1; $prev; } sub format_page_number { my $old; $old = new SelectSaver qualify($_[0], caller) if ref($_[0]); my $prev = $%; $% = $_[1] if @_ > 1; $prev; } sub format_lines_per_page { my $old; $old = new SelectSaver qualify($_[0], caller) if ref($_[0]); my $prev = $=; $= = $_[1] if @_ > 1; $prev; } sub format_lines_left { my $old; $old = new SelectSaver qualify($_[0], caller) if ref($_[0]); my $prev = $-; $- = $_[1] if @_ > 1; $prev; } sub format_name { my $old; $old = new SelectSaver qualify($_[0], caller) if ref($_[0]); my $prev = $~; $~ = qualify($_[1], caller) if @_ > 1; $prev; } sub format_top_name { my $old; $old = new SelectSaver qualify($_[0], caller) if ref($_[0]); my $prev = $^; $^ = qualify($_[1], caller) if @_ > 1; $prev; } sub format_line_break_characters { carp "format_line_break_characters is not supported on a per-handle basis" if ref($_[0]); my $prev = $:; $: = $_[1] if @_ > 1; $prev; } sub format_formfeed { carp "format_formfeed is not supported on a per-handle basis" if ref($_[0]); my $prev = $^L; $^L = $_[1] if @_ > 1; $prev; } sub formline { my $io = shift; my $picture = shift; local($^A) = $^A; local($\) = ""; formline($picture, @_); print $io $^A; } sub format_write { @_ < 3 || croak 'usage: $io->write( [FORMAT_NAME] )'; if (@_ == 2) { my ($io, $fmt) = @_; my $oldfmt = $io->format_name(qualify($fmt,caller)); CORE::write($io); $io->format_name($oldfmt); } else { CORE::write($_[0]); } } sub fcntl { @_ == 3 || croak 'usage: $io->fcntl( OP, VALUE );'; my ($io, $op) = @_; return fcntl($io, $op, $_[2]); } sub ioctl { @_ == 3 || croak 'usage: $io->ioctl( OP, VALUE );'; my ($io, $op) = @_; return ioctl($io, $op, $_[2]); } # this sub is for compatibility with older releases of IO that used # a sub called constant to determine if a constant existed -- GMB # # The SEEK_* and _IO?BF constants were the only constants at that time # any new code should just check defined(&CONSTANT_NAME) sub constant { no strict 'refs'; my $name = shift; (($name =~ /^(SEEK_(SET|CUR|END)|_IO[FLN]BF)$/) && defined &{$name}) ? &{$name}() : undef; } # so that flush.pl can be deprecated sub printflush { my $io = shift; my $old; $old = new SelectSaver qualify($io, caller) if ref($io); local $| = 1; if(ref($io)) { print $io @_; } else { print @_; } } 1; PK d�\�D���>