Documentation for routine
words assembled from the following types:
method words(Int() )
Coerces the invocant to Str, and returns a list of words that make up the string (and if
$limit is supplied, only the first
say 'The quick brown fox'.words.join('|'); # OUTPUT: «The|quick|brown|fox␤»say 'The quick brown fox'.words(2).join('|'); # OUTPUT: «The|quick␤»
Only whitespace counts as word boundaries
say "isn't, can't".words.join('|'); # OUTPUT: «isn't,|can't␤»
multi sub words(Str , = Inf --> Positional)multi method words(Str : = Inf --> Positional)
Returns a list of non-whitespace bits, i.e. the same as a call to
$input.comb( / \S+ /, $limit ) would.
say "a\nb\n".words.perl; # OUTPUT: «("a", "b").Seq␤»say "hello world".words.perl; # OUTPUT: «("hello", "world").Seq␤»say "foo:bar".words.perl; # OUTPUT: «("foo:bar",).Seq␤»say "foo:bar\tbaz".words.perl; # OUTPUT: «("foo:bar", "baz").Seq␤»
multi sub words(IO::Handle = , = Inf, : --> Seq)multi method words(IO::Handle: = Inf, : --> Seq)
Str.words, separates the handle's stream on contiguous chunks of whitespace (as defined by Unicode) and returns a Seq of the resultant "words." Takes an optional
$limit argument that can be a non-negative Int,
Inf, or Whatever (which is interpreted to mean
Inf), to indicate only up-to
$limit words must be returned. If Bool
:$close named argument is set to
True, will automatically close the handle when the returned Seq is exhausted. 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.
my := bag .words;say "Most common words: ", .sort(-*.value).head: 5;
NOTE: implementations may read more data than necessary when a call to
.words is made. That is,
$handle.words(2) may read more data than two "words" worth of data and subsequent calls to read methods might not read from the place right after the two fetched words. After a call to
.words, the file position should be treated as undefined.
method words(IO::CatHandle: = Inf, : --> Seq)
IO::Handle.words (including the caveat about more data read than needed to make some number of words). Note that a boundary between source handles is considered to be word boundary.
(my = 'foo'.IO).spurt: 'foo bar';(my = 'bar'.IO).spurt: 'meow';IO::CatHandle.new(, ).words.perl.say;# OUTPUT: «("foo", "bar", "meow").Seq␤»
method words(IO::Path: : = True, : = 'utf8', : = ["\x0A", "\r\n"], |c --> Seq)
Opens the invocant and returns its words.
The behaviour is equivalent to opening the file specified by the invocant, forwarding the
:$nl-in arguments to
IO::Handle.open, then calling
IO::Handle.words on that handle, forwarding any of the remaining arguments to that method, and returning the resultant Seq.
NOTE: words are lazily read. The handle used under the hood is not closed until the returned Seq is fully reified, and this could lead to leaking open file handles. It is possible to avoid leaking open file handles using the
$limit argument to cut down the
Seq of words to be generated.
my := bag 'my-file.txt'.IO.words;say "Most common words: ", .sort(-*.value).head: 5;
method words(Supply: --> Supply)
Creates a supply that will emit the characters coming in word for word from a supply that's usually created by some asynchronous I/O operation.
my = Supply.from-list("Hello Word!".comb);my = .words;.tap(); # OUTPUT: «Hello␤Word!␤»