標準入力から1行読み込んでくれる
ちなみに、STDIN は standard inputの略です。
どんな、感じ?
1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 my $a = <STDIN>; 7 my $b = <STDIN>; 8 9 print $a; 10 print $b;
$ cat test.txt Hello World $ ./test.pl < test.txt Hello World $
こんな、感じです。
1行目を読み込んで、$aに代入して、
2行めを読み込んで、$bに代入して、
printで読み込んだのを出力。
何回も、呼び出したら、どうなるのか?
$a = <STDIN>; $b = <STDIN>; $c = <STDIN>; $d = <STDIN>; $e = <STDIN>; . .
てな感じで、標準入力から読み込むことがないのに
実は、Perl殿は
ということで、この場合、
$c = undef; $d = undef; $e = undef; . .
となる。
このことを利用して
次のような書き方ができる
while (<STDIN>) { ..... }
標準入力から読み込んでいる間、