/usr/share/perl5/pod
NameSizeModeActions
perl.pod162700644editdlrm
perl56delta.pod1072010644editdlrm
perl58delta.pod1151650644editdlrm
perl561delta.pod1247130644editdlrm
perl581delta.pod380610644editdlrm
perl582delta.pod44700644editdlrm
perl583delta.pod63350644editdlrm
perl584delta.pod73630644editdlrm
perl585delta.pod58890644editdlrm
perl586delta.pod46510644editdlrm
perl587delta.pod83570644editdlrm
perl588delta.pod252720644editdlrm
perl589delta.pod539000644editdlrm
perl5004delta.pod562400644editdlrm
perl5005delta.pod342830644editdlrm
perl5100delta.pod555350644editdlrm
perl5101delta.pod438880644editdlrm
perl5120delta.pod892720644editdlrm
perl5121delta.pod101410644editdlrm
perl5122delta.pod96030644editdlrm
perl5123delta.pod41000644editdlrm
perl5124delta.pod36720644editdlrm
perl5125delta.pod76830644editdlrm
perl5140delta.pod1443240644editdlrm
perl5141delta.pod79660644editdlrm
perl5142delta.pod68920644editdlrm
perl5143delta.pod77600644editdlrm
perl5144delta.pod63270644editdlrm
perl5160delta.pod1336510644editdlrm
perl5161delta.pod61420644editdlrm
perl5162delta.pod35940644editdlrm
perl5163delta.pod40850644editdlrm
perl5180delta.pod1194310644editdlrm
perl5181delta.pod65950644editdlrm
perl5182delta.pod53350644editdlrm
perl5184delta.pod46420644editdlrm
perl5200delta.pod1156990644editdlrm
perl5201delta.pod108990644editdlrm
perl5202delta.pod125090644editdlrm
perl5203delta.pod93920644editdlrm
perl5220delta.pod1309630644editdlrm
perl5221delta.pod107670644editdlrm
perl5222delta.pod126290644editdlrm
perl5223delta.pod84560644editdlrm
perl5224delta.pod44600644editdlrm
perl5240delta.pod649270644editdlrm
perl5241delta.pod82150644editdlrm
perl5242delta.pod41130644editdlrm
perl5243delta.pod114280644editdlrm
perl5244delta.pod45100644editdlrm
perl5260delta.pod1018360644editdlrm
perl5261delta.pod79270644editdlrm
perl5262delta.pod78800644editdlrm
perl5263delta.pod70630644editdlrm
perl5280delta.pod721130644editdlrm
perlaix.pod204370644editdlrm
perlamiga.pod57490644editdlrm
perlandroid.pod78710644editdlrm
perlapi.pod4435350644editdlrm
perlapio.pod192850644editdlrm
perlartistic.pod70100644editdlrm
perlbook.pod83380644editdlrm
perlboot.pod2940644editdlrm
perlbot.pod3040644editdlrm
perlbs2000.pod80580644editdlrm
perlcall.pod567060644editdlrm
perlce.pod146020644editdlrm
perlcheat.pod44810644editdlrm
perlclib.pod96190644editdlrm
perlcn.pod46910644editdlrm
perlcommunity.pod72170644editdlrm
perlcygwin.pod271990644editdlrm
perldata.pod467430644editdlrm
perldbmfilter.pod49810644editdlrm
perldebguts.pod385350644editdlrm
perldebtut.pod221520644editdlrm
perldebug.pod392580644editdlrm
perldelta.pod70630644editdlrm
perldeprecation.pod181690644editdlrm
perldiag.pod2845720644editdlrm
perldos.pod105220644editdlrm
perldsc.pod256140644editdlrm
perldtrace.pod79580644editdlrm
perlebcdic.pod842330644editdlrm
perlembed.pod371960644editdlrm
perlexperiment.pod71950644editdlrm
perlfork.pod133550644editdlrm
perlform.pod166080644editdlrm
perlfreebsd.pod16100644editdlrm
perlfunc.pod3929570644editdlrm
perlgit.pod335090644editdlrm
perlgpl.pod138150644editdlrm
perlguts.pod1393290644editdlrm
perlhack.pod404450644editdlrm
perlhacktips.pod555090644editdlrm
perlhacktut.pod61530644editdlrm
perlhaiku.pod15080644editdlrm
perlhist.pod535460644editdlrm
perlhpux.pod305090644editdlrm
perlhurd.pod19930644editdlrm
perlintern.pod545720644editdlrm
perlinterp.pod336870644editdlrm
perlintro.pod221190644editdlrm
perliol.pod341850644editdlrm
perlipc.pod708290644editdlrm
perlirix.pod43950644editdlrm
perljp.pod75210644editdlrm
perlko.pod122590644editdlrm
perllexwarn.pod3550644editdlrm
perllinux.pod14880644editdlrm
perllocale.pod686780644editdlrm
perllol.pod95800644editdlrm
perlmacos.pod10010644editdlrm
perlmacosx.pod120600644editdlrm
perlmod.pod262500644editdlrm
perlmodinstall.pod127920644editdlrm
perlmodlib.pod764820644editdlrm
perlmodstyle.pod225750644editdlrm
perlmroapi.pod32120644editdlrm
perlnetware.pod66480644editdlrm
perlnewmod.pod110360644editdlrm
perlnumber.pod83530644editdlrm
perlobj.pod355370644editdlrm
perlootut.pod267830644editdlrm
perlop.pod1362520644editdlrm
perlopenbsd.pod12040644editdlrm
perlopentut.pod94550644editdlrm
perlos2.pod933510644editdlrm
perlos390.pod156740644editdlrm
perlos400.pod47680644editdlrm
perlpacktut.pod512820644editdlrm
perlperf.pod498810644editdlrm
perlplan9.pod51250644editdlrm
perlpod.pod221960644editdlrm
perlpodspec.pod684760644editdlrm
perlpolicy.pod256290644editdlrm
perlport.pod876020644editdlrm
perlpragma.pod51760644editdlrm
perlqnx.pod66730644editdlrm
perlre.pod1209010644editdlrm
perlreapi.pod303340644editdlrm
perlrebackslash.pod318170644editdlrm
perlrecharclass.pod490290644editdlrm
perlref.pod353040644editdlrm
perlreftut.pod187900644editdlrm
perlreguts.pod383280644editdlrm
perlrepository.pod5090644editdlrm
perlrequick.pod184970644editdlrm
perlreref.pod147440644editdlrm
perlretut.pod1212570644editdlrm
perlriscos.pod15290644editdlrm
perlrun.pod535500644editdlrm
perlsec.pod261840644editdlrm
perlsolaris.pod298220644editdlrm
perlsource.pod68760644editdlrm
perlstyle.pod86300644editdlrm
perlsub.pod729670644editdlrm
perlsymbian.pod153590644editdlrm
perlsyn.pod445120644editdlrm
perlsynology.pod77780644editdlrm
perlthrtut.pod464590644editdlrm
perltie.pod386070644editdlrm
perltoc.pod6941710644editdlrm
perltodo.pod3760644editdlrm
perltooc.pod2940644editdlrm
perltoot.pod2940644editdlrm
perltrap.pod106200644editdlrm
perltru64.pod84920644editdlrm
perltw.pod44770644editdlrm
perlunicode.pod824910644editdlrm
perlunicook.pod254880644editdlrm
perlunifaq.pod136470644editdlrm
perluniintro.pod383400644editdlrm
perluniprops.pod2853060644editdlrm
perlunitut.pod79510644editdlrm
perlutil.pod76400644editdlrm
perlvar.pod783640644editdlrm
perlvms.pod508230644editdlrm
perlvos.pod38430644editdlrm
perlwin32.pod392980644editdlrm
perlxs.pod789200644editdlrm
perlxstut.pod500950644editdlrm
perlxstypemap.pod240010644editdlrm
Edit: /usr/share/perl5/pod/perlopentut.pod (9455B)
=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. A filehandle is an internal name for an external file. It is the job of the C function to make the association between the internal name and the external name, and it is the job of the C 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, C, C, and C. 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 = // die "how come no input?"; print STDOUT "Thank you!\n"; while () { ... } As you see from those examples, C and C are output handles, and C and C 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 = open(I, I, I)> Where: =over =item I will be some defined value if the open succeeds, but C if it fails; =item I should be an undefined scalar variable to be filled in by the C function if it succeeds; =item I is the access mode and the encoding format to open the file with; =item I is the external name of the file you want opened. =back Most of the complexity of the C function lies in the many possible values that the I 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 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, C, C, and C 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 function returns C 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 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: Having to specify the text encoding every time might seem a bit of a bother. To set up a default encoding for C so that you don't have to supply it each time, you can use the C 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 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, C, C, C, or C. 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, C, C, C, or C. 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 for details. =head1 Opening Binary Files If the file to be opened contains binary data instead of text characters, then the C argument to C 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 an explicit encoding to change it on the fly. This isn't exactly "binary" mode, but we still use C 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 instead of the variable-sized C 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.