<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#

package IO::File;

=head1 NAME

IO::File - supply object methods for filehandles

=head1 SYNOPSIS

    use IO::File;

    my $fh = IO::File-&gt;new();
    if ($fh-&gt;open("&lt; file")) {
        print &lt;$fh&gt;;
        $fh-&gt;close;
    }

    my $fh = IO::File-&gt;new("&gt; file");
    if (defined $fh) {
        print $fh "bar\n";
        $fh-&gt;close;
    }

    my $fh = IO::File-&gt;new("file", "r");
    if (defined $fh) {
        print &lt;$fh&gt;;
        undef $fh;       # automatically closes the file
    }

    my $fh = IO::File-&gt;new("file", O_WRONLY|O_APPEND);
    if (defined $fh) {
        print $fh "corge\n";

        my $pos = $fh-&gt;getpos;
        $fh-&gt;setpos($pos);

        undef $fh;       # automatically closes the file
    }

    autoflush STDOUT 1;

=head1 DESCRIPTION

C&lt;IO::File&gt; inherits from C&lt;IO::Handle&gt; and C&lt;IO::Seekable&gt;. It extends
these classes with methods that are specific to file handles.

=head1 CONSTRUCTOR

=over 4

=item new ( FILENAME [,MODE [,PERMS]] )

Creates an C&lt;IO::File&gt;.  If it receives any parameters, they are passed to
the method C&lt;open&gt;; if the open fails, the object is destroyed.  Otherwise,
it is returned to the caller.

=item new_tmpfile

Creates an C&lt;IO::File&gt; 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&lt;IO::File&gt; 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&lt;open&gt; accepts one, two or three parameters.  With one parameter,
it is just a front end for the built-in C&lt;open&gt; 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&lt;IO::File::open&gt; receives a Perl mode string ("E&lt;gt&gt;", "+E&lt;lt&gt;", etc.)
or an ANSI C fopen() mode string ("w", "r+", etc.), it uses the basic
Perl C&lt;open&gt; operator (but protects any special characters).

If C&lt;IO::File::open&gt; is given a numeric mode, it passes that mode
and the optional permissions value to the Perl C&lt;sysopen&gt; operator.
The permissions default to 0666.

If C&lt;IO::File::open&gt; is given a mode that includes the C&lt;:&gt; character,
it passes all the three arguments to the three-argument C&lt;open&gt; operator.

For convenience, C&lt;IO::File&gt; exports the O_XXX constants from the
Fcntl module, if this module is available.

=item binmode( [LAYER] )

C&lt;binmode&gt; sets C&lt;binmode&gt; on the underlying C&lt;IO&gt; object, as documented
in C&lt;perldoc -f binmode&gt;.

C&lt;binmode&gt; accepts one optional parameter, which is the layer to be
passed on to the C&lt;binmode&gt; call.

=back

=head1 NOTE

Some operating systems may perform  C&lt;IO::File::new()&gt; or C&lt;IO::File::open()&gt;
on a directory without errors.  This behavior is not portable and not
suggested for use.  Using C&lt;opendir()&gt; and C&lt;readdir()&gt; or C&lt;IO::Dir&gt; are
suggested instead.

=head1 SEE ALSO

L&lt;perlfunc&gt;, 
L&lt;perlop/"I/O Operators"&gt;,
L&lt;IO::Handle&gt;,
L&lt;IO::Seekable&gt;,
L&lt;IO::Dir&gt;

=head1 HISTORY

Derived from FileHandle.pm by Graham Barr E&lt;lt&gt;F&lt;gbarr@pobox.com&gt;E&lt;gt&gt;.

=cut

use 5.008_001;
use strict;
use Carp;
use Symbol;
use SelectSaver;
use IO::Seekable;

require Exporter;

our @ISA = qw(IO::Handle IO::Seekable Exporter);

our $VERSION = "1.52";

our @EXPORT = @IO::Seekable::EXPORT;

eval {
    # Make all Fcntl O_XXX constants available for importing
    require Fcntl;
    my @O = grep /^O_/, @Fcntl::EXPORT;
    Fcntl-&gt;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";
    @_ &gt;= 0 &amp;&amp; @_ &lt;= 3
	or croak "usage: $class-&gt;new([FILENAME [,MODE [,PERMS]]])";
    my $fh = $class-&gt;SUPER::new();
    if (@_) {
	$fh-&gt;open(@_)
	    or return undef;
    }
    $fh;
}

################################################
## Open
##

sub open {
    @_ &gt;= 2 &amp;&amp; @_ &lt;= 4 or croak 'usage: $fh-&gt;open(FILENAME [,MODE [,PERMS]])';
    my ($fh, $file) = @_;
    if (@_ &gt; 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-&gt;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-&gt;binmode([LAYER])';

    my($fh, $layer) = @_;

    return binmode $$fh unless $layer;
    return binmode $$fh, $layer;
}

1;
</pre></body></html>