TEXT::PARSEWORDS(3perl)

πŸ’– Text::ParseWords β€” the Text::ParseWords(3perl) manual, but make it make sense πŸ’–

okay bestie, sit down, we're learning Text::ParseWords and honestly? it's giving "tiny module, big power". no cap.

✨ NAME ✨

Text::ParseWords β€” parse text into an array of tokens or array of arrays πŸŽ€


🌸 SYNOPSIS 🌸

use Text::ParseWords;
@lists = nested_quotewords($delim, $keep, @lines);
@words = quotewords($delim, $keep, @lines);
@words = shellwords(@lines);
@words = parse_line($delim, $keep, $line);
@words = old_shellwords(@lines); # DEPRECATED!
πŸ’€ bestie that last one says DEPRECATED! and we do NOT ignore that. it's literally written in the source. do not be the person shipping old_shellwords.

πŸ¦‹ DESCRIPTION πŸ¦‹

so nested_quotewords() and quotewords() take a delimiter (and yes, it can be a regular expression, she's flexible like that πŸ’…) plus a list of lines, and then breaks those lines up into a list of words β€” while ignoring delimiters that appear inside quotes. iconic behavior.

  • quotewords() hands you back all the tokens in one single long list. one big list, no notes.
  • nested_quotewords() hands you back a list of token lists, and each inner list matches up with a corresponding element of @lines. very organized, very demure.
  • parse_line() does the tokenizing on just one single string.

and here's the tea β˜• β€” the *quotewords() functions just call parse_line() underneath. so if you're only splitting one line, just call parse_line() directly and save yourself a whole function call. free real estate. πŸ”₯

🧚 the $keep drama 🧚

$keep controls what happens with delimiters and special characters. three moods:

  • true β€” if true, tokens are split on the specified delimiter, but all other characters (including quotes and backslashes) are kept in the tokens. nothing gets thrown away ✨
  • false β€” if $keep is false, the *quotewords() functions remove all quotes and backslashes that are not themselves backslash-escaped or inside of single quotes (i.e. quotewords() tries to interpret these characters just like the Bourne shell).
🚨 NB: these semantics are significantly different from the original version of this module shipped with Perl 5.000 through 5.004. letting you know fr fr.
  • "delimiters" β€” as an extra lil feature, $keep can also be the keyword "delimiters". that makes the functions preserve the delimiters in each string as tokens in the token lists, on top of preserving quote and backslash characters. bonus content 🎁

and shellwords()? she's written as a special case of quotewords(), and she does token parsing with whitespace as a delimiter β€” similar to most Unix shells. that's the vibe 🐚


🌈 EXAMPLES 🌈

sample program, go:

use Text::ParseWords;
@words = quotewords('\s+', 0, q{this   is "a test" of\ quotewords \"for you});
$i = 0;
foreach (@words) {
    print "$i: <$_>\n";
    $i++;
}

and it produces:

0: <this>
1: <is>
2: <a test>
3: <of quotewords>
4: <"for>
5: <you>

which is demonstrating, one by one:

  1. a simple word πŸ‘‘
  2. multiple spaces are skipped because of our $delim ✨
  3. use of quotes to include a space in a word πŸŽ€
  4. use of a backslash to include a space in a word πŸ’…
  5. use of a backslash to remove the special meaning of a double-quote 😭
  6. another simple word β€” and note the lack of effect of the backslashed double-quote 🫢

and the glow-up: replacing quotewords('\s+', 0, q{this is...}) with shellwords(q{this is...}) is literally a simpler way to accomplish the same thing. shorter, cuter, same result. periodt.


πŸ“ SEE ALSO πŸ“

  • Text::CSV β€” for parsing CSV files

πŸ’œ AUTHORS πŸ’œ

  • the original author is unknown, but presumably this evolved from shellwords.pl in Perl 4.
  • much of the code for parse_line() (including the primary regexp) came from Joerk Behrends <jbehrends@multimediaproduzenten.de>.
  • Examples section and other documentation provided by John Heidemann <johnh@ISI.EDU>.
  • Hal Pomeranz <pomeranz@netcom.com> maintained this from 1994 through 1999, and did the first CPAN release.
  • Alexandr Ciornii <alexchornyATgmail.com> maintained this from 2008 to 2015.
  • many other people have contributed, with special thanks due to Michael Schwern <schwern@envirolink.org> and Jeff Friedl <jfriedl@yahoo-inc.com>.

This library is free software; you may redistribute and/or modify it under the same terms as Perl itself. slay, that's the whole license.


okay that's the whole page, you ate that 🫢 go tokenize something. this was perl v5.44.0, dated 2026-10-17, and the page is Text::ParseWords(3perl) β€” love u, bye πŸ’–βœ¨

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms