$text =~ s/^\s+|\s+$//g;

This can be hopelessly inefficient (the regexp engine gets bogged down in the middle of the string, looking for hypothetical end anchors). The longer the string gets, the better it is to write:

$text =~ s/^\s+//g; $text =~ s/\s+$//g;

Consider the following, somewhat pathological cases:

#! /usr/local/bin/perl -w use Benchmark qw/:all/; ` my $long = ' aaa bbb ccc' . (' ' x 100).'ggg hhh '; my $short = ' aaa bbb ccc ddd eee fff ggg hhh '; sub one_long { my $s = $long; $s =~ s/^\s+|\s+$//g; $s; } sub one_short { my $s = $short; $s =~ s/^\s+|\s+$//g; $s; } sub two_long { my $s = $long; $s =~ s/^\s+//g; $s =~ s/\s+$//g; $s; } sub two_short { my $s = $short; $s =~ s/^\s+//g; $s =~ s/\s+$//g; $s; } print "tests:\n"; { no strict 'subs'; print "$_ [", &$_, "]\n" for qw/one_long one_short two_long two_short/; } cmpthese( shift || 1000, { one_long => \&one_long, one_short => \&one_short, two_long => \&two_long, two_short => \&two_short, } ); __PRODUCES__ tests: one_long [aaa bbb ccc + ggg hhh] one_short [aaa bbb ccc ddd eee fff ggg hhh] two_long [aaa bbb ccc + ggg hhh] two_short [aaa bbb ccc ddd eee fff ggg hhh] Benchmark: timing 100000 iterations of one_long, one_short, two_long, +two_short... one_long: 8 wallclock secs ( 7.13 usr + 0.00 sys = 7.13 CPU) @ 14 +019.72/s (n=100000) one_short: 2 wallclock secs ( 1.62 usr + 0.00 sys = 1.62 CPU) @ 61 +835.75/s (n=100000) two_long: 1 wallclock secs ( 0.62 usr + 0.00 sys = 0.62 CPU) @ 16 +0000.00/s (n=100000) two_short: 0 wallclock secs ( 0.63 usr + 0.00 sys = 0.63 CPU) @ 15 +8024.69/s (n=100000) Rate one_long one_short two_short two_long one_long 14020/s -- -77% -91% -91% one_short 61836/s 341% -- -61% -61% two_short 158025/s 1027% 156% -- -1% two_long 160000/s 1041% 159% 1% --

It should be obvious from the results that it's good insurance to write it in the two s/// form and be done with it :)


In reply to Re:x2 split every second word (don't use a single s/// to trim both ends of a string) by grinder
in thread split every second word by Anonymous Monk

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.