Search code examples
iostdinrakurakudo

How do I read from STDIN in Rakudo Perl6?


As simple as that, how can I read input from STDIN in Perl6?

I reckon there's many ways of doing it, but I'm interested in the most idiomatic Perl6 solution.


Solution

  • The standard input file descriptor in Perl6 is $*IN (in Perl5 the *STDIN typeglob had a reference to the STDIN file descriptor as *STDIN{IO}).

    One way of reading from standard input is the following:

    for lines() {
        say "Read: ", $_
    }
    

    In fact, lines() without an invocant object defaults to $*IN.lines().

    An alternative that uses a local variable is:

    for $*IN.lines() -> $line {
        say "Read: ", $line
    }
    

    Would be cool to see more alternative ways of doing it.