develooper Front page | perl.perl5.porters | Postings from February 2022

Re: trim vs trimmed revisited

Thread Previous | Thread Next
Ovid via perl5-porters
February 26, 2022 08:49
Re: trim vs trimmed revisited
Message ID:
Apologies if these bits are already decided. Feel free to ignore.

On Friday, 25 February 2022, 19:20:50 CET, David Nicol <> wrote:

> Is it set in stone yet that trim takes only one arg (has a ($) prototype)?
> Compare:
>     my @trimmed = trim @strings;  # handling multiple arguments is orthogonal to in-place or not

I don't think that's orthogonal. If we do in-place on the grounds that things like chomp() do that, I would ask why chomp() allows a list and trim() does not.

Also, in-place means that this is going to bite people:

    my @orig = ...; # must not be changed
    my @new = map { trim } @orig;  # oops. you changed it

Even if in-place also returned the trimmed value, we've mutated our original list, so I need to make an extra copy to protect it. That's an annoying bit of structural code to need to remember to achieve the behavior I want. (though in practice, I don't know that this would be a big deal).

Also, I've had a couple of weird "action at a distance" issues I've debugged in the past because someone's written something like `s/x/y/ for @_` and there's been a constant passed to the sub in question.

Having trim() behave like, well, just about every other language would not only avoid what I think are traps (but maybe not serious ones), but also be less surprising (the more serious issue).

IT consulting, training, specializing in Perl, databases, and agile development 

Buy my book! -

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