in reply to Re: Re: Parsing strings into @ARGV
in thread Parsing strings into @ARGV

Your solution fails on 't\\\ ', yielding 't\\\', but I expect 't\\\ '.

$ echo t\\\ | cat -e t\ $

Abigail

Replies are listed 'Best First'.
Re: Re: Parsing strings into @ARGV
by mirod (Canon) on Apr 23, 2004 at 17:39 UTC

    Let's try again, trying to keep the regexp anchored at the end:

    s{(\\*)(\s+)$}{ if( $1) { if( length( $1) % 2) { $1 . substr( $2, 0, 1) } else { $1 } } else { '' } }e;

    A bit ugly, but I would think it works: keep the first whitespace in the final sequence if it comes after an odd number of \

    s{^((\\.|.)*?)\s*$}{defined $1 ? $1 : ''}e; works too, but I would suspect it is slower (no, I don't have a benchmark to prove it).

    The test again:

    #!/usr/bin/perl -w use strict; use Test::More qw(no_plan); while( <DATA>) { my( $input, $expected)= m{^'(.*?)'\s*=>\s*'(.*?)'}; #(my $trimmed= $input)=~ s{^((\\.|.)*?)\s*$}{defined $1 ? $1 : ''} +e; (my $trimmed= $input)=~ s{(\\*)(\s+)$}{ if( $1) { if( length( $1) % 2) { + $1 . substr( $2, 0, 1) } else { $ +1 } } else { '' } }e; is( $trimmed, $expected, "'$input'"); } __DATA__ '' => '' 't' => 't' 't ' => 't' 't\ ' => 't\ ' 't\ ' => 't\ ' 't\ \ ' => 't\ \ ' 't\\\ ' => 't\\\ ' 't\\ ' => 't\\'