404

[ Avaa Bypassed ]




Upload:

Command:

elspacio@3.145.11.142: ~ $
=encoding utf8

=head1 NAME

perlopentut - simple recipes for opening files and pipes in Perl

=head1 DESCRIPTION

Whenever you do I/O on a file in Perl, you do so through what in Perl is
called a B<filehandle>.  A filehandle is an internal name for an external
file.  It is the job of the C<open> function to make the association
between the internal name and the external name, and it is the job
of the C<close> function to break that association.

For your convenience, Perl sets up a few special filehandles that are
already open when you run.  These include C<STDIN>, C<STDOUT>, C<STDERR>,
and C<ARGV>.  Since those are pre-opened, you can use them right away
without having to go to the trouble of opening them yourself:

    print STDERR "This is a debugging message.\n";

    print STDOUT "Please enter something: ";
    $response = <STDIN> // die "how come no input?";
    print STDOUT "Thank you!\n";

    while (<ARGV>) { ... }

As you see from those examples, C<STDOUT> and C<STDERR> are output
handles, and C<STDIN> and C<ARGV> are input handles.  They are
in all capital letters because they are reserved to Perl, much
like the C<@ARGV> array and the C<%ENV> hash are.  Their external
associations were set up by your shell.

You will need to open every other filehandle on your own. Although there
are many variants, the most common way to call Perl's open() function
is with three arguments and one return value:

C<    I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)>

Where:

=over

=item I<OK>

will be some defined value if the open succeeds, but
C<undef> if it fails;

=item I<HANDLE>

should be an undefined scalar variable to be filled in by the
C<open> function if it succeeds;

=item I<MODE>

is the access mode and the encoding format to open the file with;

=item I<PATHNAME>

is the external name of the file you want opened.

=back

Most of the complexity of the C<open> function lies in the many
possible values that the I<MODE> parameter can take on.

One last thing before we show you how to open files: opening
files does not (usually) automatically lock them in Perl.  See
L<perlfaq5> for how to lock.

=head1 Opening Text Files

=head2 Opening Text Files for Reading

If you want to read from a text file, first open it in
read-only mode like this:

    my $filename = "/some/path/to/a/textfile/goes/here";
    my $encoding = ":encoding(UTF-8)";
    my $handle   = undef;     # this will be filled in on success

    open($handle, "< $encoding", $filename)
        || die "$0: can't open $filename for reading: $!";

As with the shell, in Perl the C<< "<" >> is used to open the file in
read-only mode.  If it succeeds, Perl allocates a brand new filehandle for
you and fills in your previously undefined C<$handle> argument with a
reference to that handle.

Now you may use functions like C<readline>, C<read>, C<getc>, and
C<sysread> on that handle.  Probably the most common input function
is the one that looks like an operator:

    $line = readline($handle);
    $line = <$handle>;          # same thing

Because the C<readline> function returns C<undef> at end of file or
upon error, you will sometimes see it used this way:

    $line = <$handle>;
    if (defined $line) {
        # do something with $line
    }
    else {
        # $line is not valid, so skip it
    }

You can also just quickly C<die> on an undefined value this way:

    $line = <$handle> // die "no input found";

However, if hitting EOF is an expected and normal event, you do not want to
exit simply because you have run out of input.  Instead, you probably just want
to exit an input loop.  You can then test to see if an actual error has caused
the loop to terminate, and act accordingly:

    while (<$handle>) {
        # do something with data in $_
    }
    if ($!) {
        die "unexpected error while reading from $filename: $!";
    }

B<A Note on Encodings>: Having to specify the text encoding every time
might seem a bit of a bother.  To set up a default encoding for C<open> so
that you don't have to supply it each time, you can use the C<open> pragma:

    use open qw< :encoding(UTF-8) >;

Once you've done that, you can safely omit the encoding part of the
open mode:

    open($handle, "<", $filename)
        || die "$0: can't open $filename for reading: $!";

But never use the bare C<< "<" >> without having set up a default encoding
first.  Otherwise, Perl cannot know which of the many, many, many possible
flavors of text file you have, and Perl will have no idea how to correctly
map the data in your file into actual characters it can work with.  Other
common encoding formats including C<"ASCII">, C<"ISO-8859-1">,
C<"ISO-8859-15">, C<"Windows-1252">, C<"MacRoman">, and even C<"UTF-16LE">.
See L<perlunitut> for more about encodings.

=head2 Opening Text Files for Writing

When you want to write to a file, you first have to decide what to do about
any existing contents of that file.  You have two basic choices here: to
preserve or to clobber.

