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

Re: Revisiting trim

Thread Previous | Thread Next
Dan Book
May 28, 2021 16:32
Re: Revisiting trim
Message ID:
On Fri, May 28, 2021 at 12:26 PM 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.
> 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.

My two cents on the parameterized trims:

1) trim-right and trim-left are certainly reasonable use cases, *however*
they are not as common a need across CPAN and general code.

2) The Perlish way is to add an option rather than similar functions with
slightly different names.

3) Such an option or additional functions can be added later; even possibly
during the two-year experimental window of the feature.


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