| 1 | # Copyright 2001 Free Software Foundation, Inc.
|
|---|
| 2 |
|
|---|
| 3 | # This program is free software; you can redistribute it and/or modify
|
|---|
| 4 | # it under the terms of the GNU General Public License as published by
|
|---|
| 5 | # the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 6 | # any later version.
|
|---|
| 7 |
|
|---|
| 8 | # This program is distributed in the hope that it will be useful,
|
|---|
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 11 | # GNU General Public License for more details.
|
|---|
| 12 |
|
|---|
| 13 | # You should have received a copy of the GNU General Public License
|
|---|
| 14 | # along with this program; if not, write to the Free Software
|
|---|
| 15 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|---|
| 16 | # 02111-1307, USA.
|
|---|
| 17 |
|
|---|
| 18 | # Written by Akim Demaille <[email protected]>.
|
|---|
| 19 |
|
|---|
| 20 | ###############################################################
|
|---|
| 21 | # The main copy of this file is in Autoconf's CVS repository. #
|
|---|
| 22 | # Updates should be sent to [email protected]. #
|
|---|
| 23 | ###############################################################
|
|---|
| 24 |
|
|---|
| 25 | package Automake::XFile;
|
|---|
| 26 |
|
|---|
| 27 | =head1 NAME
|
|---|
| 28 |
|
|---|
| 29 | Automake::XFile - supply object methods for filehandles with error handling
|
|---|
| 30 |
|
|---|
| 31 | =head1 SYNOPSIS
|
|---|
| 32 |
|
|---|
| 33 | use Automake::XFile;
|
|---|
| 34 |
|
|---|
| 35 | $fh = new Automake::XFile;
|
|---|
| 36 | $fh->open("< file"))
|
|---|
| 37 | # No need to check $FH: we died if open failed.
|
|---|
| 38 | print <$fh>;
|
|---|
| 39 | $fh->close;
|
|---|
| 40 | # No need to check the return value of close: we died if it failed.
|
|---|
| 41 |
|
|---|
| 42 | $fh = new Automake::XFile "> file";
|
|---|
| 43 | # No need to check $FH: we died if new failed.
|
|---|
| 44 | print $fh "bar\n";
|
|---|
| 45 | $fh->close;
|
|---|
| 46 |
|
|---|
| 47 | $fh = new Automake::XFile "file", "r";
|
|---|
| 48 | # No need to check $FH: we died if new failed.
|
|---|
| 49 | defined $fh
|
|---|
| 50 | print <$fh>;
|
|---|
| 51 | undef $fh; # automatically closes the file and checks for errors.
|
|---|
| 52 |
|
|---|
| 53 | $fh = new Automake::XFile "file", O_WRONLY|O_APPEND;
|
|---|
| 54 | # No need to check $FH: we died if new failed.
|
|---|
| 55 | print $fh "corge\n";
|
|---|
| 56 |
|
|---|
| 57 | $pos = $fh->getpos;
|
|---|
| 58 | $fh->setpos($pos);
|
|---|
| 59 |
|
|---|
| 60 | undef $fh; # automatically closes the file and checks for errors.
|
|---|
| 61 |
|
|---|
| 62 | autoflush STDOUT 1;
|
|---|
| 63 |
|
|---|
| 64 | =head1 DESCRIPTION
|
|---|
| 65 |
|
|---|
| 66 | C<Automake::XFile> inherits from C<IO::File>. It provides dying
|
|---|
| 67 | version of the methods C<open>, C<new>, and C<close>. It also overrides
|
|---|
| 68 | the C<getline> and C<getlines> methods to translate C<\r\n> to C<\n>.
|
|---|
| 69 |
|
|---|
| 70 | =head1 SEE ALSO
|
|---|
| 71 |
|
|---|
| 72 | L<perlfunc>,
|
|---|
| 73 | L<perlop/"I/O Operators">,
|
|---|
| 74 | L<IO::File>
|
|---|
| 75 | L<IO::Handle>
|
|---|
| 76 | L<IO::Seekable>
|
|---|
| 77 |
|
|---|
| 78 | =head1 HISTORY
|
|---|
| 79 |
|
|---|
| 80 | Derived from IO::File.pm by Akim Demaille E<lt>F<[email protected]>E<gt>.
|
|---|
| 81 |
|
|---|
| 82 | =cut
|
|---|
| 83 |
|
|---|
| 84 | require 5.000;
|
|---|
| 85 | use strict;
|
|---|
| 86 | use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD @ISA);
|
|---|
| 87 | use Carp;
|
|---|
| 88 | use File::Basename;
|
|---|
| 89 |
|
|---|
| 90 | require Exporter;
|
|---|
| 91 | require DynaLoader;
|
|---|
| 92 |
|
|---|
| 93 | @ISA = qw(IO::File Exporter DynaLoader);
|
|---|
| 94 |
|
|---|
| 95 | $VERSION = "1.1";
|
|---|
| 96 |
|
|---|
| 97 | @EXPORT = @IO::File::EXPORT;
|
|---|
| 98 |
|
|---|
| 99 | eval {
|
|---|
| 100 | # Make all Fcntl O_XXX constants available for importing
|
|---|
| 101 | require Fcntl;
|
|---|
| 102 | my @O = grep /^O_/, @Fcntl::EXPORT;
|
|---|
| 103 | Fcntl->import(@O); # first we import what we want to export
|
|---|
| 104 | push(@EXPORT, @O);
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | ################################################
|
|---|
|
|---|