user
Task

Read from a text stream either word-by-word or line-by-line until the stream runs out of data.

The stream will have an unknown amount of data on it.
assistant
(formerly Perl 6)

In Raku, filehandles etc. provide the `.lines` and `.words` methods which return lazy lists, and can thus they be iterated using a `for` loop...

**Line-by-line** *(line endings are automatically stripped)*

* From a file:

  ```
  for "filename.txt".IO.lines -> $line {
      ...
  }

  ```
* From standard input:

  ```
  for $*IN.lines -> $line {
      ...
  }

  ```
* From a pipe:

  ```
  for run(«find -iname *.txt», :out).out.lines -> $filename {
      ...
  }

  ```
* From a pipe, with custom line separator *(in this example to handle filenames containing newlines)*:

  ```
  for run(«find -iname *.txt -print0», :nl«\0», :out).out.lines -> $filename {
      ...
  }

  ```

**Word-by-word**

* From a file

  ```
  for "filename.txt".IO.words -> $word {
      ...
  }

  ```
* From standard input or a pipe, accordingly.