Perl script does not print multiple times

前端 未结 2 476
慢半拍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:40

    If you want to loop printing your text an user defined number of times, you can use a do while loop like this:

    #!/usr/bin/env perl
    #Program: stdin.pl
    use strict;
    use warnings;
    use feature 'say'; # like print but automatic ending newline.
    
    my $i = 1; # Our incrementer.
    my $input; # Our text from standard input that we want to loop.
    my $total; # How many times we want to print the text on a loop.
    
    say "Please provide the desired text of your standard input.";
    chomp($input = <STDIN>); # chomp removes a newline from the standard input. 
    say "Please provide the amount of times that you want to print your standard input to the screen.";
    chomp($total = <STDIN>);
    say ">>> I'm in your loop! <<<"; 
    do # A do while loop iterates at least once and up to the total number of iterations provided by user. 
    {
    say "Iteration $i of text: $input";
    $i++;
    }while($i <= $total);
    say ">>> I'm out of your loop! <<<";
    

    This is what I get when I execute the code with "Hello", and "8" from standard input:

    > perl .\stdin.pl
    Please provide the desired text of your standard input.
    Hello!
    Please provide the amount of times that you want to print your standard input to the screen.
    8
    >>> I'm in your loop! <<<
    Iteration 1 of text: Hello!
    Iteration 2 of text: Hello!
    Iteration 3 of text: Hello!
    Iteration 4 of text: Hello!
    Iteration 5 of text: Hello!
    Iteration 6 of text: Hello!
    Iteration 7 of text: Hello!
    Iteration 8 of text: Hello!
    >>> I'm out of your loop! <<<
    
    0 讨论(0)
  • 2021-01-29 14:41
    print <STDIN>, "\n";
    

    <STDIN> (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 <STDIN> 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 <STDIN>;
    print scalar <STDIN>;
    print scalar <STDIN>;
    print scalar <STDIN>;
    print scalar <STDIN>;
    

    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 <STDIN> 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.

    0 讨论(0)
提交回复
热议问题