package Text::ParseWords;
-use vars qw($VERSION @ISA @EXPORT);
-$VERSION = "3.0";
+use vars qw($VERSION @ISA @EXPORT $PERL_SINGLE_QUOTE);
+$VERSION = "3.21";
require 5.000;
sub parse_line {
+ # We will be testing undef strings
+ no warnings;
+ use re 'taint'; # if it's tainted, leave it as such
+
my($delimiter, $keep, $line) = @_;
my($quote, $quoted, $unquoted, $delim, $word, @pieces);
while (length($line)) {
- ($quote, $quoted, $unquoted, $delim) =
+
+ ($quote, $quoted, undef, $unquoted, $delim, undef) =
$line =~ m/^(["']) # a $quote
- ((?:\\.|[^\1\\])*?) # and $quoted text
- \1 # followed by the same quote
+ ((?:\\.|(?!\1)[^\\])*) # and $quoted text
+ \1 # followed by the same quote
+ ([\000-\377]*) # and the rest
| # --OR--
^((?:\\.|[^\\"'])*?) # an $unquoted text
- (\Z(?!\n)|$delimiter|(?!^)(?=["']))
+ (\Z(?!\n)|(?-x:$delimiter)|(?!^)(?=["']))
# plus EOL, delimiter, or quote
- /x; # extended layout
+ ([\000-\377]*) # the rest
+ /x; # extended layout
+ return() unless( $quote || length($unquoted) || length($delim));
- return() unless(length($&));
- $line = $';
+ $line = $+;
if ($keep) {
$quoted = "$quote$quoted$quote";
}
else {
$unquoted =~ s/\\(.)/$1/g;
- $quoted =~ s/\\(.)/$1/g if ($quote eq '"');
+ if (defined $quote) {
+ $quoted =~ s/\\(.)/$1/g if ($quote eq '"');
+ $quoted =~ s/\\([\\'])/$1/g if ( $PERL_SINGLE_QUOTE && $quote eq "'");
+ }
}
- $word .= ($quote) ? $quoted : $unquoted;
+ $word .= defined $quote ? $quoted : $unquoted;
if (length($delim)) {
push(@pieces, $word);
=over 4
=item 0
+
a simple word
=item 1
+
multiple spaces are skipped because of our $delim
=item 2
+
use of quotes to include a space in a word
=item 3
+
use of a backslash to include a space in a word
=item 4
+
use of a backslash to remove the special meaning of a double-quote
=item 5
+
another simple word (note the lack of effect of the
backslashed double-quote)