Combined from primary sources listed below.
See primary docmentation in context for method lines.
See primary docmentation in context for routine lines.
sub lines( $what = $*ARGFILES, |c)
multi method lines( IO::Handle:D: $limit, :$close )
multi method lines( IO::Handle:D: :$close )
The sub form, which takes $*ARGFILES by default, will apply the lines method to the object that's the first argument, and pass it the rest of the arguments.
The method will return a Seq each element of which is a line from the handle (that is chunks delineated by .nl-in). If the handle's .chomp attribute is set to True, then characters specified by .nl-in will be stripped from each line.
Reads up to $limit lines, where $limit can be a non-negative Int, Inf, or Whatever (which is interpreted to mean Inf). If :$close is set to True, will close the handle when the file ends or $limit is reached. Subroutine form defaults to $*ARGFILES, if no handle is provided.
Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.
NOTE: the lines are read lazily, so ensure the returned Seq is either fully reified or is no longer needed when you close the handle or attempt to use any other method that changes the file position.
say "The file contains ",
'50GB-file'.IO.open.lines.grep(*.contains: 'Raku').elems,
" lines that mention Raku";
# OUTPUT: «The file contains 72 lines that mention Raku»
You can use lines in /proc/* files (from the 6.d version):
say lines( "/proc/$*PID/statm".IO ); # OUTPUT: «(58455 31863 8304 2 0 29705 0)»
See primary docmentation in context for method lines.
method lines(IO::CatHandle:D: $limit = Inf, :$close --> Seq:D)
Same as IO::Handle.lines. Note that a boundary between source handles is considered to be a newline break.
(my $f1 = 'foo'.IO).spurt: "foo\nbar";
(my $f2 = 'bar'.IO).spurt: 'meow';
IO::CatHandle.new($f1, $f2).lines.raku.say;
# OUTPUT: «("foo", "bar", "meow").Seq»
Note: if :$close is False, fully-consumed handles are still going to be closed.
See primary docmentation in context for method lines.
method lines(IO::Path:D: :$chomp = True, :$enc = 'utf8', :$nl-in = ["\x0A", "\r\n"], |c --> Seq:D)
Opens the invocant and returns its lines.
The behavior is equivalent to opening the file specified by the invocant, forwarding the :$chomp, :$enc, and :$nl-in arguments to IO::Handle.open, then calling IO::Handle.lines on that handle, forwarding any of the remaining arguments to that method, and returning the resultant Seq.
NOTE: the lines are ready lazily and the handle used under the hood won't get closed until the returned Seq is fully reified, so ensure it is, or you'll be leaking open filehandles. (TIP: use the $limit argument)
say "The file contains ",
'50GB-file'.IO.lines.grep(*.contains: 'Raku').elems,
" lines that mention Raku";
# OUTPUT: «The file contains 72 lines that mention Raku»