The usual way to write perl programs that take input from files is to construct them as follows:
while ($line = <>) {
# do stuff with $line
}
If filenames are given on the command line, perl will automatically open them one by one, feeding the lines to your script. If no filenames are given, it will read from standard input.
But if you write your script this way, you won't be able to give it fruits directly on the command line, they will have to be in a file or standard input.
To handle multiple fruit on the same line, your code can do:
while (my $line = <>) {
chomp $line;
foreach my $fruit ( split ' ', $line ) {
# do something with $fruit
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…