develooper Front page | perl.perl5.porters | Postings from May 2021

Re: Revisiting trim

Thread Previous | Thread Next
Tom Molesworth via perl5-porters
May 29, 2021 04:46
Re: Revisiting trim
Message ID:
On Sat, 29 May 2021 at 04:31, David Nicol <> wrote:

> On Fri, May 28, 2021 at 11:25 AM Joseph Brenner <> wrote:
>> André Warnier (tomcat/perl) <> wrote:
>> > $stripped_line =~ s/^\s+//; $stripped_line =~ /\s+$//; # or only one of
>> those, depends
>> > Is /that/ the worst possible way ? or if not *the* worst, was there a
>> better way all along ? (*)
>> That's a very reasonable way of doing it which may very well be the
>> best way (though you dropped an "s" on the second "s///").
>> They were probably referring to a tendency of many programmers to
>> obsess with trimming the left and right with a single s/// operation,
>> which will result in a hairy, unreadable solution that won't peform
>> any better than just doing it in two steps.
> Is this really slowerr? Is this really hairier and less readable than the
> two step approach?
>      $reference_identifier =~ s/^\s*(.+?)\s*$/$1/;  # how I usually
> full-trim a reference identifier

It's not equivalent - you're missing the /s modifier, so a string such as
"  example\ntext  " would not match.

So, yes - there's a discussion about benchmarking and whether it's more
efficient, but given the minor detail that it's doing something *different*
appears to have escaped attention, I'd suggest that this was indeed less

(also yes, I appreciate that this is $reference_identifier and earlier we
had $stripped_line, but the purpose of trimmed() is "remove whitespace from
the start and end of a string", so "similar problem domain with different
details" just muddies the discussion)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About