If you want to preserve any existing contents, then you want to open the file
in append mode.  As in the shell, in Perl you use C<<< ">>" >>> to open an
existing file in append mode.  C<<< ">>" >>> creates the file if it does not
already exist.

    my $handle   = undef;
    my $filename = "/some/path/to/a/textfile/goes/here";
    my $encoding = ":encoding(UTF-8)";

    open($handle, ">> $encoding", $filename)
        || die "$0: can't open $filename for appending: $!";

Now you can write to that filehandle using any of C<print>, C<printf>,
C<say>, C<write>, or C<syswrite>.

As noted above, if the file does not already exist, then the append-mode open
will create it for you.  But if the file does already exist, its contents are
safe from harm because you will be adding your new text past the end of the
old text.

On the other hand, sometimes you want to clobber whatever might already be
there.  To empty out a file before you start writing to it, you can open it
in write-only mode:

    my $handle   = undef;
    my $filename = "/some/path/to/a/textfile/goes/here";
    my $encoding = ":encoding(UTF-8)";

    open($handle, "> $encoding", $filename)
        || die "$0: can't open $filename in write-open mode: $!";

Here again Perl works just like the shell in that the C<< ">" >> clobbers
an existing file.

As with the append mode, when you open a file in write-only mode,
you can now write to that filehandle using any of C<print>, C<printf>,
C<say>, C<write>, or C<syswrite>.

What about read-write mode?  You should probably pretend it doesn't exist,
because opening text files in read-write mode is unlikely to do what you
would like.  See L<perlfaq5> for details.

=head1 Opening Binary Files

If the file to be opened contains binary data instead of text characters,
then the C<MODE> argument to C<open> is a little different.  Instead of
specifying the encoding, you tell Perl that your data are in raw bytes.

    my $filename = "/some/path/to/a/binary/file/goes/here";
    my $encoding = ":raw :bytes"
    my $handle   = undef;     # this will be filled in on success

And then open as before, choosing C<<< "<" >>>, C<<< ">>" >>>, or
C<<< ">" >>> as needed:

    open($handle, "< $encoding", $filename)
        || die "$0: can't open $filename for reading: $!";

    open($handle, ">> $encoding", $filename)
        || die "$0: can't open $filename for appending: $!";

    open($handle, "> $encoding", $filename)
        || die "$0: can't open $filename in write-open mode: $!";

Alternately, you can change to binary mode on an existing handle this way:

    binmode($handle)    || die "cannot binmode handle";

This is especially handy for the handles that Perl has already opened for you.

    binmode(STDIN)      || die "cannot binmode STDIN";
    binmode(STDOUT)     || die "cannot binmode STDOUT";

You can also pass C<binmode> an explicit encoding to change it on the fly.
This isn't exactly "binary" mode, but we still use C<binmode> to do it:

  binmode(STDIN,  ":encoding(MacRoman)") || die "cannot binmode STDIN";
  binmode(STDOUT, ":encoding(UTF-8)")    || die "cannot binmode STDOUT";

Once you have your binary file properly opened in the right mode, you can
use all the same Perl I/O functions as you used on text files.  However,
you may wish to use the fixed-size C<read> instead of the variable-sized
C<readline> for your input.

Here's an example of how to copy a binary file:

    my $BUFSIZ   = 64 * (2 ** 10);
    my $name_in  = "/some/input/file";
    my $name_out = "/some/output/flie";

    my($in_fh, $out_fh, $buffer);

    open($in_fh,  "<", $name_in)
        || die "$0: cannot open $name_in for reading: $!";
    open($out_fh, ">", $name_out)
        || die "$0: cannot open $name_out for writing: $!";

    for my $fh ($in_fh, $out_fh)  {
        binmode($fh)               || die "binmode failed";
    }

    while (read($in_fh, $buffer, $BUFSIZ)) {
        unless (print $out_fh $buffer) {
            die "couldn't write to $name_out: $!";
        }
    }

    close($in_fh)       || die "couldn't close $name_in: $!";
    close($out_fh)      || die "couldn't close $name_out: $!";

=head1 Opening Pipes

To be announced.

=head1 Low-level File Opens via sysopen

To be announced.  Or deleted.

=head1 SEE ALSO

To be announced.

=head1 AUTHOR and COPYRIGHT

Copyright 2013 Tom Christiansen.

This documentation is free; you can redistribute it and/or modify it under
the same terms as Perl itself.


Filemanager

