1Text::ParseWords(3) User Contributed Perl Documentation Text::ParseWords(3)
2
3
4
6 Text::ParseWords - parse text into an array of tokens or array of
7 arrays
8
10 use Text::ParseWords;
11 @lists = nested_quotewords($delim, $keep, @lines);
12 @words = quotewords($delim, $keep, @lines);
13 @words = shellwords(@lines);
14 @words = parse_line($delim, $keep, $line);
15 @words = old_shellwords(@lines); # DEPRECATED!
16
18 The nested_quotewords() and quotewords() functions accept a delimiter
19 (which can be a regular expression) and a list of lines and then breaks
20 those lines up into a list of words ignoring delimiters that appear
21 inside quotes. quotewords() returns all of the tokens in a single long
22 list, while nested_quotewords() returns a list of token lists
23 corresponding to the elements of @lines. parse_line() does tokenizing
24 on a single string. The *quotewords() functions simply call
25 parse_line(), so if you're only splitting one line you can call
26 parse_line() directly and save a function call.
27
28 The $keep controls what happens with delimters and special characters:
29
30 true
31 If true, then the tokens are split on the specified delimiter, but
32 all other characters (including quotes and backslashes) are kept in
33 the tokens.
34
35 false
36 If $keep is false then the *quotewords() functions remove all
37 quotes and backslashes that are not themselves backslash-escaped or
38 inside of single quotes (i.e., quotewords() tries to interpret
39 these characters just like the Bourne shell). NB: these semantics
40 are significantly different from the original version of this
41 module shipped with Perl 5.000 through 5.004.
42
43 "delimiters"
44 As an additional feature, $keep may be the keyword "delimiters"
45 which causes the functions to preserve the delimiters in each
46 string as tokens in the token lists, in addition to preserving
47 quote and backslash characters.
48
49 shellwords() is written as a special case of quotewords(), and it does
50 token parsing with whitespace as a delimiter-- similar to most Unix
51 shells.
52
54 The sample program:
55
56 use Text::ParseWords;
57 @words = quotewords('\s+', 0, q{this is "a test" of\ quotewords \"for you});
58 $i = 0;
59 foreach (@words) {
60 print "$i: <$_>\n";
61 $i++;
62 }
63
64 produces:
65
66 0: <this>
67 1: <is>
68 2: <a test>
69 3: <of quotewords>
70 4: <"for>
71 5: <you>
72
73 demonstrating:
74
75 0 a simple word
76
77 1 multiple spaces are skipped because of our $delim
78
79 2 use of quotes to include a space in a word
80
81 3 use of a backslash to include a space in a word
82
83 4 use of a backslash to remove the special meaning of a double-quote
84
85 5 another simple word (note the lack of effect of the backslashed
86 double-quote)
87
88 Replacing "quotewords('\s+', 0, q{this is...})" with
89 "shellwords(q{this is...})" is a simpler way to accomplish the same
90 thing.
91
93 Text::CSV - for parsing CSV files
94
96 The original author is unknown, but presumably this evolved from
97 "shellwords.pl" in Perl 4.
98
99 Much of the code for parse_line() (including the primary regexp) came
100 from Joerk Behrends <jbehrends@multimediaproduzenten.de>.
101
102 Examples section and other documentation provided by John Heidemann
103 <johnh@ISI.EDU>.
104
105 Hal Pomeranz <pomeranz@netcom.com> maintained this from 1994 through
106 1999, and did the first CPAN release.
107
108 Alexandr Ciornii <alexchornyATgmail.com> maintained this from 2008 to
109 2015.
110
111 Many other people have contributed, with special thanks due to Michael
112 Schwern <schwern@envirolink.org> and Jeff Friedl
113 <jfriedl@yahoo-inc.com>.
114
116 This library is free software; you may redistribute and/or modify it
117 under the same terms as Perl itself.
118
119
120
121perl v5.36.0 2023-01-20 Text::ParseWords(3)