perl6 what is the best way to enter multiple multi-line here-docs from $*IN

63 views Asked by At

I need to enter multiple HERE-DOCs. My codes have 2 while loops. The inner loop is used to get the multi-line here-doc. But after I enter Control-D to complete one here-doc, then the inner while loop does not run again and the outer loop runs infinitely. What is the best way to fix it?

while True {
    my $y = ""; 
    my $x = 32;
    while $x=$*IN.getc { 
        $y = $y ~ $x; 
    }
    say "==========";
    say "[$y]"; sleep 2;
}

Why would $x continue to be EOF even after I reset $x to 32? Why would EOF be stuck in $*IN ? Thank you for your help.

1

There are 1 answers

1
smls On BEST ANSWER

When you press Ctrl+D, the terminal closes the stdin stream on its end, and then when Perl 6 tries to continue to read from it, it always gets EOF.

In your program, this causes the inner while loop to end. You then set $x to 32 again, but at the next start of the inner while loop, the $x = $*IN.getc immediately sets it to Nil again, because the stream is still closed.