如何在 Perl 中从控制台读取多行?
我用过@a = <STDIN>;但我无法摆脱这种说法。每次我点击进入它都会进入新行。我已阅读以ctrl+d结束输入,但它似乎不起作用。
也许更好的主意是某种循环:
use strict;
use warnings;
my @a;
for(;;) {
my $input = <STDIN>;
last if not defined $input;
chomp $input;
push @a, $input;
}
这将在您键入 Unix <EOF>(通常Ctrl-D默认设置为)时结束。
你可以使用while循环,
my @a;
while (<STDIN>) {
/\S/ or last; # last line if empty
push @a, $_;
}
print @a;
好像你在 Windows 上。在 Windows 上,您必须在空行上按Control-z,然后按 Enter。