Perl script does not print multiple times

前端 未结 2 504
慢半拍i
慢半拍i 2021-01-29 14:05

I\'ve got this Perl script:

#!/usr/bin/perl
use strict;
use warnings;

print , \"\\n\";
print , \"\\n\";
print , \"\\n\";
         


        
2条回答
  •  没有蜡笔的小新
    2021-01-29 14:41

    print , "\n";
    

    (i.e. readline(STDIN)) "in list context reads until end-of-file is reached and returns a list of lines."

    In your program, the first print therefore prints all the lines read from STDIN.

    By definition, there are no more lines coming from STDIN, because in list context read everything there was to read.

    If you want to read five consecutive lines from STDIN and print them, you need:

    print scalar ;
    print scalar ;
    print scalar ;
    print scalar ;
    print scalar ;
    

    By definition, a line ends with a newline. If you do not remove it, there is no need to tack on another.

    It's more intuitive to me that the value represented by is held somewhere in memory once it's piped into the script

    Your program contains no instructions to store the input read from STDIN. All it does is to read everything available on STDIN, print all of it, and discard it.

    If you wanted to store everything you read from STDIN, you have to explicitly do so. That's how computer programs work: They do exactly as they are told. Imagine what a disaster it would be if computer programs did different things depending on the intuition of the person writing or running them.

    Of course, the data coming from STDIN might be unbounded. In that case, storing all of it somewhere is not going to be practical.

提交回复
热议问题