I don't know where I got the idea in my brain that the global substitute expression was faster. It could very well be that I figured that in some normal case, the difference didn't matter based upon some other benchmark.

I re-wrote your benchmark and yes, this does show that using 2 lines of Perl to do this is faster!
I show my code below..

use strict; use warnings; use Benchmark qw(cmpthese); use Data::Dumper; use List::Util qw(pairmap); my @strings = ( no_trim_short => 'asd', no_trim_mid => 'asdasdasdasdasdasdasd', no_trim_long => 'asd' x 50, no_trim_mid_with_ws => 'asd asd asd asd asd asd asd', no_trim_long_with_ws => (join ' ', ('asd') x 20), short => ' asd ', mid => ' asdasdasdasdasdasdasd ', long => ' '.('asd' x 20).' ', mid_with_ws => ' asd asd asd asd asd asd asd ', long_with_ws => ' '.(join ' ', ('asd') x 20).' ', ); my @test_line_array = pairmap{$b}@strings; #print "$_\n" for @test_line_array; #for debug cmpthese (100000, { global => sub{ my @copy = @test_line_array; $_ =~ s/\A\s+|\s+\z//g foreach @copy; }, global2 => sub{ my @copy = @test_line_array; $_ =~ s/^\s+|\s+$//g foreach @copy; }, twoLines => sub { my @copy = @test_line_array; s/\A\s+//, s/\s+\z// foreach @copy; }, twoLines2 => sub { my @copy = @test_line_array; s/^\s+//, s/\s+$// foreach @copy; }, dollar1 => sub { my @copy = @test_line_array; s/\A\s*(.*?)\s*\z/$1/ foreach @copy; } }); print "\n"; __END__ Rate global global2 dollar1 twoLines twoLines2 global 33681/s -- 0% -21% -68% -68% global2 33681/s 0% -- -21% -68% -68% dollar1 42662/s 27% 27% -- -60% -60% twoLines 106610/s 217% 217% 150% -- -0% twoLines2 106724/s 217% 217% 150% 0% --

In reply to Re^4: How to trim a line from leading and trailing blanks without using regex or non-standard modules by Marshall
in thread How to trim a line from leading and trailing blanks without using regex or non-standard modules by likbez

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.