diff options
author | Shishir Gundavaram <shishir@ruby.ora.com> | 1997-04-02 09:53:42 +1200 |
---|---|---|
committer | Chip Salzenberg <chip@atlantic.net> | 1997-04-17 00:00:00 +0000 |
commit | 936c88379edb0f52f0303aeb6882907ebed01976 (patch) | |
tree | 50b5dbc5240c0c904569ef563ac6e629d1ae1084 | |
parent | fabdde38538cf589b606a8865ea32704f8cd60cd (diff) | |
download | perl-936c88379edb0f52f0303aeb6882907ebed01976.tar.gz |
Revise quotewords()
-rw-r--r-- | lib/Text/ParseWords.pm | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/lib/Text/ParseWords.pm b/lib/Text/ParseWords.pm index bd95b4f204..62da1d273f 100644 --- a/lib/Text/ParseWords.pm +++ b/lib/Text/ParseWords.pm @@ -37,7 +37,6 @@ This version differs from the original in that it will _NOT_ default to using $_ if no arguments are given. I personally find the old behavior to be a mis-feature. - "ewords() works by simply jamming all of @lines into a single string in $_ and then pulling off words a bit at a time until $_ is exhausted. @@ -90,43 +89,49 @@ sub quotewords { # at a time behavior was necessary if the delimiter was going to be a # regexp (love to hear it if you can figure out a better way). - local($delim, $keep, @lines) = @_; - local(@words,$snippet,$field,$_); + my ($delim, $keep, @lines) = @_; + my (@words, $snippet, $field); + + local $_ = join ('', @lines); - $_ = join('', @lines); - while (length($_)) { + while (length) { $field = ''; + for (;;) { $snippet = ''; - if (s/^"(([^"\\]|\\.)*)"//) { + + if (s/^"([^"\\]*(\\.[^"\\]*)*)"//) { $snippet = $1; - $snippet = "\"$snippet\"" if ($keep); + $snippet = qq|"$snippet"| if $keep; } - elsif (s/^'(([^'\\]|\\.)*)'//) { + elsif (s/^'([^'\\]*(\\.[^'\\]*)*)'//) { $snippet = $1; - $snippet = "'$snippet'" if ($keep); + $snippet = "'$snippet'" if $keep; } elsif (/^["']/) { - croak "Unmatched quote"; + croak 'Unmatched quote'; + } + elsif (s/^\\(.)//) { + $snippet = $1; + $snippet = "\\$snippet" if $keep; } - elsif (s/^\\(.)//) { - $snippet = $1; - $snippet = "\\$snippet" if ($keep); - } - elsif (!length($_) || s/^$delim//) { - last; + elsif (!length || s/^$delim//) { + last; } else { - while ($_ ne '' && !(/^$delim/ || /^['"\\]/)) { - $snippet .= substr($_, 0, 1); - substr($_, 0, 1) = ''; - } + while (length && !(/^$delim/ || /^['"\\]/)) { + $snippet .= substr ($_, 0, 1); + substr($_, 0, 1) = ''; + } } + $field .= $snippet; } - push(@words, $field); + + push @words, $field; } - @words; + + return @words; } |