So, restricting to solutions that do not compress internal spaces, and ignoring the fact that " " is identical to \s, I am surprised that split is faster than tr. Converted the one-liner to a script for readability):

$ cat test.pl use 5.18.0; use warnings; use Benchmark qw( cmpthese ); my $x = join " " => "", ("abc") x 5, ""; sub splt { split " ", reverse ((split " ", (reverse $x), 1)[0]), 1; } sub rgx { $x =~ s/^\s+//r =~ s/\s+$//r; } sub trx { my $y = $x =~ y/ /x/cr; substr ($x, index ($y, "x"), rindex ($y, "x") - length ($x) + 1); } say "sourc: |$x|"; say "split: |", splt (), "|"; say "regex: |", rgx (), "|"; say "tr/x/: |", trx (), "|"; cmpthese (-2, { splt => \&splt, rgx => \&rgx, trx => \&trx });
$ perl test.pl sourc: | abc abc abc abc abc | split: |abc abc abc abc abc| regex: |abc abc abc abc abc| tr/x/: |abc abc abc abc abc| Rate rgx trx splt rgx 1047602/s -- -59% -65% trx 2553722/s 144% -- -14% splt 2958598/s 182% 16% --

So far, none of the presented alternatives to s{^\s+}{}r and friends appeal to me, even though twice as fast or more, I would definitely choose the regex one over the magic of the other two. YMMV.

Here's the one that also squeezes internal spaces with no \s, and there I would seriously consider the join/split variant:

$ cat test.pl use 5.18.0; use warnings; use Benchmark qw( cmpthese ); my $x = join " " => "", ("abc") x 5, ""; sub splt { join " " => split " " => $x; } sub rgx { $x =~ tr/ / /sr =~ s/^ //r =~ s/ $//r; } sub trx { my $y = $x =~ tr/ /x/cr; substr ($x, index ($y, "x"), rindex ($y, "x") - length ($x) + 1) = +~ tr/ / /sr; } say "sourc: |$x|"; say "split: |", splt (), "|"; say "regex: |", rgx (), "|"; say "tr/x/: |", trx (), "|"; cmpthese (-2, { splt => \&splt, rgx => \&rgx, trx => \&trx });
$ perl test.pl sourc: | abc abc abc abc abc | split: |abc abc abc abc abc| regex: |abc abc abc abc abc| tr/x/: |abc abc abc abc abc| Rate rgx splt trx rgx 1012851/s -- -42% -52% splt 1739341/s 72% -- -17% trx 2105282/s 108% 21% --

Enjoy, Have FUN! H.Merijn

In reply to Re^10: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff by Tux
in thread What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff 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.