perlopentut > 5.36.0 との差分

perlopentut 5.36.0 と 5.26.1 の差分

11
2=encoding utf8
2=encoding euc-jp
33
44=head1 NAME
55
66=begin original
77
88perlopentut - simple recipes for opening files and pipes in Perl
99
1010=end original
1111
1212perlopentut - Perl でファイルを開いたりパイプを使ったりするための簡単なレシピ
1313
1414=head1 DESCRIPTION
1515
1616=begin original
1717
1818Whenever you do I/O on a file in Perl, you do so through what in Perl is
1919called a B<filehandle>. A filehandle is an internal name for an external
2020file. It is the job of the C<open> function to make the association
2121between the internal name and the external name, and it is the job
2222of the C<close> function to break that association.
2323
2424=end original
2525
2626Perl でファイルに対して入出力をするとき、Perl では B<ファイルハンドル> と
2727呼ばれるものを通して行います。
2828ファイルハンドルは外部ファイルに対する内部名です。
2929C<open> 関数の仕事は内部名と外部名を関連づけることで、C<close> 関数は
3030関連づけを壊すことです。
3131
3232=begin original
3333
3434For your convenience, Perl sets up a few special filehandles that are
3535already open when you run. These include C<STDIN>, C<STDOUT>, C<STDERR>,
3636and C<ARGV>. Since those are pre-opened, you can use them right away
3737without having to go to the trouble of opening them yourself:
3838
3939=end original
4040
4141便利なように、Perl は実行開始時に既に開いているいくつかの特別な
4242ファイルハンドルを設定します。
4343それは C<STDIN>, C<STDOUT>, C<STDERR>, C<ARGV> です。
4444これらは既に開いているので、自分でこれらを開くときの問題を受けることなく
4545正しく使うことができます。
4646
4747 print STDERR "This is a debugging message.\n";
4848
4949 print STDOUT "Please enter something: ";
5050 $response = <STDIN> // die "how come no input?";
5151 print STDOUT "Thank you!\n";
5252
5353 while (<ARGV>) { ... }
5454
5555=begin original
5656
5757As you see from those examples, C<STDOUT> and C<STDERR> are output
5858handles, and C<STDIN> and C<ARGV> are input handles. They are
5959in all capital letters because they are reserved to Perl, much
6060like the C<@ARGV> array and the C<%ENV> hash are. Their external
6161associations were set up by your shell.
6262
6363=end original
6464
6565これらの例で見られるように、C<STDOUT> と C<STDERR> は出力ハンドルで、
6666C<STDIN> と C<ARGV> は入力ハンドルです。
6767これらは C<@ARGV> 配列や C<%ENV> ハッシュと同様に Perl によって
6868予約されているので、全て大文字になっています。
6969これらの外部関連づけはシェルによって行われます。
7070
7171=begin original
7272
7373You will need to open every other filehandle on your own. Although there
7474are many variants, the most common way to call Perl's open() function
7575is with three arguments and one return value:
7676
7777=end original
7878
7979その他のファイルハンドルは自分で開く必要があります。
8080多くのバリエーションはありますが、Perl の open() 関数を開く最も一般的な方法は
81813 引数と一つの返り値のものです:
8282
8383=begin original
8484
8585C< I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)>
8686
8787=end original
8888
8989C< I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)>
9090
9191=begin original
9292
9393Where:
9494
9595=end original
9696
9797ここで:
9898
9999=over
100100
101101=item I<OK>
102102
103103=begin original
104104
105105will be some defined value if the open succeeds, but
106106C<undef> if it fails;
107107
108108=end original
109109
110110これは、開くのに成功すれば何らかの定義された値、失敗すれば C<undef> です;
111111
112112=item I<HANDLE>
113113
114114=begin original
115115
116116should be an undefined scalar variable to be filled in by the
117117C<open> function if it succeeds;
118118
119119=end original
120120
121121これは、成功すれば C<open> 関数によって埋められる未定義のスカラ変数です;
122122
123123=item I<MODE>
124124
125125=begin original
126126
127127is the access mode and the encoding format to open the file with;
128128
129129=end original
130130
131131これはファイルを開くときのアクセスモードとエンコーディング型式です;
132132
133133=item I<PATHNAME>
134134
135135=begin original
136136
137137is the external name of the file you want opened.
138138
139139=end original
140140
141141これは開きたいファイルの外部名です。
142142
143143=back
144144
145145=begin original
146146
147147Most of the complexity of the C<open> function lies in the many
148148possible values that the I<MODE> parameter can take on.
149149
150150=end original
151151
152152C<open> 関数の複雑さの大部分は、I<MODE> 引数が多くの値を
153153取ることのできることにあります。
154154
155155=begin original
156156
157157One last thing before we show you how to open files: opening
158158files does not (usually) automatically lock them in Perl. See
159159L<perlfaq5> for how to lock.
160160
161161=end original
162162
163163ファイルの開き方を説明する前に最後に一言: Perl ではファイルを開いても
164164(普通は)自動的にロックすることはしません。
165165ロックの方法については L<perlfaq5> を参照してください。
166166
167167=head1 Opening Text Files
168168
169169(テキストファイルを開く)
170170
171171=head2 Opening Text Files for Reading
172172
173173(読み込み用にテキストファイルを開く)
174174
175175=begin original
176176
177177If you want to read from a text file, first open it in
178178read-only mode like this:
179179
180180=end original
181181
182182テキストファイルを読み込みたい場合、まず次のように読み込み専用モードで
183183開きます:
184184
185185 my $filename = "/some/path/to/a/textfile/goes/here";
186186 my $encoding = ":encoding(UTF-8)";
187187 my $handle = undef; # this will be filled in on success
188188
189189 open($handle, "< $encoding", $filename)
190190 || die "$0: can't open $filename for reading: $!";
191191
192192=begin original
193193
194194As with the shell, in Perl the C<< "<" >> is used to open the file in
195195read-only mode. If it succeeds, Perl allocates a brand new filehandle for
196196you and fills in your previously undefined C<$handle> argument with a
197197reference to that handle.
198198
199199=end original
200200
201201シェルと同様に、Perl でもファイルを読み込み専用モードで開くために
202202C<< "<" >> が使われます。
203203これに成功すると、Perl は新しいファイルハンドルを割り当て、未定義だった
204204C<$handle> 引数にそのハンドルへのリファレンスを設定します。
205205
206206=begin original
207207
208208Now you may use functions like C<readline>, C<read>, C<getc>, and
209209C<sysread> on that handle. Probably the most common input function
210210is the one that looks like an operator:
211211
212212=end original
213213
214214これでこのハンドルに対して C<readline>, C<read>, C<getc>,
215215C<sysread> のような関数が使えます。
216216おそらく最も一般的な入力関数は演算子のように見えるものでしょう:
217217
218218 $line = readline($handle);
219219 $line = <$handle>; # same thing
220220
221221=begin original
222222
223223Because the C<readline> function returns C<undef> at end of file or
224224upon error, you will sometimes see it used this way:
225225
226226=end original
227227
228228C<readline> 関数はファイル終端やエラーのときに C<undef> を返すので、
229229時々次のように使われているのを見るでしょう:
230230
231231 $line = <$handle>;
232232 if (defined $line) {
233233 # do something with $line
234234 }
235235 else {
236236 # $line is not valid, so skip it
237237 }
238238
239239=begin original
240240
241241You can also just quickly C<die> on an undefined value this way:
242242
243243=end original
244244
245245また、次のようにして単に未定義値に対してすばやく C<die> することもできます:
246246
247247 $line = <$handle> // die "no input found";
248248
249249=begin original
250250
251251However, if hitting EOF is an expected and normal event, you do not want to
252252exit simply because you have run out of input. Instead, you probably just want
253253to exit an input loop. You can then test to see if an actual error has caused
254254the loop to terminate, and act accordingly:
255255
256256=end original
257257
258258しかし、EOF に到達するのが想定されていて通常の出来事の場合は、
259259入力がなくなっただけで終了したくありません。
260260そうではなく、単に入力ループを終了したいでしょう。
261261実際のエラーがループを終了させたのかをテストして、適切に行動できます:
262262
263263 while (<$handle>) {
264264 # do something with data in $_
265265 }
266266 if ($!) {
267267 die "unexpected error while reading from $filename: $!";
268268 }
269269
270270=begin original
271271
272272B<A Note on Encodings>: Having to specify the text encoding every time
273273might seem a bit of a bother. To set up a default encoding for C<open> so
274274that you don't have to supply it each time, you can use the C<open> pragma:
275275
276276=end original
277277
278278B<エンコーディングに関する注意>: テキストエンコーディングを毎回指定する
279279必要があるのは少し面倒に感じるかもしれません。
280280毎回設定する必要がないように C<open> のためのデフォルトエンコーディングを
281281設定するために、C<open> プラグマを使えます:
282282
283283 use open qw< :encoding(UTF-8) >;
284284
285285=begin original
286286
287287Once you've done that, you can safely omit the encoding part of the
288288open mode:
289289
290290=end original
291291
292292一度これを行えば、open モードからエンコーディングの部分を安全に省略できます:
293293
294294 open($handle, "<", $filename)
295295 || die "$0: can't open $filename for reading: $!";
296296
297297=begin original
298298
299299But never use the bare C<< "<" >> without having set up a default encoding
300300first. Otherwise, Perl cannot know which of the many, many, many possible
301301flavors of text file you have, and Perl will have no idea how to correctly
302302map the data in your file into actual characters it can work with. Other
303303common encoding formats including C<"ASCII">, C<"ISO-8859-1">,
304304C<"ISO-8859-15">, C<"Windows-1252">, C<"MacRoman">, and even C<"UTF-16LE">.
305305See L<perlunitut> for more about encodings.
306306
307307=end original
308308
309309しかし、先にデフォルトのエンコーディングを設定することなく裸の
310310C<< "<" >> を使うことは決してしないでください。
311311さもなければ、Perl はとてもとてもとてもたくさんあるテキストファイルの
312312種類のうちどれかを知ることができず、Perl はあなたのファイルのデータを
313313動作させるための実際の文字にマッピングすることができません。
314314その他のよくあるエンコーディング形式には
315315C<"ASCII">, C<"ISO-8859-1">,
316316C<"ISO-8859-15">, C<"Windows-1252">, C<"MacRoman"> および、
317317C<"UTF-16LE"> すらもあります。
318318エンコーディングに関するさらなる情報については L<perlunitut> を
319319参照してください。
320320
321321=head2 Opening Text Files for Writing
322322
323323(書き込み用にテキストファイルを開く)
324324
325325=begin original
326326
327327When you want to write to a file, you first have to decide what to do about
328328any existing contents of that file. You have two basic choices here: to
329329preserve or to clobber.
330330
331331=end original
332332
333333ファイルに書き込みたい場合、そのファイルの既存の内容をどうするかを
334334まず決定する必要があります。
335335二つの基本的な選択肢があります: 保存するか上書きするかです。
336336
337337=begin original
338338
339339If you want to preserve any existing contents, then you want to open the file
340340in append mode. As in the shell, in Perl you use C<<< ">>" >>> to open an
341341existing file in append mode. C<<< ">>" >>> creates the file if it does not
342342already exist.
343343
344344=end original
345345
346346既存の内容を保存したい場合、ファイルを追記モードで開きます。
347347シェルと同様に、 Perl でも既存のファイルを追記モードで開くために
348348C<<< ">>" >>> が使われます。
349349ファイルがない場合、C<<< ">>" >>> はファイルを作ります。
350350
351351 my $handle = undef;
352352 my $filename = "/some/path/to/a/textfile/goes/here";
353353 my $encoding = ":encoding(UTF-8)";
354354
355355 open($handle, ">> $encoding", $filename)
356356 || die "$0: can't open $filename for appending: $!";
357357
358358=begin original
359359
360360Now you can write to that filehandle using any of C<print>, C<printf>,
361361C<say>, C<write>, or C<syswrite>.
362362
363363=end original
364364
365365これでこのハンドルに対して C<print>, C<printf>,
366366C<say>, C<write>, C<syswrite> を使って書き込めます。
367367
368368=begin original
369369
370370As noted above, if the file does not already exist, then the append-mode open
371371will create it for you. But if the file does already exist, its contents are
372372safe from harm because you will be adding your new text past the end of the
373373old text.
374374
375375=end original
376376
377377前述したように、ファイルが既に存在していない場合、追記モードで開くと
378378ファイルを作ります。
379379しかしファイルが既に存在している場合、その内容は保護されます; 新しいテキストは
380380既存のテキストの末尾に追加されるからです。
381381
382382=begin original
383383
384384On the other hand, sometimes you want to clobber whatever might already be
385385there. To empty out a file before you start writing to it, you can open it
386386in write-only mode:
387387
388388=end original
389389
390390一方、時々、既に何かがあっても上書きしたいときもあります。
391391書き込みを始める前にファイルを消すために、書き込み専用モードで
392392開くことができます:
393393
394394 my $handle = undef;
395395 my $filename = "/some/path/to/a/textfile/goes/here";
396396 my $encoding = ":encoding(UTF-8)";
397397
398398 open($handle, "> $encoding", $filename)
399399 || die "$0: can't open $filename in write-open mode: $!";
400400
401401=begin original
402402
403403Here again Perl works just like the shell in that the C<< ">" >> clobbers
404404an existing file.
405405
406406=end original
407407
408408ここで再び Perl はシェルと同様に動作し、C<< ">" >> は既存のファイルを
409409上書きします。
410410
411411=begin original
412412
413413As with the append mode, when you open a file in write-only mode,
414414you can now write to that filehandle using any of C<print>, C<printf>,
415415C<say>, C<write>, or C<syswrite>.
416416
417417=end original
418418
419419追記モードと同様に、ファイルを書き込みモードで開くと、
420420C<print>, C<printf>, C<say>, C<write>, C<syswrite> を使って
421421ファイルハンドルに書き込めるようになります。
422422
423423=begin original
424424
425425What about read-write mode? You should probably pretend it doesn't exist,
426426because opening text files in read-write mode is unlikely to do what you
427427would like. See L<perlfaq5> for details.
428428
429429=end original
430430
431431読み書きモードについては?
432432おそらくそれは存在しないというふりをした方がよいでしょう;
433433なぜならテキストファイルを読み書きモードで開いても
434434おそらくあなたが望んでいることをしないからです。
435435詳しくは L<perlfaq5> を参照してください。
436436
437437=head1 Opening Binary Files
438438
439439(バイナリファイルを開く)
440440
441441=begin original
442442
443443If the file to be opened contains binary data instead of text characters,
444444then the C<MODE> argument to C<open> is a little different. Instead of
445445specifying the encoding, you tell Perl that your data are in raw bytes.
446446
447447=end original
448448
449449開こうとしているファイルがテキスト文字ではなくバイナリデータが含まれている
450450場合、C<open> の C<MODE> 引数は少し異なるものになります。
451451エンコーディングを指定する代わりに、データが生のバイト列であることを
452452Perl に知らせます。
453453
454454 my $filename = "/some/path/to/a/binary/file/goes/here";
455455 my $encoding = ":raw :bytes"
456456 my $handle = undef; # this will be filled in on success
457457
458458=begin original
459459
460460And then open as before, choosing C<<< "<" >>>, C<<< ">>" >>>, or
461461C<<< ">" >>> as needed:
462462
463463=end original
464464
465465それから前述の通り、必要に応じて
466466C<<< "<" >>>, C<<< ">>" >>>, C<<< ">" >>> を選びます:
467467
468468 open($handle, "< $encoding", $filename)
469469 || die "$0: can't open $filename for reading: $!";
470470
471471 open($handle, ">> $encoding", $filename)
472472 || die "$0: can't open $filename for appending: $!";
473473
474474 open($handle, "> $encoding", $filename)
475475 || die "$0: can't open $filename in write-open mode: $!";
476476
477477=begin original
478478
479479Alternately, you can change to binary mode on an existing handle this way:
480480
481481=end original
482482
483483あるいは、次のようにして既に存在しているハンドルをバイナリモードに
484484変えることが出来ます:
485485
486486 binmode($handle) || die "cannot binmode handle";
487487
488488=begin original
489489
490490This is especially handy for the handles that Perl has already opened for you.
491491
492492=end original
493493
494494これは、Perl が既に開いているハンドルに対して特に有用です。
495495
496496 binmode(STDIN) || die "cannot binmode STDIN";
497497 binmode(STDOUT) || die "cannot binmode STDOUT";
498498
499499=begin original
500500
501501You can also pass C<binmode> an explicit encoding to change it on the fly.
502502This isn't exactly "binary" mode, but we still use C<binmode> to do it:
503503
504504=end original
505505
506506また、その場で変更するために C<binmode> に明示的にエンコーディングを
507507渡すこともできます。
508508これは正確には「バイナリ」モードではありませんが、それでも
509509これをするために C<binmode> を使います:
510510
511511 binmode(STDIN, ":encoding(MacRoman)") || die "cannot binmode STDIN";
512512 binmode(STDOUT, ":encoding(UTF-8)") || die "cannot binmode STDOUT";
513513
514514=begin original
515515
516516Once you have your binary file properly opened in the right mode, you can
517517use all the same Perl I/O functions as you used on text files. However,
518518you may wish to use the fixed-size C<read> instead of the variable-sized
519519C<readline> for your input.
520520
521521=end original
522522
523523一旦バイナリファイルを正しいモードで適切に開くと、テキストファイルで
524524使ったものと全て同じ Perl I/O 関数を使えます。
525525しかし、入力に対して可変長の C<readline> ではなく固定長の
526526C<read> を使った方が良いでしょう。
527527
528528=begin original
529529
530530Here's an example of how to copy a binary file:
531531
532532=end original
533533
534534次のものはバイナリファイルをコピーする例です:
535535
536536 my $BUFSIZ = 64 * (2 ** 10);
537537 my $name_in = "/some/input/file";
538538 my $name_out = "/some/output/flie";
539539
540540 my($in_fh, $out_fh, $buffer);
541541
542542 open($in_fh, "<", $name_in)
543543 || die "$0: cannot open $name_in for reading: $!";
544544 open($out_fh, ">", $name_out)
545545 || die "$0: cannot open $name_out for writing: $!";
546546
547547 for my $fh ($in_fh, $out_fh) {
548548 binmode($fh) || die "binmode failed";
549549 }
550550
551551 while (read($in_fh, $buffer, $BUFSIZ)) {
552552 unless (print $out_fh $buffer) {
553553 die "couldn't write to $name_out: $!";
554554 }
555555 }
556556
557557 close($in_fh) || die "couldn't close $name_in: $!";
558558 close($out_fh) || die "couldn't close $name_out: $!";
559559
560560=head1 Opening Pipes
561561
562562(パイプを開く)
563563
564564=begin original
565565
566Perl also lets you open a filehandle into an external program or shell
566To be announced.
567command rather than into a file. You can do this in order to pass data
568from your Perl program to an external command for further processing, or
569to receive data from another program for your own Perl program to
570process.
571567
572568=end original
573569
574Perl はまた、ファイルではなく外部プログラムやシェルコマンドへの
570未定。
575ファイルハンドルも開きます。
576これを、更なる処理のために Perl プログラムから外部コマンドへ渡すため、
577または処理する Perl プログラムのために他のプログラムからデータを
578受け取るために行えます。
579571
580=begin original
572=head1 Low-level File Opens via sysopen
581573
582Filehandles into commands are also known as I<pipes>, since they work on
574(sysopen 経由で低レベルにファイルを開く)
583similar inter-process communication principles as Unix pipelines. Such a
584filehandle has an active program instead of a static file on its
585external end, but in every other sense it works just like a more typical
586file-based filehandle, with all the techniques discussed earlier in this
587article just as applicable.
588575
589=end original
590
591コマンドへのファイルハンドルは、I<パイプ> としても知られます;
592Unix パイプラインという似たようなプロセス間通信原則に基づいて
593動作するからです。
594そのようなファイルハンドルは、外側が静的なファイルではなく
595動作中のプログラムですが、それ以外の点については
596より典型的なファイルベースのファイルハンドルとちょうど同じように
597動作し、この文書で既に議論した全てのテクニックが利用可能です。
598
599576=begin original
600577
601As such, you open a pipe using the same C<open> call that you use for
578To be announced. Or deleted.
602opening files, setting the second (C<MODE>) argument to special
603characters that indicate either an input or an output pipe. Use C<"-|"> for a
604filehandle that will let your Perl program read data from an external
605program, and C<"|-"> for a filehandle that will send data to that
606program instead.
607579
608580=end original
609581
6102 番目の (C<MODE>) 引数にパイプの入力または出力を示す特殊な文字を
582未定。
611設定することで、ファイルを開くのに使うのと同じ C<open> で
583または削除する
612パイプを開きます。
613Perl プログラムが外部プログラムからデータを読み込むファイルハンドルには
614C<"-|"> を使います; プログラムにデータを送るファイルハンドルには
615C<"|-"> を使います。
616584
617=head2 Opening a pipe for reading
618
619(読み込み用にパイプを開く)
620
621=begin original
622
623Let's say you'd like your Perl program to process data stored in a nearby
624directory called C<unsorted>, which contains a number of textfiles.
625You'd also like your program to sort all the contents from these files
626into a single, alphabetically sorted list of unique lines before it
627starts processing them.
628
629=end original
630
631たくさんのテキストファイルが含まれている、C<unsorted> と呼ばれる
632近くのディレクトリに保管されているデータを処理する
633Perl プログラムが欲しいとしましょう。
634また、処理を開始する前に、複数のファイルを単一の、ユニークな行を
635アルファベット順にソートしたいとします。
636
637=begin original
638
639You could do this through opening an ordinary filehandle into each of
640those files, gradually building up an in-memory array of all the file
641contents you load this way, and finally sorting and filtering that array
642when you've run out of files to load. I<Or>, you could offload all that
643merging and sorting into your operating system's own C<sort> command by
644opening a pipe directly into its output, and get to work that much
645faster.
646
647=end original
648
649それぞれのファイルに対して通常のファイルハンドルを開き、
650このようにして読み込んだ全てのファイルの内容を徐々にメモリ内の配列に
651構築し、読み込むファイルがなくなったら最後にソートとフィルタリングをする、
652という形でこれを行うことも出来ます。
653I<あるいは>、結合とソートをオペレーティング自身の C<sort> コマンドに
654任せて、その出力を直接パイプで開くことで、遙かに速く作業することも出来ます。
655
656=begin original
657
658Here's how that might look:
659
660=end original
661
662以下は、これがどのように見えるかです:
663
664 open(my $sort_fh, '-|', 'sort -u unsorted/*.txt')
665 or die "Couldn't open a pipe into sort: $!";
666
667 # And right away, we can start reading sorted lines:
668 while (my $line = <$sort_fh>) {
669 #
670 # ... Do something interesting with each $line here ...
671 #
672 }
673
674=begin original
675
676The second argument to C<open>, C<"-|">, makes it a read-pipe into a
677separate program, rather than an ordinary filehandle into a file.
678
679=end original
680
681C<open> の 2 番目の引数である C<"-|"> は、ファイルへの通常の
682ファイルハンドルではなく、別個のプログラムへの読み込みパイプにします。
683
684=begin original
685
686Note that the third argument to C<open> is a string containing the
687program name (C<sort>) plus all its arguments: in this case, C<-u> to
688specify unqiue sort, and then a fileglob specifying the files to sort.
689The resulting filehandle C<$sort_fh> works just like a read-only (C<<
690"<" >>) filehandle, and your program can subsequently read data
691from it as if it were opened onto an ordinary, single file.
692
693=end original
694
695C<open> の 3 番目の引数は、
696プログラム名 (C<sort>) とその全ての引数を含んだ文字列です:
697この場合、C<-u> はユニークソートを指定し、それからファイルグロブは
698ソートするファイルを指定することに注意してください。
699結果のファイルハンドル C<$sort_fh> は
700ちょうど読み込み専用 (C<< "<" >>) ファイルハンドルのように動作し、
701プログラムは、通常の単一のファイルが開かれたかのように、
702引き続いてそこからデータを読み込むことができます。
703
704=head2 Opening a pipe for writing
705
706(書き込み用にパイプを開く)
707
708=begin original
709
710Continuing the previous example, let's say that your program has
711completed its processing, and the results sit in an array called
712C<@processed>. You want to print these lines to a file called
713C<numbered.txt> with a neatly formatted column of line-numbers.
714
715=end original
716
717前回の例の続きとして、プログラムの処理を完成させて、
718結果は C<@processed> と呼ばれる配列に入っているとしましょう。
719これらの行を C<numbered.txt> というファイル名に、
720いい感じに整形された行番号の列と共に出力したいとします。
721
722=begin original
723
724Certainly you could write your own code to do this ? or, once again,
725you could kick that work over to another program. In this case, C<cat>,
726running with its own C<-n> option to activate line numbering, should do
727the trick:
728
729=end original
730
731確かにこれをするコードを自分で書くこともできます - あるいは、再び、
732この作業を他のプログラムに送ることもできます。
733この場合、C<cat> を、行番号付けを有効にする C<-n> オプション込みで
734実行するには、次の技を使います:
735
736 open(my $cat_fh, '|-', 'cat -n > numbered.txt')
737 or die "Couldn't open a pipe into cat: $!";
738
739 for my $line (@processed) {
740 print $cat_fh $line;
741 }
742
743=begin original
744
745Here, we use a second C<open> argument of C<"|-">, signifying that the
746filehandle assigned to C<$cat_fh> should be a write-pipe. We can then
747use it just as we would a write-only ordinary filehandle, including the
748basic function of C<print>-ing data to it.
749
750=end original
751
752ここで、C<open> の 2 番目の引数に C<"|-"> を使います;
753これにより、C<$cat_fh> に代入されるファイルハンドルが書き込み
754パイプであることを示します。
755それから、データを C<print> する基本的な関数を含めて、
756書き込み専用の普通のファイルハンドルを使うのと同じようにこれを使えます。
757
758=begin original
759
760Note that the third argument, specifying the command that we wish to
761pipe to, sets up C<cat> to redirect its output via that C<< ">" >>
762symbol into the file C<numbered.txt>. This can start to look a little
763tricky, because that same symbol would have meant something
764entirely different had it showed it in the second argument to C<open>!
765But here in the third argument, it's simply part of the shell command that
766Perl will open the pipe into, and Perl itself doesn't invest any special
767meaning to it.
768
769=end original
770
771パイプしたいコマンドを指定する 3 番目の引数は、
772C<cat> の出力を C<< ">" >> 記号を使ってファイル C<numbered.txt> に
773リダイレクトするように指定していることに注意してください。
774これは最初は少しおかしく見えるかもしれません;
775この同じ記号は、C<open> の 2 番目の引数では全く違うものを意味するからです!
776しかし、ここ 3 番目の引数では、これは単に Perl がパイプを開く
777シェルコマンドの一部であり、Perl 自身はこれに何の特別な意味も与えません。
778
779=head2 Expressing the command as a list
780
781(コマンドをリストとして表現する)
782
783=begin original
784
785For opening pipes, Perl offers the option to call C<open> with a list
786comprising the desired command and all its own arguments as separate
787elements, rather than combining them into a single string as in the
788examples above. For instance, we could have phrased the C<open> call in
789the first example like this:
790
791=end original
792
793パイプを開くために、Perl は、目的のコマンドとそれ自身の引数を、
794前述の例のように単一の文字列として結合するのではなく、
795別個の要素として構成されたリストで C<open> を呼び出すという
796選択肢を提供しています。
797例えば、最初の例の C<open> 呼び出しは次のように書けます:
798
799 open(my $sort_fh, '-|', 'sort', '-u', glob('unsorted/*.txt'))
800 or die "Couldn't open a pipe into sort: $!";
801
802=begin original
803
804When you call C<open> this way, Perl invokes the given command directly,
805bypassing the shell. As such, the shell won't try to interpret any
806special characters within the command's argument list, which might
807overwise have unwanted effects. This can make for safer, less
808error-prone C<open> calls, useful in cases such as passing in variables
809as arguments, or even just referring to filenames with spaces in them.
810
811=end original
812
813この方法で C<open> を呼び出す場合、
814Perl はシェルをバイパスして指定されたコマンドを直接起動します。
815シェルはコマンド路の引数リストの中の特殊文字を解釈しようとはしません;
816さもなければ望まない効果を生むことがあります。
817これはより安全で、C<open> 呼び出しの誤りを減らし、
818引数として変数の内容を渡すような場合に有用で、
819単に空白を含むファイルを参照する場合にも安全です。
820
821=begin original
822
823However, when you I<do> want to pass a meaningful metacharacter to the
824shell, such with the C<"*"> inside that final C<unsorted/*.txt> argument
825here, you can't use this alternate syntax. In this case, we have worked
826around it via Perl's handy C<glob> built-in function, which evaluates
827its argument into a list of filenames ? and we can safely pass that
828resulting list right into C<open>, as shown above.
829
830=end original
831
832しかし、シェルに意味のあるメタ文字を I<渡したい>、
833例えば最終的な C<unsorted/*.txt> の中の C<"*"> のような場合、
834この代替文法は使えません。
835この場合、引数をファイル名として評価する Perl の便利な C<glob> 組み込み関数で
836回避します; そして前述したように、結果のリストを C<open> に安全に
837渡せます。
838
839=begin original
840
841Note also that representing piped-command arguments in list form like
842this doesn't work on every platform. It will work on any Unix-based OS
843that provides a real C<fork> function (e.g. macOS or Linux), as well as
844on Windows when running Perl 5.22 or later.
845
846=end original
847
848また、このようなリスト形式でのパイプコマンド引数表現は、全ての
849プラットフォームで動作するわけではないことに注意してください。
850真の C<fork> 関数を提供する Unix ベースの OS (例えば macOS や Linux)、
851および Perl 5.22 以降の Windows では動作します。
852
853585=head1 SEE ALSO
854586
855587=begin original
856588
857The full documentation for L<C<open>|perlfunc/open FILEHANDLE,MODE,EXPR>
589To be announced.
858provides a thorough reference to this function, beyond the best-practice
859basics covered here.
860590
861591=end original
862592
863L<C<open>|perlfunc/open FILEHANDLE,MODE,EXPR> の完全な文書は、
593未定。
864ここでカバーしているベストプラクティスベースのものを超えて、
865この関数の完全なリファレンスを提供します。
866594
867595=head1 AUTHOR and COPYRIGHT
868596
869Copyright 2013 Tom Christiansen; now maintained by Perl5 Porters
597Copyright 2013 Tom Christiansen.
870598
871599This documentation is free; you can redistribute it and/or modify it under
872600the same terms as Perl itself.
873601
874602=begin meta
875603
876604Translate: SHIRAKTA Kentaro <argrath@ub32.org>
877605Status: completed
878606
879607=end meta