1 package Text::ParseWords;
7 *AUTOLOAD = \&AutoLoader::AUTOLOAD;
11 @EXPORT = qw(shellwords quotewords);
12 @EXPORT_OK = qw(old_shellwords);
16 Text::ParseWords - parse text into an array of tokens
21 @words = "ewords($delim, $keep, @lines);
22 @words = &shellwords(@lines);
23 @words = &old_shellwords(@lines);
27 "ewords() accepts a delimiter (which can be a regular expression)
28 and a list of lines and then breaks those lines up into a list of
29 words ignoring delimiters that appear inside quotes.
31 The $keep argument is a boolean flag. If true, the quotes are kept
32 with each word, otherwise quotes are stripped in the splitting process.
33 $keep also defines whether unprotected backslashes are retained.
35 A &shellwords() replacement is included to demonstrate the new package.
36 This version differs from the original in that it will _NOT_ default
37 to using $_ if no arguments are given. I personally find the old behavior
41 "ewords() works by simply jamming all of @lines into a single
42 string in $_ and then pulling off words a bit at a time until $_
47 Hal Pomeranz (pomeranz@netcom.com), 23 March 1994
49 Basically an update and generalization of the old shellwords.pl.
50 Much code shamelessly stolen from the old version (author unknown).
59 $lines[$#lines] =~ s/\s+$//;
60 "ewords('\s+', 0, @lines);
67 # The inner "for" loop builds up each word (or $field) one $snippet
68 # at a time. A $snippet is a quoted string, a backslashed character,
69 # or an unquoted string. We fall out of the "for" loop when we reach
70 # the end of $_ or when we hit a delimiter. Falling out of the "for"
71 # loop, we push the $field we've been building up onto the list of
72 # @words we'll be returning, and then loop back and pull another word
75 # The first two cases inside the "for" loop deal with quoted strings.
76 # The first case matches a double quoted string, removes it from $_,
77 # and assigns the double quoted string to $snippet in the body of the
78 # conditional. The second case handles single quoted strings. In
79 # the third case we've found a quote at the current beginning of $_,
80 # but it didn't match the quoted string regexps in the first two cases,
81 # so it must be an unbalanced quote and we croak with an error (which can
82 # be caught by eval()).
84 # The next case handles backslashed characters, and the next case is the
85 # exit case on reaching the end of the string or finding a delimiter.
87 # Otherwise, we've found an unquoted thing and we pull of characters one
88 # at a time until we reach something that could start another $snippet--
89 # a quote of some sort, a backslash, or the delimiter. This one character
90 # at a time behavior was necessary if the delimiter was going to be a
91 # regexp (love to hear it if you can figure out a better way).
93 local($delim, $keep, @lines) = @_;
94 local(@words,$snippet,$field,$_);
96 $_ = join('', @lines);
101 if (s/^"(([^"\\]|\\.)*)"//) {
103 $snippet = "\"$snippet\"" if ($keep);
105 elsif (s/^'(([^'\\]|\\.)*)'//) {
107 $snippet = "'$snippet'" if ($keep);
110 croak "Unmatched quote";
114 $snippet = "\\$snippet" if ($keep);
116 elsif (!length($_) || s/^$delim//) {
120 while ($_ ne '' && !(/^$delim/ || /^['"\\]/)) {
121 $snippet .= substr($_, 0, 1);
122 substr($_, 0, 1) = '';
127 push(@words, $field);
137 # @words = old_shellwords($line);
139 # @words = old_shellwords(@lines);
141 local($_) = join('', @_);
142 my(@words,$snippet,$field);
148 if (s/^"(([^"\\]|\\.)*)"//) {
149 ($snippet = $1) =~ s#\\(.)#$1#g;
152 croak "Unmatched double quote: $_";
154 elsif (s/^'(([^'\\]|\\.)*)'//) {
155 ($snippet = $1) =~ s#\\(.)#$1#g;
158 croak "Unmatched single quote: $_";
163 elsif (s/^([^\s\\'"]+)//) {
172 push(@words, $field);