in reply to Weird Regex in CGI::Cookie
yes, it is broken. the reason is that the pattern is only matching the leading space and nothing else -- ie:
which gives matched ' '; retained ''; str is 'blee blah '$_ = ' blee blah '; s/\s*(.*?)\s*/$1/; print "matched '$&'; retained '$1'; str is '$_'\n";
to strip whitespace, the minimal match needs to be forcibly anchored to match to the end of the string, ie: s/^\s*(.+?)\s*$/$1/, which now successfully strips leading & training whitespace.
however this is a poor way to do it. it is clearer and faster to use 2 regexps, one to strip leading \s, the other the trailing \s; ie:
s/^\s+//; s/\s+$//;
using 2 regexps is also quite a bit faster than the single regexp too:
[matt@blade8 matt]$ perl -MBenchmark -e '@list1 = @list2 = map { " "x +rand(100) . "some text" . " "x rand(100) } 1 .. 100; timethese( 100_ +000, { single_regexp => sub { return map { s/^\s*(.+?)\s*$/$1/; $_ } + @list1 }, dual_regexp => sub { return map { s/^\s+//; s/\s+$//; $_ } + @list2 } } ); ' Benchmark: timing 100000 iterations of single_regexp, dual_regexp... single_regexp: 56 wallclock secs (56.04 usr + 0.00 sys = 56.04 CPU +) @ 1784.44/s (n=100000) dual_regexp: 17 wallclock secs (18.63 usr + 0.00 sys = 18.63 CPU) +@ 5367.69/s (n=100000)
|
---|
In Section
Seekers of Perl Wisdom