I think you'll find that that runs much faster than one regexp involving alternation....
Well, I don't really care much about faster runtimes -- most of what I do waits on disk access and network I/O long before it runs into RAM or CPU limits, so which regex is fastest almost never comes into play.
I try to write primarily for readability. I realize, of course, that "readability" is subjective. But, I find s{^\s+|\s+$}{}g to be more readable than the two-line alternative. When I see that one line, I immediately think "ok, trim whitespace from the ends". When I see the two, I have to stop and think about it. Just me.
One of the things I like about Python1 is the readability of performing this action:
text = " a string of some sort, with space at either end " text = text.strip()
I think that's what draws me to Text::Trim (if I have to do the step repeatedly, especially):
my $text = " a string of some sort, with space at either end "; $text = trim( $text );
Of course, creating a dependency for something like that seems a little excessive, so I've recently taken to
my $TRIM_WHITESPACE_RE = qr/^\s+|\s+$/ ## ... later on .. ## $text =~ s/$TRIME_WHITESPACE_RE//g;
1: I know, I know. :) Seriously, though, Python has some nifty things in it, even if I still like Perl better.
In reply to Re^2: How to introduce a frustrating bug with a single whitespace
by radiantmatrix
in thread How to introduce a frustrating bug with a single whitespace
by radiantmatrix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |