http://qs1969.pair.com?node_id=11121801


in reply to Re^7: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff
in thread What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff

Had a reeeeeaaaaly vague recollection and the Perl Cookbook (Ch 1 section 19) does actually offer $_ = join(' ', split(' ')); as an alternative to these three substitutions:

s/^\s+//; s/\s+$//; s/\s+/ /g;

to strip and canonicalize to single spaces; and does offer this trim sub:

sub trim { my @out = @_ ? @_ : $_; $_ = join(' ', split(' ')) for @out; return wantarray ? @out : "@out"; }

Edit: That being said, I don't recall having seen this construct in the wild otherwise and had the vaguest of hunches that PC mentioned anything like this so I'd hardly call it a "common idiom" either.

The cake is a lie.
The cake is a lie.
The cake is a lie.

  • Comment on Re^8: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff
  • Select or Download Code

Replies are listed 'Best First'.
Re^9: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff
by Tux (Canon) on Sep 15, 2020 at 16:39 UTC

    How often do you need to compress internal whitespace? Anyway. it is faster:

    $ perl -MBenchmark=cmpthese -wE'my$x=join" "=>"",("abc")x5,"";say"s +ourc: |$x|";sub trim{join" ",split" ",$x};sub rgx{$x=~s/^\s+//r=~s/\s ++$//r=~s/\s\s+/ /gr};say "split: |",trim(),"|";say"regex: |",rgx(),"| +";cmpthese(-2,{splt=>\&trim,rgx=>\&rgx})' sourc: | abc abc abc abc abc | split: |abc abc abc abc abc| regex: |abc abc abc abc abc| Rate rgx splt rgx 506423/s -- -71% splt 1767763/s 249% --

    Enjoy, Have FUN! H.Merijn

      I think I always want whitespace normalized. I can’t think of a case where it wouldn’t bother me, even if, as in HTML rendering defaults, it generally will not be apparent. Two spaces between words in a title or regular text is as much a typographical error as a misspelling.

      $ perl -MBenchmark=cmpthese -wE'my$x=join"    "=>"",("abc")x5,"";say"sourc: ... yadda yadda ...

      Sorry, but this overly wide code-line is breaking the formatting of most of the thread. (took a bit to find it too)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        strange, logging out showed proper wrapping of long lines.

        Not sure whats going wrong here, these are my Display Settings

        *Code Listing Settings* Code Wrapping Off [ ] Code Wrap Length [80] Auto Code Wrapping [x] Code Prefix [<font color="red"><i></i></font>]

        have to check my own CSS settings and other browsers.

        never mind! :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery