[perldocjp-cvs 2079] CVS update: docs/perl/5.22.1

Back to archive index

argra****@users***** argra****@users*****
2018年 3月 18日 (日) 17:23:35 JST


Index: docs/perl/5.22.1/perlopentut.pod
diff -u /dev/null docs/perl/5.22.1/perlopentut.pod:1.1
--- /dev/null	Sun Mar 18 17:23:35 2018
+++ docs/perl/5.22.1/perlopentut.pod	Sun Mar 18 17:23:35 2018
@@ -0,0 +1,608 @@
+
+=encoding euc-jp
+
+=head1 NAME
+
+=begin original
+
+perlopentut - simple recipes for opening files and pipes in Perl
+
+=end original
+
+perlopentut - Perl でファイルを開いたりパイプを使ったりするための簡単なレシピ
+
+=head1 DESCRIPTION
+
+=begin original
+
+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.
+
+=end original
+
+Perl でファイルにタイして入出力をするとき、Perl では B<ファイルハンドル> と
+呼ばれるものを通して行います。
+ファイルハンドルは外部ファイルに対する内部名です。
+C<open> 関数の仕事は内部名と外部名を関連づけることで、C<close> 関数は
+関連づけを壊すことです。
+
+=begin original
+
+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:
+
+=end original
+
+便利なように、Perl は実行開始時に既に開いているいくつかの特別な
+ファイルハンドルを設定します。
+それは C<STDIN>, C<STDOUT>, C<STDERR>, C<ARGV> です。
+これらは既に開いているので、自分でこれらを開くときの問題を受けることなく
+正しく使うことができます。
+
+    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>) { ... }
+
+=begin original
+
+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.
+
+=end original
+
+これらの例で見られるように、C<STDOUT> と C<STDERR> は出力ハンドルで、
+C<STDIN> と C<ARGV> は入力ハンドルです。
+これらは C<@ARGV> 配列や C<%ENV> ハッシュと同様に Perl によって
+予約されているので、全て大文字になっています。
+これらの外部関連づけはシェルによって行われます。
+
+=begin original
+
+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:
+
+=end original
+
+その他のファイルハンドルは自分で開く必要があります。
+多くのバリエーションはありますが、Perl の open() 関数を開く最も一般的な方法は
+3 引数と一つの返り値のものです:
+
+=begin original
+
+C<    I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)>
+
+=end original
+
+C<    I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)>
+
+=begin original
+
+Where:
+
+=end original
+
+ここで:
+
+=over
+
+=item I<OK>
+
+=begin original
+
+will be some defined value if the open succeeds, but
+C<undef> if it fails;
+
+=end original
+
+これは、開くのに成功すれば何らかの定義された値、失敗すれば C<undef> です;
+
+=item I<HANDLE>
+
+=begin original
+
+should be an undefined scalar variable to be filled in by the
+C<open> function if it succeeds;
+
+=end original
+
+これは、成功すれば C<open> 巻数によって埋められる未定義のスカラ変数です;
+
+=item I<MODE>
+
+=begin original
+
+is the access mode and the encoding format to open the file with;
+
+=end original
+
+これはファイルを開くときのアクセスモードとエンコーディング型式です;
+
+=item I<PATHNAME>
+
+=begin original
+
+is the external name of the file you want opened.
+
+=end original
+
+これは開きたいファイルの外部名です。
+
+=back
+
+=begin original
+
+Most of the complexity of the C<open> function lies in the many
+possible values that the I<MODE> parameter can take on.
+
+=end original
+
+C<open> 関数の複雑さの大部分は、I<MODE> 引数が多くの値を
+取ることのできることにあります。
+
+=begin original
+
+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.
+
+=end original
+
+ファイルの開き方を説明する前に最後に一言: Perl ではファイルを開いても
+(普通は)自動的にロックすることはしません。
+ロックの方法については L<perlfaq5> を参照してください。
+
+=head1 Opening Text Files
+
+(テキストファイルを開く)
+
+=head2 Opening Text Files for Reading
+
+(読み込み用にテキストファイルを開く)
+
+=begin original
+
+If you want to read from a text file, first open it in
+read-only mode like this:
+
+=end original
+
+テキストファイルを読み込みたい場合、まず次のように読み込み専用モードで
+開きます:
+
+    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: $!";
+
+=begin original
+
+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.
+
+=end original
+
+シェルと同様に、Perl でもファイルを読み込み専用モードで開くために
+C<< "<" >> が使われます。
+これに成功すると、Perl は新しいファイルアンドルを割り当て、未定義だった
+C<$handle> 引数にそのハンドルへのリファレンスを設定します。
+
+=begin original
+
+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:
+
+=end original
+
+これでこのハンドルに対して C<readline>, C<read>, C<getc>,
+C<sysread> のような関数が使えます。
+おそらく最も一般的な入力関数は演算子のように見えるものでしょう:
+
+    $line = readline($handle);
+    $line = <$handle>;          # same thing
+
+=begin original
+
+Because the C<readline> function returns C<undef> at end of file or
+upon error, you will sometimes see it used this way:
+
+=end original
+
+C<readline> 関数はファイル終端やエラーのときに C<undef> を返すので、
+時々次のように使われているのを見るでしょう:
+
+    $line = <$handle>;
+    if (defined $line) {
+        # do something with $line
+    }
+    else {
+        # $line is not valid, so skip it
+    }
+
+=begin original
+
+You can also just quickly C<die> on an undefined value this way:
+
+=end original
+
+また、次のようにして単に未定義値に対してすばやく C<die> することもできます:
+
+    $line = <$handle> // die "no input found";
+
+=begin original
+
+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:
+
+=end original
+
+しかし、EOF に到達するのが想定されていて通常の出来事の場合は、
+入力がなくなっただけで終了したくありません。
+そうではなく、単に入力ループを終了したいでしょう。
+実際のエラーがループを終了させたのかをテストして、適切に行動できます:
+
+    while (<$handle>) {
+        # do something with data in $_
+    }
+    if ($!) {
+        die "unexpected error while reading from $filename: $!";
+    }
+
+=begin original
+
+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:
+
+=end original
+
+B<エンコーディングに関する注意>: テキストエンコーディングを毎回指定する
+必要があるのは少し面倒に感じるかもしれません。
+毎回設定する必要がないように C<open> のためのデフォルトエンコーディングを
+設定するために、C<open> プラグマを使えます:
+
+    use open qw< :encoding(UTF-8) >;
+
+=begin original
+
+Once you've done that, you can safely omit the encoding part of the
+open mode:
+
+=end original
+
+一度これを行えば、open モードからエンコーディングの部分を安全に省略できます:
+
+    open($handle, "<", $filename)
+        || die "$0: can't open $filename for reading: $!";
+
+=begin original
+
+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.
+
+=end original
+
+しかし、先にデフォルトのエンコーディングを設定することなく裸の
+C<< "<" >> を使うことは決してしないでください。
+さもなければ、Perl はとてもとてもとてもたくさんあるテキストファイルの
+種類のうちどれかを知ることができず、Perl はあなたのファイルのデータを
+動作させるための実際の文字にマッピングすることができません。
+その他のよくあるエンコーディング形式には
+C<"ASCII">, C<"ISO-8859-1">,
+C<"ISO-8859-15">, C<"Windows-1252">, C<"MacRoman"> および、
+C<"UTF-16LE"> すらもあります。
+エンコーディングに関するさらなる情報については L<perlunitut> を
+参照してください。
+
+=head2 Opening Text Files for Writing
+
+(書き込み用にテキストファイルを開く)
+
+=begin original
+
+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.
+
+=end original
+
+ファイルに書き込みたい場合、そのファイルの既存の内容をどうするかを
+まず決定する必要があります。
+二つの基本的な選択肢があります: 保存するか上書きするかです。
+
+=begin original
+
+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.
+
+=end original
+
+既存の内容を保存したい場合、ファイルを追記モードで開きます。
+シェルと同様に、 Perl でも既存のファイルを追記モードで開くために
+C<<< ">>" >>> が使われます。
+ファイルがない場合、C<<< ">>" >>> はファイルを作ります。
+
+    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: $!";
+
+=begin original
+
+Now you can write to that filehandle using any of C<print>, C<printf>,
+C<say>, C<write>, or C<syswrite>.
+
+=end original
+
+これでこのハンドルに対して C<print>, C<printf>,
+C<say>, C<write>, C<syswrite> を使って書き込めます。
+
+=begin original
+
+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.
+
+=end original
+
+前述したように、ファイルが既に存在していない場合、追記モードで開くと
+ファイルを作ります。
+しかしファイルが既に存在している場合、その内容は保護されます; 新しいテキストは
+既存のテキストの末尾に追加されるからです。
+
+=begin original
+
+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:
+
+=end original
+
+一方、時々、既に何かがあっても上書きしたいときもあります。
+書き込みを始める前にファイルを消すために、書き込み専用モードで
+開くことができます:
+
+    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: $!";
+
+=begin original
+
+Here again Perl works just like the shell in that the C<< ">" >> clobbers
+an existing file.
+
+=end original
+
+ここで再び Perl はシェルと同様に動作し、C<< ">" >> は既存のファイルを
+上書きします。
+
+=begin original
+
+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>.
+
+=end original
+
+追記モードと同様に、ファイルを書き込みモードで開くと、
+C<print>, C<printf>, C<say>, C<write>, C<syswrite> を使って
+ファイルハンドルに書き込めるようになります。
+
+=begin original
+
+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.
+
+=end original
+
+読み書きモードについては?
+おそらくそれは存在しないというふりをした方がよいでしょう;
+なぜならテキストファイルを読み書きモードで開いても
+おそらくあなたが望んでいることをしないからです。
+詳しくは L<perlfaq5> を参照してください。
+
+=head1 Opening Binary Files
+
+(バイナリファイルを開く)
+
+=begin original
+
+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.
+
+=end original
+
+開こうとしているファイルがテキスト文字ではなくバイナリデータが含まれている
+場合、C<open> の C<MODE> 引数は少し異なるものになります。
+エンコーディングを指定する代わりに、データが生のバイト列であることを
+Perl に知らせます。
+
+    my $filename = "/some/path/to/a/binary/file/goes/here";
+    my $encoding = ":raw :bytes"
+    my $handle   = undef;     # this will be filled in on success
+
+=begin original
+
+And then open as before, choosing C<<< "<" >>>, C<<< ">>" >>>, or
+C<<< ">" >>> as needed:
+
+=end original
+
+それから前述の通り、必要に応じて
+C<<< "<" >>>, C<<< ">>" >>>, C<<< ">" >>> を選びます:
+
+    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: $!";
+
+=begin original
+
+Alternately, you can change to binary mode on an existing handle this way:
+
+=end original
+
+あるいは、次のようにして既に存在しているハンドルをバイナリモードに
+変えることが出来ます:
+
+    binmode($handle)    || die "cannot binmode handle";
+
+=begin original
+
+This is especially handy for the handles that Perl has already opened for you.
+
+=end original
+
+これは、Perl が既に開いているハンドルに対して特に有用です。
+
+    binmode(STDIN)      || die "cannot binmode STDIN";
+    binmode(STDOUT)     || die "cannot binmode STDOUT";
+
+=begin original
+
+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:
+
+=end original
+
+また、その場で変更するために C<binmode> に明示的にエンコーディングを
+渡すこともできます。
+これは正確には「バイナリ」モードではありませんが、それでも
+これをするために C<binmode> を使います:
+
+  binmode(STDIN,  ":encoding(MacRoman)") || die "cannot binmode STDIN";
+  binmode(STDOUT, ":encoding(UTF-8)")    || die "cannot binmode STDOUT";
+
+=begin original
+
+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.
+
+=end original
+
+一旦バイナリファイルを正しいモードで適切に開くと、テキストファイルで
+使ったものと全て同じ Perl I/O 関数を使えます。
+しかし、入力に対して可変長の C<readline> ではなく固定長の
+C<read> を使った方が良いでしょう。
+
+=begin original
+
+Here's an example of how to copy a binary file:
+
+=end original
+
+次のものはバイナリファイルをコピーする例です:
+
+    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
+
+(パイプを開く)
+
+=begin original
+
+To be announced.
+
+=end original
+
+未定。
+
+=head1 Low-level File Opens via sysopen
+
+(sysopen 経由で低レベルにファイルを開く)
+
+=begin original
+
+To be announced.  Or deleted.
+
+=end original
+
+未定。
+または削除する。
+
+=head1 SEE ALSO
+
+=begin original
+
+To be announced.
+
+=end original
+
+未定。
+
+=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.
+
+=begin meta
+
+Translate: SHIRAKTA Kentaro <argra****@ub32*****>
+Status: completed
+
+=end meta
+



perldocjp-cvs メーリングリストの案内
Back to archive index