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

Re: trim vs trimmed revisited

Thread Previous | Thread Next
Paul "LeoNerd" Evans
February 25, 2022 15:09
Re: trim vs trimmed revisited
Message ID:
On Fri, 25 Feb 2022 01:24:00 -0600
David Nicol <> wrote:

> IMO trim should operate in place in void context and return the
> substring in nonvoid context. Optimizing

Functions that do materially different things depending on context have
been a constant annoyance to newbies and experts alike, ever since the
language was first created. They are commonly agreed to be one of the
worst mis-features of the entire language.

What does this do?

  sub shortname {
     my $self = shift;
     return trim( $self->{fullname} );

Does it mutate the object instance, or not? Perhaps it depends on the

Maybe far away in a script somewhere many files away:

  printf "My shortname is %s\n", $user->shortname;

  printf "Fullname is now %s\n", $user->{fullname};

Oops. We just broke the object.

This sort of "spooky action-at-a-distance" makes it hard to build
robust code and reason about its behaviour in small modular pieces.

Paul "LeoNerd" Evans      |  |

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