ダイヤモンド演算子

行入力演算子は、<IN>というようにファイルハンドルから1行ずつ読み出す形であった。
ファイルハンドルを指定しない<>はダイヤモンド演算子と呼ばれ、標準入力またはコマンドライン引数で指定されたファイルから1行ずつ読み出す。

行入力演算子「<>」 - ファイルから一行読み込む - Perl入門ゼミ

"$_"に行文字列が格納されるのでそれを出力。

$ vi diamond_operator.pl
$ vi test1.txt
$ cat test1.txt
test1 line1
test1 line2
test1 line3
$ vi test2.txt
$ cat test2.txt
test2 line1
test2 line2
test2 line3
$ vi test3.txt
$ cat test3.txt
test3 line1
test3 line2
test3 line3
$ perl diamond_operator.pl test1.txt test2.txt test3.txt
test1 line1
test1 line2
test1 line3
test2 line1
test2 line2
test2 line3
test3 line1
test3 line2
test3 line3
$

それぞれ1行ずつ交互かと思ったら、test1.txtを全部読み込んだ後、test2.txt、test3.txtと続く模様。
コマンドライン引数なしだと、

$ perl diamond_operator.pl
ABC

待ち状態になるので、このように文字列を入れて、リターンで、

$ perl diamond_operator.pl
ABC
ABC

こうなる。

$ perl diamond_operator.pl
ABC
ABC
DEF
DEF
GHI
GHI
^C
$

ただ、終了できないので、Ctrl+Cで終了したけど・・・。

Sample/perl/diamond_operator/diamond_operator/src/diamond_operator at master · bg1bgst333/Sample · GitHub