Is Perl 6 eof giving up too fast?

In Perl 5, I can check if standard input is open and read one line from it.

for (;;) {
    last if eof(STDIN);
    print "Got line " . readline(STDIN);
    }

When I run it, enter the input line, it reads this line and does its job before moving on. The program doesn’t care if there are long pauses:

$ perl print-stdin.pl
this
Got line this
is
Got line is
a
Got line a
line
Got line line

If I do the same in Perl 6 (Rakudo 2017.07), the program stops immediately:

use v6;
loop {
    last if $*IN.eof;
    put "Got line " ~ $*IN.get;
    }

I'm really after Supply, which can give me one line of input as it arrives (perhaps from a program that slowly displays a line with long pauses), but I fully supported this simple problem. I did not find a built-in way to do this (which is a bit surprising for such a general task).

+4
source share
1 answer

, . , , , .eof. , , .get , Nil. , Got line .

.lines

for $*IN.lines { put "Got line $_" }

.get, , .

loop {
  with $*IN.get {
    put "Got line $_"
  } else {
    last
  }
}

:

$*IN.lines.Supply
react {
  start whenever $*IN.lines.Supply {
    put "Got line $_";
    LAST done; # finish the outer 「react」 block when this closes
  }
  whenever Supply.interval(1) {
    put DateTime.now.hh-mm-ss
  }
}
22:46:33
22:46:34
a
Got line a
22:46:35
22:46:36
b
Got line b
22:46:37
22:46:38
c
Got line c
22:46:39
22:46:40
d
Got line d
22:46:41
22:46:42
^D               # represents Ctrl+D

start, Supply.interval(1) .


- , :

my \in-supply = supply {

  # 「await start」 needed so this won't block other things on this thread.

  await start loop {
    with $*IN.get { # defined (so still open)

      emit $_

    } else {        # not defined (closed)

      done;         # stop the Supply

      # last        # stop this loop (never reached)

    }
  }
}

react {
  whenever in-supply {
    put "Got line $_";
    LAST done # finish the outer 「react」 block when this closes
  }
  whenever Supply.interval(1) {
    put DateTime.now.hh-mm-ss
  }
}
+2

Source: https://habr.com/ru/post/1688494/


All Articles