Name Type Size Permission Actions
perl.pod File 15.89 KB 0644
perl5004delta.pod File 54.92 KB 0644
perl5005delta.pod File 33.48 KB 0644
perl5100delta.pod File 54.23 KB 0644
perl5101delta.pod File 42.86 KB 0644
perl5120delta.pod File 87.18 KB 0644
perl5121delta.pod File 9.9 KB 0644
perl5122delta.pod File 9.38 KB 0644
perl5123delta.pod File 4 KB 0644
perl5124delta.pod File 3.59 KB 0644
perl5125delta.pod File 7.5 KB 0644
perl5140delta.pod File 140.94 KB 0644
perl5141delta.pod File 7.78 KB 0644
perl5142delta.pod File 6.73 KB 0644
perl5143delta.pod File 7.58 KB 0644
perl5144delta.pod File 6.18 KB 0644
perl5160delta.pod File 130.52 KB 0644
perl5161delta.pod File 6 KB 0644
perl5162delta.pod File 3.51 KB 0644
perl5163delta.pod File 3.99 KB 0644
perl5180delta.pod File 116.63 KB 0644
perl5181delta.pod File 6.44 KB 0644
perl5182delta.pod File 5.21 KB 0644
perl5184delta.pod File 4.53 KB 0644
perl5200delta.pod File 112.99 KB 0644
perl5201delta.pod File 10.64 KB 0644
perl5202delta.pod File 12.22 KB 0644
perl5203delta.pod File 9.17 KB 0644
perl5220delta.pod File 127.89 KB 0644
perl5221delta.pod File 10.51 KB 0644
perl5222delta.pod File 12.33 KB 0644
perl5223delta.pod File 8.26 KB 0644
perl5224delta.pod File 4.36 KB 0644
perl5240delta.pod File 63.41 KB 0644
perl5241delta.pod File 8.02 KB 0644
perl5242delta.pod File 4.02 KB 0644
perl5243delta.pod File 11.16 KB 0644
perl5244delta.pod File 4.4 KB 0644
perl5260delta.pod File 99.45 KB 0644
perl5261delta.pod File 7.74 KB 0644
perl5262delta.pod File 7.7 KB 0644
perl5263delta.pod File 6.9 KB 0644
perl5280delta.pod File 70.42 KB 0644
perl561delta.pod File 121.79 KB 0644
perl56delta.pod File 104.69 KB 0644
perl581delta.pod File 37.17 KB 0644
perl582delta.pod File 4.37 KB 0644
perl583delta.pod File 6.19 KB 0644
perl584delta.pod File 7.19 KB 0644
perl585delta.pod File 5.75 KB 0644
perl586delta.pod File 4.54 KB 0644
perl587delta.pod File 8.16 KB 0644
perl588delta.pod File 24.68 KB 0644
perl589delta.pod File 52.64 KB 0644
perl58delta.pod File 112.47 KB 0644
perlaix.pod File 19.96 KB 0644
perlamiga.pod File 5.61 KB 0644
perlandroid.pod File 7.69 KB 0644
perlapi.pod File 433.14 KB 0644
perlapio.pod File 18.83 KB 0644
perlartistic.pod File 6.85 KB 0644
perlbook.pod File 8.14 KB 0644
perlboot.pod File 294 B 0644
perlbot.pod File 304 B 0644
perlbs2000.pod File 7.87 KB 0644
perlcall.pod File 55.38 KB 0644
perlce.pod File 14.26 KB 0644
perlcheat.pod File 4.38 KB 0644
perlclib.pod File 9.39 KB 0644
perlcn.pod File 4.58 KB 0644
perlcommunity.pod File 7.05 KB 0644
perlcygwin.pod File 26.56 KB 0644
perldata.pod File 45.65 KB 0644
perldbmfilter.pod File 4.86 KB 0644
perldebguts.pod File 37.63 KB 0644
perldebtut.pod File 21.63 KB 0644
perldebug.pod File 38.34 KB 0644
perldelta.pod File 6.9 KB 0644
perldeprecation.pod File 17.74 KB 0644
perldiag.pod File 277.9 KB 0644
perldos.pod File 10.28 KB 0644
perldsc.pod File 25.01 KB 0644
perldtrace.pod File 7.77 KB 0644
perlebcdic.pod File 82.26 KB 0644
perlembed.pod File 36.32 KB 0644
perlexperiment.pod File 7.03 KB 0644
perlfork.pod File 13.04 KB 0644
perlform.pod File 16.22 KB 0644
perlfreebsd.pod File 1.57 KB 0644
perlfunc.pod File 383.75 KB 0644
perlgit.pod File 32.72 KB 0644
perlgpl.pod File 13.49 KB 0644
perlguts.pod File 136.06 KB 0644
perlhack.pod File 39.5 KB 0644
perlhacktips.pod File 54.21 KB 0644
perlhacktut.pod File 6.01 KB 0644
perlhaiku.pod File 1.47 KB 0644
perlhist.pod File 52.29 KB 0644
perlhpux.pod File 29.79 KB 0644
perlhurd.pod File 1.95 KB 0644
perlintern.pod File 53.29 KB 0644
perlinterp.pod File 32.9 KB 0644
perlintro.pod File 21.6 KB 0644
perliol.pod File 33.38 KB 0644
perlipc.pod File 69.17 KB 0644
perlirix.pod File 4.29 KB 0644
perljp.pod File 7.34 KB 0644
perlko.pod File 11.97 KB 0644
perllexwarn.pod File 355 B 0644
perllinux.pod File 1.45 KB 0644
perllocale.pod File 67.07 KB 0644
perllol.pod File 9.36 KB 0644
perlmacos.pod File 1001 B 0644
perlmacosx.pod File 11.78 KB 0644
perlmod.pod File 25.63 KB 0644
perlmodinstall.pod File 12.49 KB 0644
perlmodlib.pod File 74.69 KB 0644
perlmodstyle.pod File 22.05 KB 0644
perlmroapi.pod File 3.14 KB 0644
perlnetware.pod File 6.49 KB 0644
perlnewmod.pod File 10.78 KB 0644
perlnumber.pod File 8.16 KB 0644
perlobj.pod File 34.7 KB 0644
perlootut.pod File 26.16 KB 0644
perlop.pod File 133.06 KB 0644
perlopenbsd.pod File 1.18 KB 0644
perlopentut.pod File 9.23 KB 0644
perlos2.pod File 91.16 KB 0644
perlos390.pod File 15.31 KB 0644
perlos400.pod File 4.66 KB 0644
perlpacktut.pod File 50.08 KB 0644
perlperf.pod File 48.71 KB 0644
perlplan9.pod File 5 KB 0644
perlpod.pod File 21.68 KB 0644
perlpodspec.pod File 66.87 KB 0644
perlpolicy.pod File 25.03 KB 0644
perlport.pod File 85.55 KB 0644
perlpragma.pod File 5.05 KB 0644
perlqnx.pod File 6.52 KB 0644
perlre.pod File 118.07 KB 0644
perlreapi.pod File 29.62 KB 0644
perlrebackslash.pod File 31.07 KB 0644
perlrecharclass.pod File 47.88 KB 0644
perlref.pod File 34.48 KB 0644
perlreftut.pod File 18.35 KB 0644
perlreguts.pod File 37.43 KB 0644
perlrepository.pod File 509 B 0644
perlrequick.pod File 18.06 KB 0644
perlreref.pod File 14.4 KB 0644
perlretut.pod File 118.42 KB 0644
perlriscos.pod File 1.49 KB 0644
perlrun.pod File 52.29 KB 0644
perlsec.pod File 25.57 KB 0644
perlsolaris.pod File 29.12 KB 0644
perlsource.pod File 6.71 KB 0644
perlstyle.pod File 8.43 KB 0644
perlsub.pod File 71.26 KB 0644
perlsymbian.pod File 15 KB 0644
perlsyn.pod File 43.47 KB 0644
perlsynology.pod File 7.6 KB 0644
perlthrtut.pod File 45.37 KB 0644
perltie.pod File 37.7 KB 0644
perltoc.pod File 677.89 KB 0644
perltodo.pod File 376 B 0644
perltooc.pod File 294 B 0644
perltoot.pod File 294 B 0644
perltrap.pod File 10.37 KB 0644
perltru64.pod File 8.29 KB 0644
perltw.pod File 4.37 KB 0644
perlunicode.pod File 80.56 KB 0644
perlunicook.pod File 24.89 KB 0644
perlunifaq.pod File 13.33 KB 0644
perluniintro.pod File 37.44 KB 0644
perluniprops.pod File 278.62 KB 0644
perlunitut.pod File 7.76 KB 0644
perlutil.pod File 7.46 KB 0644
perlvar.pod File 76.53 KB 0644
perlvms.pod File 49.63 KB 0644
perlvos.pod File 3.75 KB 0644
perlwin32.pod File 38.38 KB 0644
perlxs.pod File 77.07 KB 0644
perlxstut.pod File 48.92 KB 0644
perlxstypemap.pod File 23.44 KB 0644