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\\'
|