Update: I'm just saying that benchmarks for performing an operation on $_ won't necessarily carry over to benchmarks for performing an operation on a lexical. In fact, with a lexical, the two-regex method outshines the others even more:
shows "two-s" gaining more than the others when working on lexicals.use Benchmark 'cmpthese'; use Regexp::Common 'whitespace'; my $f = ' this is a string with spaces to remove '; cmpthese(-3, { 'Regexp-Common' => sub { my $g=$f; $g=~s/$RE{ws}{crop}//g; }, 'two-s///' => sub { my $g=$f; $g=~s/^\s+//; $g=~s/\s+$//; }, 'one-s///' => sub { my $g=$f; $g=~s/^\s+|\s+$//g; }, 's-capture' => sub { my $g=$f; $g=~s/^\s*(.*?)\s*$/$1/; }, 's-capture2' => sub { my $g=$f; $g=~s/^\s*(\S+(?:\s+\S+)?)?\s*$/$ +1/; }, 'm-capture' => sub { my $g=$f; ($g) = $g=~/(\S+(?:\s+\S+)?)/; + }, });
In reply to Re: Answer: How do I remove whitespace at the beginning or end of my string?
by ysth
in thread How do I remove whitespace at the beginning or end of my string?
by vroom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |