From: Alexey Toptygin Date: Thu, 20 Apr 2006 15:42:20 +0000 (+0000) Subject: Re: [perl #38904] Text::ParseWords doesn't always handle backslashes correctly X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d5c14ab28afc297a5ad2b7ac52ffd5c0cc1941a7;p=p5sagit%2Fp5-mst-13.2.git Re: [perl #38904] Text::ParseWords doesn't always handle backslashes correctly Message-ID: p4raw-id: //depot/perl@27932 --- diff --git a/lib/Text/ParseWords.pm b/lib/Text/ParseWords.pm index 2f6812a..1411986 100644 --- a/lib/Text/ParseWords.pm +++ b/lib/Text/ParseWords.pm @@ -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); } diff --git a/lib/Text/ParseWords.t b/lib/Text/ParseWords.t index 74c2733..00e2a40 100755 --- a/lib/Text/ParseWords.t +++ b/lib/Text/ParseWords.t @@ -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";