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.

<radiant.matrix>
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.