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

RE: Let's talk about trim() so more

Thread Previous | Thread Next
March 30, 2021 19:13
RE: Let's talk about trim() so more
Message ID:
From the keyboard of Konovalov, Vadim [28.03.21,05:59]:

>> From: Christian Walde
>> Nope, the motivation is to replace this:
>> $a =~ s/^\s+//; $a =~ s/\s+$//;
>> Which is like
>> 1600+ times on cpan.
> My CPAN modules do not count: I also use s/^\s+|\s+$/g often
> At the same time I am opposed to having trim as part of perl syntax. (string::trim or another package is better, so it will not conflict, but I doubt I will ever use it)
> I don't think I am alone with my view

You are not alone. I don't understand all the hullaballoo about trim().
And I don't get why it ever should be implemented in core.
Removing something from the beginning and/or end from some something is
a no-brainer in perl. If I need to do it, I just do it.

     $thingy =~ s/^\s+|\s+$//g; # or some such

Okay, okay, maybe \z instead of $ as anchor. Depends.
If that pattern happens too often, I whip up a subroutine. And yes,
to do that inplace or returning a result, there's wantarray. If I want
to apply it on an array, that's easy enough, since there's loops.
And statement modifiers. So I come up with this

     sub trim {
        	if (defined wantarray) {
 	    my @ret = @_;
 	    s/^\s+|\s+$//g for @ret;
 	    return @ret;
 	} else {
 	    s/^\s+|\s+$//g for @_;

and it just worksforme™. Wait, what? there are UTF8 whitespace characters
not included in \s ? Okay, I'll care for that.
I might want to trim some "#!%$bzz~Pfrll!" ? Okay, I modify my sub to do
that. No problem.

Why, for the sake of... er, something... does this simple operation draw
so much brain power from the perl porters? Why a perl keyword for it?
Just because other languages have it? Because in them languages trimming
isn't that durn easy? I don't get it.

And then orthogonality. What if I want to trim a blessed arrayref? or a
hash? a hashref? Some overloaded what-not-oozy-crap? a filehandle? etc

Are we having trim, tromp, tramp, trump, trim_first, trim_last etc
going the weird way of php? a trim with modifiers as a one ring to rule
them all? pages of documentation about trim()? It's not like pack().

There are many ways to take off the beard. Scissors, fire, dog bites,
depilating creams, atomic bombs. To each as it suits them, having some
sort of beard, in the face or around the ass. Or none.
It is easy enough. Let them do it as they like, without musing about
what the documentation of trim sayeth.

Don't waste your time reading this. Sorry for that.

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                               /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About