Count the number of words in a text.
Before solving the task, let us assume that by words we mean here a sequence of alphanumeric characters, including the underscore symbol.
Here is the solution:
my $text = prompt('Text> ');
Try it on a few test inputs:
Text> Hello, World;
The program uses regexes for extracting words using the
\w character class and the combstring method that returns a sequence of the words:
+ quantifier allows a repetition of
\w, so it matches the whole word.
Alternatively, a more traditional match with a regex may be used:
$text ~~ m:g/(\w+)/;
Parentheses in the regex capture the word, and the
:g adverb applies it a few times until all the words are found. The
$/ variable (called the match object) keeps all the matched substrings, and the
elems method returns the number of elements in it.