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

Re: trim vs trimmed revisited

Thread Previous | Thread Next
From:
David Nicol
Date:
February 25, 2022 18:14
Subject:
Re: trim vs trimmed revisited
Message ID:
CAFwScO_yX+fogKhibxuR7MJ4+GCZkoTUa6PK7cUF0HwNTxJCaA@mail.gmail.com
hmm. I may just be being difficult, having internal featuritis. I'd like to
be able to trim arrayrefs and hashrefs at once, recursively, either
in-place or making a deep copy, depending. But without causing trouble.
Almost mentally healthy enough to delete this without sending.

 sub trim {
      if (wantarray){
              my @ret = @_;
              trim(@ret)
              return @ret;
      }elsif (defined wantarray){
              my $t = shift;
              trim($t);
              return $t;
      }

      # something about dealing with references here?

      s/\s*(.*?)\s*\z/$1/s for @_;
  }



> What does this do?
>
>   sub shortname {
>      my $self = shift;
>      return trim( $self->{fullname} );
>   }
>

Causes problems. Should raise a warning or not even compile. Better written
like so:

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

or maybe

 sub shortname {
     my $self = shift;
     "@{[trim( $self->{fullname}]}";
  }



-- 
"Lay off that whiskey, and let that cocaine be!" -- Johnny Cash

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About