Why s/^\s+|\s*$/g rather than s/^\s+|\s+$/g, s/^\s*|\s*$/g or s/^\s*|\s+$/g?
A benchmark suggests the two substitution approach is faster than any of the single substitution approaches and that there are interesting variations between the different single substitution options:
Rate starstar plusstar plusplus starplus twosub
starstar 47.0/s -- -8% -25% -28% -42%
plusstar 51.2/s 9% -- -18% -21% -37%
plusplus 62.5/s 33% 22% -- -4% -23%
starplus 65.1/s 39% 27% 4% -- -20%
twosub 81.6/s 74% 59% 31% 25% --
The benchmark uses a single large string (100_000 characters) with a fairly large run of spaces (1000) at the start and end.
DWIM is Perl's answer to Gödel
|