1*0Sstevel@tonic-gatepackage FileHandle; 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateuse 5.006; 4*0Sstevel@tonic-gateuse strict; 5*0Sstevel@tonic-gateour($VERSION, @ISA, @EXPORT, @EXPORT_OK); 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gate$VERSION = "2.01"; 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gaterequire IO::File; 10*0Sstevel@tonic-gate@ISA = qw(IO::File); 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate@EXPORT = qw(_IOFBF _IOLBF _IONBF); 13*0Sstevel@tonic-gate 14*0Sstevel@tonic-gate@EXPORT_OK = qw( 15*0Sstevel@tonic-gate pipe 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gate autoflush 18*0Sstevel@tonic-gate output_field_separator 19*0Sstevel@tonic-gate output_record_separator 20*0Sstevel@tonic-gate input_record_separator 21*0Sstevel@tonic-gate input_line_number 22*0Sstevel@tonic-gate format_page_number 23*0Sstevel@tonic-gate format_lines_per_page 24*0Sstevel@tonic-gate format_lines_left 25*0Sstevel@tonic-gate format_name 26*0Sstevel@tonic-gate format_top_name 27*0Sstevel@tonic-gate format_line_break_characters 28*0Sstevel@tonic-gate format_formfeed 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate print 31*0Sstevel@tonic-gate printf 32*0Sstevel@tonic-gate getline 33*0Sstevel@tonic-gate getlines 34*0Sstevel@tonic-gate); 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate# 37*0Sstevel@tonic-gate# Everything we're willing to export, we must first import. 38*0Sstevel@tonic-gate# 39*0Sstevel@tonic-gateimport IO::Handle grep { !defined(&$_) } @EXPORT, @EXPORT_OK; 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate# 42*0Sstevel@tonic-gate# Some people call "FileHandle::function", so all the functions 43*0Sstevel@tonic-gate# that were in the old FileHandle class must be imported, too. 44*0Sstevel@tonic-gate# 45*0Sstevel@tonic-gate{ 46*0Sstevel@tonic-gate no strict 'refs'; 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate my %import = ( 49*0Sstevel@tonic-gate 'IO::Handle' => 50*0Sstevel@tonic-gate [qw(DESTROY new_from_fd fdopen close fileno getc ungetc gets 51*0Sstevel@tonic-gate eof flush error clearerr setbuf setvbuf _open_mode_string)], 52*0Sstevel@tonic-gate 'IO::Seekable' => 53*0Sstevel@tonic-gate [qw(seek tell getpos setpos)], 54*0Sstevel@tonic-gate 'IO::File' => 55*0Sstevel@tonic-gate [qw(new new_tmpfile open)] 56*0Sstevel@tonic-gate ); 57*0Sstevel@tonic-gate for my $pkg (keys %import) { 58*0Sstevel@tonic-gate for my $func (@{$import{$pkg}}) { 59*0Sstevel@tonic-gate my $c = *{"${pkg}::$func"}{CODE} 60*0Sstevel@tonic-gate or die "${pkg}::$func missing"; 61*0Sstevel@tonic-gate *$func = $c; 62*0Sstevel@tonic-gate } 63*0Sstevel@tonic-gate } 64*0Sstevel@tonic-gate} 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate# 67*0Sstevel@tonic-gate# Specialized importer for Fcntl magic. 68*0Sstevel@tonic-gate# 69*0Sstevel@tonic-gatesub import { 70*0Sstevel@tonic-gate my $pkg = shift; 71*0Sstevel@tonic-gate my $callpkg = caller; 72*0Sstevel@tonic-gate require Exporter; 73*0Sstevel@tonic-gate Exporter::export($pkg, $callpkg, @_); 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate # 76*0Sstevel@tonic-gate # If the Fcntl extension is available, 77*0Sstevel@tonic-gate # export its constants. 78*0Sstevel@tonic-gate # 79*0Sstevel@tonic-gate eval { 80*0Sstevel@tonic-gate require Fcntl; 81*0Sstevel@tonic-gate Exporter::export('Fcntl', $callpkg); 82*0Sstevel@tonic-gate }; 83*0Sstevel@tonic-gate} 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate################################################ 86*0Sstevel@tonic-gate# This is the only exported function we define; 87*0Sstevel@tonic-gate# the rest come from other classes. 88*0Sstevel@tonic-gate# 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gatesub pipe { 91*0Sstevel@tonic-gate my $r = new IO::Handle; 92*0Sstevel@tonic-gate my $w = new IO::Handle; 93*0Sstevel@tonic-gate CORE::pipe($r, $w) or return undef; 94*0Sstevel@tonic-gate ($r, $w); 95*0Sstevel@tonic-gate} 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate# Rebless standard file handles 98*0Sstevel@tonic-gatebless *STDIN{IO}, "FileHandle" if ref *STDIN{IO} eq "IO::Handle"; 99*0Sstevel@tonic-gatebless *STDOUT{IO}, "FileHandle" if ref *STDOUT{IO} eq "IO::Handle"; 100*0Sstevel@tonic-gatebless *STDERR{IO}, "FileHandle" if ref *STDERR{IO} eq "IO::Handle"; 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate1; 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate__END__ 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate=head1 NAME 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gateFileHandle - supply object methods for filehandles 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate=head1 SYNOPSIS 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate use FileHandle; 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate $fh = new FileHandle; 115*0Sstevel@tonic-gate if ($fh->open("< file")) { 116*0Sstevel@tonic-gate print <$fh>; 117*0Sstevel@tonic-gate $fh->close; 118*0Sstevel@tonic-gate } 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate $fh = new FileHandle "> FOO"; 121*0Sstevel@tonic-gate if (defined $fh) { 122*0Sstevel@tonic-gate print $fh "bar\n"; 123*0Sstevel@tonic-gate $fh->close; 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate $fh = new FileHandle "file", "r"; 127*0Sstevel@tonic-gate if (defined $fh) { 128*0Sstevel@tonic-gate print <$fh>; 129*0Sstevel@tonic-gate undef $fh; # automatically closes the file 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate $fh = new FileHandle "file", O_WRONLY|O_APPEND; 133*0Sstevel@tonic-gate if (defined $fh) { 134*0Sstevel@tonic-gate print $fh "corge\n"; 135*0Sstevel@tonic-gate undef $fh; # automatically closes the file 136*0Sstevel@tonic-gate } 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate $pos = $fh->getpos; 139*0Sstevel@tonic-gate $fh->setpos($pos); 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate $fh->setvbuf($buffer_var, _IOLBF, 1024); 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate ($readfh, $writefh) = FileHandle::pipe; 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate autoflush STDOUT 1; 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate=head1 DESCRIPTION 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gateNOTE: This class is now a front-end to the IO::* classes. 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gateC<FileHandle::new> creates a C<FileHandle>, which is a reference to a 152*0Sstevel@tonic-gatenewly created symbol (see the C<Symbol> package). If it receives any 153*0Sstevel@tonic-gateparameters, they are passed to C<FileHandle::open>; if the open fails, 154*0Sstevel@tonic-gatethe C<FileHandle> object is destroyed. Otherwise, it is returned to 155*0Sstevel@tonic-gatethe caller. 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gateC<FileHandle::new_from_fd> creates a C<FileHandle> like C<new> does. 158*0Sstevel@tonic-gateIt requires two parameters, which are passed to C<FileHandle::fdopen>; 159*0Sstevel@tonic-gateif the fdopen fails, the C<FileHandle> object is destroyed. 160*0Sstevel@tonic-gateOtherwise, it is returned to the caller. 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gateC<FileHandle::open> accepts one parameter or two. With one parameter, 163*0Sstevel@tonic-gateit is just a front end for the built-in C<open> function. With two 164*0Sstevel@tonic-gateparameters, the first parameter is a filename that may include 165*0Sstevel@tonic-gatewhitespace or other special characters, and the second parameter is 166*0Sstevel@tonic-gatethe open mode, optionally followed by a file permission value. 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gateIf C<FileHandle::open> receives a Perl mode string (">", "+<", etc.) 169*0Sstevel@tonic-gateor a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic 170*0Sstevel@tonic-gatePerl C<open> operator. 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gateIf C<FileHandle::open> is given a numeric mode, it passes that mode 173*0Sstevel@tonic-gateand the optional permissions value to the Perl C<sysopen> operator. 174*0Sstevel@tonic-gateFor convenience, C<FileHandle::import> tries to import the O_XXX 175*0Sstevel@tonic-gateconstants from the Fcntl module. If dynamic loading is not available, 176*0Sstevel@tonic-gatethis may fail, but the rest of FileHandle will still work. 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gateC<FileHandle::fdopen> is like C<open> except that its first parameter 179*0Sstevel@tonic-gateis not a filename but rather a file handle name, a FileHandle object, 180*0Sstevel@tonic-gateor a file descriptor number. 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gateIf the C functions fgetpos() and fsetpos() are available, then 183*0Sstevel@tonic-gateC<FileHandle::getpos> returns an opaque value that represents the 184*0Sstevel@tonic-gatecurrent position of the FileHandle, and C<FileHandle::setpos> uses 185*0Sstevel@tonic-gatethat value to return to a previously visited position. 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gateIf the C function setvbuf() is available, then C<FileHandle::setvbuf> 188*0Sstevel@tonic-gatesets the buffering policy for the FileHandle. The calling sequence 189*0Sstevel@tonic-gatefor the Perl function is the same as its C counterpart, including the 190*0Sstevel@tonic-gatemacros C<_IOFBF>, C<_IOLBF>, and C<_IONBF>, except that the buffer 191*0Sstevel@tonic-gateparameter specifies a scalar variable to use as a buffer. WARNING: A 192*0Sstevel@tonic-gatevariable used as a buffer by C<FileHandle::setvbuf> must not be 193*0Sstevel@tonic-gatemodified in any way until the FileHandle is closed or until 194*0Sstevel@tonic-gateC<FileHandle::setvbuf> is called again, or memory corruption may 195*0Sstevel@tonic-gateresult! 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gateSee L<perlfunc> for complete descriptions of each of the following 198*0Sstevel@tonic-gatesupported C<FileHandle> methods, which are just front ends for the 199*0Sstevel@tonic-gatecorresponding built-in functions: 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate close 202*0Sstevel@tonic-gate fileno 203*0Sstevel@tonic-gate getc 204*0Sstevel@tonic-gate gets 205*0Sstevel@tonic-gate eof 206*0Sstevel@tonic-gate clearerr 207*0Sstevel@tonic-gate seek 208*0Sstevel@tonic-gate tell 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gateSee L<perlvar> for complete descriptions of each of the following 211*0Sstevel@tonic-gatesupported C<FileHandle> methods: 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate autoflush 214*0Sstevel@tonic-gate output_field_separator 215*0Sstevel@tonic-gate output_record_separator 216*0Sstevel@tonic-gate input_record_separator 217*0Sstevel@tonic-gate input_line_number 218*0Sstevel@tonic-gate format_page_number 219*0Sstevel@tonic-gate format_lines_per_page 220*0Sstevel@tonic-gate format_lines_left 221*0Sstevel@tonic-gate format_name 222*0Sstevel@tonic-gate format_top_name 223*0Sstevel@tonic-gate format_line_break_characters 224*0Sstevel@tonic-gate format_formfeed 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gateFurthermore, for doing normal I/O you might need these: 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate=over 4 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate=item $fh->print 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gateSee L<perlfunc/print>. 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate=item $fh->printf 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gateSee L<perlfunc/printf>. 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate=item $fh->getline 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gateThis works like <$fh> described in L<perlop/"I/O Operators"> 241*0Sstevel@tonic-gateexcept that it's more readable and can be safely called in a 242*0Sstevel@tonic-gatelist context but still returns just one line. 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate=item $fh->getlines 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gateThis works like <$fh> when called in a list context to 247*0Sstevel@tonic-gateread all the remaining lines in a file, except that it's more readable. 248*0Sstevel@tonic-gateIt will also croak() if accidentally called in a scalar context. 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate=back 251*0Sstevel@tonic-gate 252*0Sstevel@tonic-gateThere are many other functions available since FileHandle is descended 253*0Sstevel@tonic-gatefrom IO::File, IO::Seekable, and IO::Handle. Please see those 254*0Sstevel@tonic-gaterespective pages for documentation on more functions. 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate=head1 SEE ALSO 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gateThe B<IO> extension, 259*0Sstevel@tonic-gateL<perlfunc>, 260*0Sstevel@tonic-gateL<perlop/"I/O Operators">. 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate=cut 263