Re: [perl #38904] Text::ParseWords doesn't always handle backslashes correctly
authorAlexey Toptygin <alexeyt@freeshell.org>
Thu, 20 Apr 2006 15:42:20 +0000 (15:42 +0000)
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>
Fri, 21 Apr 2006 13:01:09 +0000 (13:01 +0000)
Message-ID: <Pine.NEB.4.62.0604201539380.20332@otaku.freeshell.org>

p4raw-id: //depot/perl@27932

lib/Text/ParseWords.pm
lib/Text/ParseWords.t

index 2f6812ade800be967faa8c0e1c2aeaaa3652b82f..1411986cd9a7e4f305925a13ff88378fb603b96c 100644 (file)
@@ -1,7 +1,7 @@
 package Text::ParseWords;
 
 use vars qw($VERSION @ISA @EXPORT $PERL_SINGLE_QUOTE);
-$VERSION = "3.24";
+$VERSION = "3.25";
 
 require 5.000;
 
@@ -12,9 +12,17 @@ use Exporter;
 
 
 sub shellwords {
-    my(@lines) = @_;
-    $lines[$#lines] =~ s/\s+$//;
-    return(quotewords('\s+', 0, @lines));
+    my (@lines) = @_;
+    my @allwords;
+
+    foreach my $line (@lines) {
+       $line =~ s/^\s+//;
+       my @words = parse_line('\s+', 0, $line);
+       pop @words if (@words and !defined $words[-1]);
+       return() unless (@words || !length($line));
+       push(@allwords, @words);
+    }
+    return(@allwords);
 }
 
 
index 74c27335804e8c57b0ae099ddb105d8f70420de9..00e2a401c72e008ad6dc7e7bd9b4e661b6f4c69c 100755 (executable)
@@ -8,7 +8,7 @@ BEGIN {
 use warnings;
 use Text::ParseWords;
 
-print "1..22\n";
+print "1..23\n";
 
 @words = shellwords(qq(foo "bar quiz" zoo));
 print "not " if $words[0] ne 'foo';
@@ -132,3 +132,9 @@ $string = qq{"missing quote};
 $result = join('|', shellwords($string));
 print "not " unless $result eq "";
 print "ok 22\n";
+
+# make sure shellwords strips out leading whitespace and trailng undefs
+# from parse_line, so it's behavior is more like /bin/sh
+$result = join('|', shellwords(" aa \\  \\ bb ", " \\  ", "cc dd ee\\ "));
+print "not " unless $result eq "aa| | bb| |cc|dd|ee ";
+print "ok 23\n";