Documentation for routine lines
assembled from the following pages:
Class: Supply §
From Supply
(Supply) method lines §
method lines(Supply: : = True --> Supply)
Creates a supply that will emit the characters coming in line by line from a supply that's usually created by some asynchronous I/O operation. The optional :chomp
parameter indicates whether to remove line separators: the default is True
.
Class: IO::CatHandle §
From IO::CatHandle
(IO::CatHandle) method lines §
Defined as:
method lines(IO::CatHandle: = Inf, : --> Seq)
Same as IO::Handle.lines
. Note that a boundary between source handles is considered to be a newline break.
(my = 'foo'.IO).spurt: "foo\nbar";(my = 'bar'.IO).spurt: 'meow';IO::CatHandle.new(, ).lines.raku.say;# OUTPUT: «("foo", "bar", "meow").Seq»
Note: if :$close
is False
, fully-consumed handles are still going to be closed.
Class: IO::Handle §
From IO::Handle
(IO::Handle) routine lines §
Defined as:
sub lines( = , |c)multi method lines( IO::Handle: , : )multi method lines( IO::Handle: : )
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)»
Class: IO::Path §
From IO::Path
(IO::Path) method lines §
Defined as:
method lines(IO::Path: : = True, : = 'utf8', : = ["\x0A", "\r\n"], |c --> Seq)
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»
Class: IO::Socket::INET §
From IO::Socket::INET
(IO::Socket::INET) method lines §
method lines()
Returns a lazy list of lines read from the socket.
Class: Str §
From Str
(Str) routine lines §
Defined as:
multi method lines(Str: , : = True)multi method lines(Str: : = True)
Returns a list of lines. By default, it chomps line endings the same as a call to $input.comb( / ^^ \N* /, $limit )
would. To keep line endings, set the optional named parameter $chomp
to False
.
Examples:
say lines("a\nb").raku; # OUTPUT: «("a", "b").Seq» say lines("a\nb").elems; # OUTPUT: «2» say "a\nb".lines.elems; # OUTPUT: «2» say "a\n".lines.elems; # OUTPUT: «1» # Keep line endings say lines(:!chomp, "a\nb").raku; # OUTPUT: «("a\n", "b").Seq» say "a\n".lines(:!chomp).elems; # OUTPUT: «1»
You can limit the number of lines returned by setting the $limit
variable to a non-zero, non-Infinity
value:
say <not there yet>.join("\n").lines( 2 ); # OUTPUT: «(not there)»
DEPRECATED as of 6.d
language, the :count
argument was used to return the total number of lines:
say <not there yet>.join("\n").lines( :count ); # OUTPUT: «3»
Use elems call on the returned Seq instead:
say <not there yet>.join("\n").lines.elems; # OUTPUT: «3»
Class: Cool §
From Cool
(Cool) routine lines §
Defined as:
sub lines(Str(Cool))method lines()
Coerces the invocant (and in sub form, the argument) to Str, decomposes it into lines (with the newline characters stripped), and returns the list of lines.
say lines("a\nb\n").join('|'); # OUTPUT: «a|b» say "some\nmore\nlines".lines.elems; # OUTPUT: «3»
This method can be used as part of an IO::Path
to process a file line-by-line, since IO::Path
objects inherit from Cool
, e.g.:
for 'huge-csv'.IO.lines -> # or if you'll be processing later my = 'huge-csv'.IO.lines;
Without any arguments, sub lines
operates on $*ARGFILES
.
To modify values in place use is copy
to force a writable container.
for .lines -> is copy