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

Re: Revisiting trim

Thread Previous | Thread Next
Joseph Brenner
May 28, 2021 16:25
Re: Revisiting trim
Message ID:
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.

I've no strong feelings on the "trim" discussion, but I think you
argue well that the "rtrim" case is pretty common.

I think tchrist probably has a point about the clarity of "trimmed",
but I suspect if it'd been up to Larry Wall, he'd have gone with the
shortest form.   For some reason "trim", "trim('R')" and "trim('L')"
seem perlish too me (though I gather "parameterization" is supposed to
be off the table at this point, so an R/L argument would be
controversial, too).

I see that in Raku, the routines are called "trim", "trim-leading" and
"trim-trailing".  (None of these trim in-place, to do that you'd use
this idiom: "$line.=trim;").

My apologies if it seems like we're re-opening old discussions at this
point, but it's a problem in these debates that there's no easy way to
review what's already been talked to death.

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