Mailing List Archive

trim - wrapping up the discussion
There has been a lot of discussion about trim(), here on p5p[1], on a github issue[2], and a pull request[3] with a proposed implementation. We've started this new thread to present a concise summary and ask people to specify their preferred option. Please do not start discussion on this thread.

We would particularly like core team members to reply.

Summary of the picture

• Looking at CPAN, when people have wanted to trim a variable, they've been massively more likely to just use a regexp than either write their own function or use one from CPAN. But in plenty of cases they get it wrong.
• There are CPAN modules offering both edit-in-place and return-trimmed-string. The latter are more widely used, though a third of those uses are of the form $_ = trim($_).
• When people roll their own trim() function, they're more likely to return the mutated string, but it's not 100%.

Brief pitch for the options:

• Return value: when people write a trim() function this is what they tend to write; can be composed and used in a map; there are plenty of other string built-ins that return a mutated copy.
• Edit-in-place: most people are editing in place to trim a variable; similar to chomp and s/// which both edit in-place; would take an lvalue so you can write trim(my $x = …);

I think we're all agreed on these points:

• There's evidence on CPAN that this is worth adding to the language.
• Trim should remove whitespace on both ends of the string.
• Trim Unicode \s – don't support an argument for specifying what to trim.
• A single function that works both ways, depending on context, is not a good solution.

What do you think we should do?

1. Two separate but similarly named functions. Names TBD.
2. One trim() function that returns the trimmed string
3. One trim() function that edits in place
4. Leave it to CPAN
5. Given we've missed the 5.34 boat, perform a wider review of text processing gaps in Perl, possibly resulting in a broader proposal, which might change how we think about trim.

[1] https://www.nntp.perl.org/group/perl.perl5.porters/2021/03/msg259427.html
[2] https://github.com/Perl/perl5/issues/17952
[3] https://github.com/Perl/perl5/pull/17999
Re: trim - wrapping up the discussion [ In reply to ]
As the original proponent of this issue my vote is: 2. One trim()
function that returns the trimmed string

On 3/31/21 1:12 PM, neilb@neilb.org wrote:
> There has been a lot of discussion about trim(), here on p5p[1], on a
> github issue[2], and a pull request[3] with a proposed implementation.
> We've started this new thread to present a concise summary and ask
> people to specify their preferred option. Please do not start
> discussion on this thread.
>
> We would particularly like core team members to reply.
>
> Summary of the picture
>
> * Looking at CPAN, when people have wanted to trim a variable,
> they've been massively more likely to just use a regexp than
> either write their own function or use one from CPAN. But in
> plenty of cases they get it wrong.
> * There are CPAN modules offering both edit-in-place and
> return-trimmed-string. The latter are more widely used, though a
> third of those uses are of the form $_ = trim($_).
> * When people roll their own trim() function, they're more likely to
> return the mutated string, but it's not 100%.
>
> Brief pitch for the options:
>
> * Return value: when people write a trim() function this is what
> they tend to write; can be composed and used in a map; there are
> plenty of other string built-ins that return a mutated copy.
> * Edit-in-place: most people are editing in place to trim a
> variable; similar to chomp and s/// which both edit in-place;
> would take an lvalue so you can write trim(my $x = …);
>
> I think we're all agreed on these points:
>
> * There's evidence on CPAN that this is worth adding to the language.
> * Trim should remove whitespace on both ends of the string.
> * Trim Unicode \s – don't support an argument for specifying what to
> trim.
> * A single function that works both ways, depending on context, is
> not a good solution.
>
> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string 
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text
> processing gaps in Perl, possibly resulting in a broader proposal,
> which might change how we think about trim.
>
> [1] https://www.nntp.perl.org/group/perl.perl5.porters/2021/03/msg259427.html
> <https://www.nntp.perl.org/group/perl.perl5.porters/2021/03/msg259427.html>
> [2] https://github.com/Perl/perl5/issues/17952
> <https://github.com/Perl/perl5/issues/17952>
> [3] https://github.com/Perl/perl5/pull/17999
> <https://github.com/Perl/perl5/pull/17999>
Re: trim - wrapping up the discussion [ In reply to ]
On Wed, 31 Mar 2021 22:12:24 +0200, <neilb@neilb.org> wrote:
> What do you think we should do?
> Two separate but similarly named functions. Names TBD.
> One trim() function that returns the trimmed stringOne trim() function that edits in place
> Leave it to CPAN
> Given we've missed the 5.34 boat, perform a wider review of text processing gaps in Perl, possibly resulting in >a broader proposal, which might change how we think about trim.

In order of preference:

1 2 3 4

Also i think 5 is a good idea regardless of what else is decided.

--
With regards,
Christian Walde
Re: trim - wrapping up the discussion [ In reply to ]
On 3/31/21 4:12 PM, neilb@neilb.org wrote:
> There has been a lot of discussion about trim(), here on p5p[1], on a
> github issue[2], and a pull request[3] with a proposed implementation.
> We've started this new thread to present a concise summary and ask
> people to specify their preferred option. Please do not start discussion
> on this thread.
>
> We would particularly like core team members to reply.
>
> Summary of the picture
> [snip]
> Brief pitch for the options:
> [snip]
> I think we're all agreed on these points:
> [snip]
>
> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text
> processing gaps in Perl, possibly resulting in a broader proposal,
> which might change how we think about trim.
>

My preference: #1 -- provided that the implementation/maintenance burden
of two builtins is not excessive. Rationale: both versions have their
advocates and plausible use-cases.

A note on #5: If we have such a review process, it should have a clear
mission statement, be time-boxed, *not* take place primarily on the
perl5-porters list, and have project leaders accountable to the Perl
Steering Council.

Thank you very much.
Jim Keenan
Re: trim - wrapping up the discussion [ In reply to ]
On Wed, 31 Mar 2021 21:12:24 +0100
neilb@neilb.org wrote:

> I think we're all agreed on these points:
>
> • There's evidence on CPAN that this is worth adding to the language.
> • Trim should remove whitespace on both ends of the string.
> • Trim Unicode \s – don't support an argument for specifying what to
> trim.
> • A single function that works both ways, depending on context, is
> not a good solution.

Agree with all of these.

> What do you think we should do?

> 5. Given we've missed the 5.34 boat, perform a wider review of text
> processing gaps in Perl, possibly resulting in a broader proposal,
> which might change how we think about trim.

I think I would actually like to do this first.

I have a great number of other additions to suggest to that bucket - a
number of things from Scalar::Util or List::Util for a start, and
probably lots of other bits and pieces. We're looking quite lacking in
"regular functions" in core and I'd like us to address this more fully.
I think a trim() can easily be part of that via a wider effort. I
wouldn't like to make a special case of adding just trim() as a core
function, while we still lack such things as weaken() or refaddr().

Of the other options 1-4 I'd prefer in this order

Most preferred
> 2. One trim() function that returns the trimmed string

> 1. Two separate but similarly named functions. Names TBD.

> 4. Leave it to CPAN

> 3. One trim() function that edits in place
Least referred

Yes - I would rather not to add a function and leave it to CPAN, than
add only an inplace-mutator.

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: trim - wrapping up the discussion [ In reply to ]
On 3/31/21 2:12 PM, neilb@neilb.org wrote:
>
> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text
> processing gaps in Perl, possibly resulting in a broader proposal,
> which might change how we think about trim.
>

My order of preference is

5 with the proviso that it be complete enough to make the trim decision
early in the next cycle.

1 2 3

not 4 ever. It's too easy to get it wrong.
Re: trim - wrapping up the discussion [ In reply to ]
On Wed, Mar 31, 2021 at 4:12 PM <neilb@neilb.org> wrote:

>
>
> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text
> processing gaps in Perl, possibly resulting in a broader proposal, which
> might change how we think about trim.
>
> I don't have a strong opinion on this other than I am against 3 and 4. I
think 5 is worthwhile but don't think trim as a feature needs to depend on
it.

-Dan
Re: trim - wrapping up the discussion [ In reply to ]
On 2021-03-31 1:12 p.m., neilb@neilb.org wrote:
> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text processing
> gaps in Perl, possibly resulting in a broader proposal, which might change
> how we think about trim.

Neil, your outline looks good, I have no disagreement with the things you stated
that I didn't quote above.

My vote is for #1 or #5. Cleanly support both options.

-- Darren Duncan
Re: trim - wrapping up the discussion [ In reply to ]
On Thu, 1 Apr 2021 at 05:12, <neilb@neilb.org> wrote:

>
>
> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text
> processing gaps in Perl, possibly resulting in a broader proposal, which
> might change how we think about trim.
>
>
2 please.
Re: trim - wrapping up the discussion [ In reply to ]
neilb@neilb.org wrote:
:Summary of the picture
:
:Looking at CPAN, when people have wanted to trim a variable, they've been
:massively more likely to just use a regexp than either write their own
:function or use one from CPAN. But in plenty of cases they get it wrong.

Before anything else there'd be value in digging into this more - why do
they get it wrong, is there *anything* we could improve that would increase
their chances of getting it right?

If they're getting this wrong, chances are they're getting lots of other
regexps wrong, and adding a trim() function won't help for those other
cases.

:1. Two separate but similarly named functions. Names TBD.
:2. One trim() function that returns the trimmed string
:3. One trim() function that edits in place
:4. Leave it to CPAN
:5. Given we've missed the 5.34 boat, perform a wider review of text
:processing gaps in Perl, possibly resulting in a broader proposal, which
:might change how we think about trim.

4-ish: rather, leave it to regexps. But concentrate on improvements -
maybe only in docs, maybe also in the language itself - to increase the
chances people get these and other regexps right.

Hugo
Re: trim - wrapping up the discussion [ In reply to ]
From the keyboard of neilb@neilb.org [31.03.21,21:12]:

> I think we're all agreed on these points:
> * There's evidence on CPAN that this is worth adding to the language.
> * Trim should remove whitespace on both ends of the string.
> * Trim Unicode \s – don't support an argument for specifying what to trim.
> * A single function that works both ways, depending on context, is not a good solution.

Why isn't that a good solution? Is context in perl being dismissed?
Why shouldn't I be able to write

while (<>) {
trim;
...
}

and have a (destructively) trimmed $_ at ... ?

> What do you think we should do?
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string 
> 3. One trim() function that edits in place

This goes against the Camel's Hair. I'd rather have trim its argument
in void context. But then, if it goes core, I'd be able to override it,
wouldn't I?

> 4. Leave it to CPAN

This. Or not even that. Have a perlfaq entry about trim.

$ perldoc -q trim
No documentation for perl FAQ keyword 'trim' found

This entry would include all caveats, and all context issues. A sane
implementation. So people get educated, can copy/paste the default, roll
their own if needed, or use a module. If I'd implement it in core, I'd
prolly stuff that into Scalar::Util, given the above requirement

* Trim should remove whitespace on both ends of the string.

and done. But then, Scalar::Util could be an automagic package like
PerlIO from which trim() is imported (as any other functions it has) if
the compiler sees "trim" the first time. Waving hands here, I am.

0--gg-

--
_($_=" "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}
Re: trim - wrapping up the discussion [ In reply to ]
From the keyboard of hv@crypt.org [01.04.21,00:46]:

> neilb@neilb.org wrote:
> :Summary of the picture
> :
> :Looking at CPAN, when people have wanted to trim a variable, they've been
> :massively more likely to just use a regexp than either write their own
> :function or use one from CPAN. But in plenty of cases they get it wrong.
>
> Before anything else there'd be value in digging into this more - why do
> they get it wrong, is there *anything* we could improve that would increase
> their chances of getting it right?
>
> If they're getting this wrong, chances are they're getting lots of other
> regexps wrong, and adding a trim() function won't help for those other
> cases.
>
> :1. Two separate but similarly named functions. Names TBD.
> :2. One trim() function that returns the trimmed string
> :3. One trim() function that edits in place
> :4. Leave it to CPAN
> :5. Given we've missed the 5.34 boat, perform a wider review of text
> :processing gaps in Perl, possibly resulting in a broader proposal, which
> :might change how we think about trim.
>
> 4-ish: rather, leave it to regexps. But concentrate on improvements -
> maybe only in docs, maybe also in the language itself - to increase the
> chances people get these and other regexps right.
>
> Hugo
>

+1

0--gg-

--
_($_=" "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}
Re: trim - wrapping up the discussion [ In reply to ]
On Wed, Mar 31, 2021 at 09:12:24PM +0100, neilb@neilb.org wrote:
> I think we're all agreed on these points:
>
> • There's evidence on CPAN that this is worth adding to the language.
> • Trim should remove whitespace on both ends of the string.
> • Trim Unicode \s – don't support an argument for specifying what to trim.
> • A single function that works both ways, depending on context, is not a good solution.
>
> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text processing gaps in Perl, possibly resulting in a broader proposal, which might change how we think about trim.

2

But I can accept 1.

5 is reasonable whatever happens with trim.

Tony
Re: trim - wrapping up the discussion [ In reply to ]
On further thought, it seems to me like trim() should be treated much the same
as commonly used Scalar::Util routines.

A simple and effective solution is to just add trim() etc to Scalar::Util, which
being a dual-life module is available bundled with newer Perl and its on CPAN
for older Perl and works the same either way.

I would say that any argument that trim() should be a language built-in in the
same way that keys() etc is, that can be applied to refaddr() and a bunch of
other Scalar::Utils things, which arguably are more like built-ins than trim()
would ever be.

-- Darren Duncan
Re: trim - wrapping up the discussion [ In reply to ]
On 2021-03-31 2:12 pm, neilb@neilb.org wrote:
>* A single function that works both ways, depending on context, is not a good solution.

When I write a trim() function, it usually is context-sensitive because
the way Perl can DWIM is one of the things that makes it more enjoyable
than other languages. But as to what would be a good way to handle
self-reflexive functions while dealing with the awkward points is worth
its own discussion.

>5. Given we've missed the 5.34 boat, perform a wider review of text processing gaps in Perl, possibly resulting in a broader proposal, which might change how we think about trim.

...so I'd pick (5); I think there are various principles to be sorted
out first, that would apply to deciding how 'trim' or other proposals
should work.
Re: trim - wrapping up the discussion [ In reply to ]
What do you think we should do?

1. Two separate but similarly named functions. Names TBD.
2. One trim() function that returns the trimmed string
3. One trim() function that edits in place
4. Leave it to CPAN
5. Given we've missed the 5.34 boat, perform a wider review of text
processing gaps in Perl, possibly resulting in a broader proposal, which
might change how we think about trim

---

I think 2 is good. I can receive 1.

On the other hand, 5 is needed because why only trim is given special
position as a part of perl syntax

This means why only trim uses a feature bit and is implemented in pp.c.


2021?4?1?(?) 5:12 <neilb@neilb.org>:

> There has been a lot of discussion about trim(), here on p5p[1], on a
> github issue[2], and a pull request[3] with a proposed implementation.
> We've started this new thread to present a concise summary and ask people
> to specify their preferred option. Please do not start discussion on this
> thread.
>
> We would particularly like core team members to reply.
>
> Summary of the picture
>
> - Looking at CPAN, when people have wanted to trim a variable, they've
> been massively more likely to just use a regexp than either write their own
> function or use one from CPAN. But in plenty of cases they get it wrong.
> - There are CPAN modules offering both edit-in-place and
> return-trimmed-string. The latter are more widely used, though a third of
> those uses are of the form $_ = trim($_).
> - When people roll their own trim() function, they're more likely to
> return the mutated string, but it's not 100%.
>
> Brief pitch for the options:
>
> - Return value: when people write a trim() function this is what they
> tend to write; can be composed and used in a map; there are plenty of other
> string built-ins that return a mutated copy.
> - Edit-in-place: most people are editing in place to trim a variable;
> similar to chomp and s/// which both edit in-place; would take an lvalue so
> you can write trim(my $x = …);
>
> I think we're all agreed on these points:
>
> - There's evidence on CPAN that this is worth adding to the language.
> - Trim should remove whitespace on both ends of the string.
> - Trim Unicode \s – don't support an argument for specifying what to
> trim.
> - A single function that works both ways, depending on context, is not
> a good solution.
>
> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text
> processing gaps in Perl, possibly resulting in a broader proposal, which
> might change how we think about trim.
>
> [1]
> https://www.nntp.perl.org/group/perl.perl5.porters/2021/03/msg259427.html
> [2] https://github.com/Perl/perl5/issues/17952
> [3] https://github.com/Perl/perl5/pull/17999
>
Re: trim - wrapping up the discussion [ In reply to ]
On Thu, 1 Apr 2021 at 09:16, shmem <gm@qwurx.de> wrote:

> > 4. Leave it to CPAN
>
> This. Or not even that. Have a perlfaq entry about trim.
>

https://perldoc.perl.org/perlfaq4#How-do-I-strip-blank-space-from-the-beginning/end-of-a-string
?

has been there for 24 years:

https://github.com/Perl/perl5/blame/blead/cpan/perlfaq/lib/perlfaq4.pod
Re: trim - wrapping up the discussion [ In reply to ]
I used the wrong word.

I can receive 1 -> I can accept 1


2021?4?1?(?) 12:14 Yuki Kimoto <kimoto.yuki@gmail.com>:

> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text
> processing gaps in Perl, possibly resulting in a broader proposal, which
> might change how we think about trim
>
> ---
>
> I think 2 is good. I can receive 1.
>
> On the other hand, 5 is needed because why only trim is given special
> position as a part of perl syntax
>
> This means why only trim uses a feature bit and is implemented in pp.c.
>
>
> 2021?4?1?(?) 5:12 <neilb@neilb.org>:
>
>> There has been a lot of discussion about trim(), here on p5p[1], on a
>> github issue[2], and a pull request[3] with a proposed implementation.
>> We've started this new thread to present a concise summary and ask people
>> to specify their preferred option. Please do not start discussion on this
>> thread.
>>
>> We would particularly like core team members to reply.
>>
>> Summary of the picture
>>
>> - Looking at CPAN, when people have wanted to trim a variable,
>> they've been massively more likely to just use a regexp than either write
>> their own function or use one from CPAN. But in plenty of cases they get it
>> wrong.
>> - There are CPAN modules offering both edit-in-place and
>> return-trimmed-string. The latter are more widely used, though a third of
>> those uses are of the form $_ = trim($_).
>> - When people roll their own trim() function, they're more likely to
>> return the mutated string, but it's not 100%.
>>
>> Brief pitch for the options:
>>
>> - Return value: when people write a trim() function this is what they
>> tend to write; can be composed and used in a map; there are plenty of other
>> string built-ins that return a mutated copy.
>> - Edit-in-place: most people are editing in place to trim a variable;
>> similar to chomp and s/// which both edit in-place; would take an lvalue so
>> you can write trim(my $x = …);
>>
>> I think we're all agreed on these points:
>>
>> - There's evidence on CPAN that this is worth adding to the language.
>> - Trim should remove whitespace on both ends of the string.
>> - Trim Unicode \s – don't support an argument for specifying what to
>> trim.
>> - A single function that works both ways, depending on context, is
>> not a good solution.
>>
>> What do you think we should do?
>>
>> 1. Two separate but similarly named functions. Names TBD.
>> 2. One trim() function that returns the trimmed string
>> 3. One trim() function that edits in place
>> 4. Leave it to CPAN
>> 5. Given we've missed the 5.34 boat, perform a wider review of text
>> processing gaps in Perl, possibly resulting in a broader proposal, which
>> might change how we think about trim.
>>
>> [1]
>> https://www.nntp.perl.org/group/perl.perl5.porters/2021/03/msg259427.html
>> [2] https://github.com/Perl/perl5/issues/17952
>> [3] https://github.com/Perl/perl5/pull/17999
>>
>
Re: trim - wrapping up the discussion [ In reply to ]
Op 31-03-2021 om 22:12 schreef neilb@neilb.org:
>
> I think we're all agreed on these points:
>
> * There's evidence on CPAN that this is worth adding to the language.
> * Trim should remove whitespace on both ends of the string.
> * Trim Unicode \s – don't support an argument for specifying what to
> trim.
> * A single function that works both ways, depending on context, is
> not a good solution.
>

FWIW, I think it would make a nice addition and I think the wrong
implementations on CPAN support that it would be worthwile.


However, there are voices here that disagree with the first bullet, so
stating "we're all agreed" seems incorrect. I do think there's
overwhelming support, but stating it as above is going roughshod over
those that disagree.


M4
Re: trim - wrapping up the discussion [ In reply to ]
On Thu, Apr 1, 2021 at 5:27 AM Ben Bullock <benkasminbullock@gmail.com>
wrote:

> On Thu, 1 Apr 2021 at 09:16, shmem <gm@qwurx.de> wrote:
>
>> > 4. Leave it to CPAN
>>
>> This. Or not even that. Have a perlfaq entry about trim.
>>
>
>
> https://perldoc.perl.org/perlfaq4#How-do-I-strip-blank-space-from-the-beginning/end-of-a-string
> ?
>
> has been there for 24 years:
>
> https://github.com/Perl/perl5/blame/blead/cpan/perlfaq/lib/perlfaq4.pod
>
> Note that the word 'trim' isn't contained in that file so someone used to
it named like that won't find the FAQ entry you are referring to. At least
I don't know from that link what you're referring to.
Re: trim - wrapping up the discussion [ In reply to ]
5 is my preferred way of solving this as we have about half a year to
discuss.
What annoys me way more than trim is all those functions living in
List::Util, List::MoreUtils, List::AllUtils and Scalar::Util!
I don't think I've ever needed Hash::Util.
For me as a user I don't care if something is *implemented* in core or a
module as long as it is shipped with Perl (an all distros!) *and* that I
don't need to load it somehow.
Is it a viable solution to keep/add functions to dual-lifed modules but
load all/some of them on 'use v5.34;' or 'use v7;'?

Regarding trim itself, one function that returns the trimmed string would
be good enough for me as you always can write
$foo = trim($foo);
Maybe the compiler can detect that and optimize it.

Best regards, Alex
Re: trim - wrapping up the discussion [ In reply to ]
On Wed, 31 Mar 2021 21:12:24 +0100, neilb@neilb.org wrote:

> There has been a lot of discussion about trim (), here on p5p[1], on
> a github issue[2], and a pull request[3] with a proposed
> implementation. We've started this new thread to present a concise
> summary and ask people to specify their preferred option. Please do
> not start discussion on this thread.

I've skipped all discussions about trim, as my point (for me) was that
I thought that I would never use it anyway, but ...

That was my same thought when "say" came into scope, and I think I
could not live without it now, so looking ahead is a good thing. So I
reviewed this post with a fresh stance and change my opinion from "I
would not use it (period)" to "I might use it (in a future release)"
and with that opinion, how would I want to use it then.

> We would particularly like core team members to reply.
>
> Summary of the picture
>
> • Looking at CPAN, when people have wanted to trim a variable,
> they've been massively more likely to just use a regexp than either
> write their own function or use one from CPAN. But in plenty of
> cases they get it wrong.

I'm with Hugo here: what exactly did they get wrong and how can we
improve. Some wrongs may look wrong but might be deliberate when
looking at specific use.

> • There are CPAN modules offering both edit-in-place and
> return-trimmed-string. The latter are more widely used, though a
> third of those uses are of the form $_ = trim ($_).
> • When people roll their own trim () function, they're more likely
> to return the mutated string, but it's not 100%.

That is about the people that took the time to write their own, but it
does not show us how many *like* that implementation or use it.

> Brief pitch for the options:
>
> • Return value: when people write a trim () function this is what they
> tend to write; can be composed and used in a map; there are plenty
> of other string built-ins that return a mutated copy.

I can see the pro's

> • Edit-in-place: most people are editing in place to trim a variable;
> similar to chomp and s/// which both edit in-place; would take an
> lvalue so you can write trim (my $x = …);

As I did not read all the discussions, i lieu of chomp, would trim
also support

trim (my @foo = <>);
trim (my $fields = split m/\t/ => $data);

? *that* for me would be a big plus for this option.

> I think we're all agreed on these points:
>
> • There's evidence on CPAN that this is worth adding to the language.

+1

> • Trim should remove whitespace on both ends of the string.

+1

> • Trim Unicode \s – don't support an argument for specifying what to
> trim.

The docs should clearly show alternatives for only stripping horizontal
or vertical whitespace

> • A single function that works both ways, depending on context, is
> not a good solution.

I can see that this is not good from an implementation option, but the
reasons are not listed, so I just have to assume this was a conclusion
from consensus

> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim () function that returns the trimmed string
> 3. One trim () function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text
> processing gaps in Perl, possibly resulting in a broader proposal,
> which might change how we think about trim.

At this moment in time, my opinion would be

5 first

1 second

if trim () would support lists and arrays like chomp,

3, 2

else

2, 3

but not 4

> [1] https://www.nntp.perl.org/group/perl.perl5.porters/2021/03/msg259427.html
> [2] https://github.com/Perl/perl5/issues/17952
> [3] https://github.com/Perl/perl5/pull/17999

--
H.Merijn Brand https://tux.nl Perl Monger http://amsterdam.pm.org/
using perl5.00307 .. 5.33 porting perl5 on HP-UX, AIX, and Linux
https://tux.nl/email.html http://qa.perl.org https://www.test-smoke.org
Re: trim - wrapping up the discussion [ In reply to ]
On 3/31/21 10:26 PM, Ben Bullock wrote:
> On Thu, 1 Apr 2021 at 09:16, shmem <gm@qwurx.de <mailto:gm@qwurx.de>> wrote:
>
> >  4. Leave it to CPAN
>
> This. Or not even that. Have a perlfaq entry about trim.
>
>
> https://perldoc.perl.org/perlfaq4#How-do-I-strip-blank-space-from-the-beginning/end-of-a-string?
>
> has been there for 24 years:
>
> https://github.com/Perl/perl5/blame/blead/cpan/perlfaq/lib/perlfaq4.pod
>
>
>>On 3/31/21 6:58 PM, shmem wrote:
>>
<snip>
>>> 4. Leave it to CPAN
>> This. Or not even that. Have a perlfaq entry about trim.
>>
>> $ perldoc -q trim
>> No documentation for perl FAQ keyword 'trim' found

You missed the crucial element of that comment ^^.

Brett
Re: trim - wrapping up the discussion [ In reply to ]
On Wed, 31 Mar 2021 21:12:24 +0100
neilb@neilb.org wrote:

> What do you think we should do?
>
> 1. Two separate but similarly named functions. Names TBD.
> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place
> 4. Leave it to CPAN
> 5. Given we've missed the 5.34 boat, perform a wider review of text processing gaps in Perl, possibly resulting in a broader proposal, which might change how we think about trim.

Both 1 and 2 are acceptable.

5 should happen either way.
Re: trim - wrapping up the discussion [ In reply to ]
On 3/31/21 8:35 PM, David Green wrote:
> On 2021-03-31 2:12 pm, neilb@neilb.org wrote:
>> * A single function that works both ways, depending on context, is not
>> a good solution.
>
> When I write a trim() function, it usually is context-sensitive because
> the way Perl can DWIM is one of the things that makes it more enjoyable
> than other languages.  But as to what would be a good way to handle
> self-reflexive functions while dealing with the awkward points is worth
> its own discussion.
>
>> 5. Given we've missed the 5.34 boat, perform a wider review of text
>> processing gaps in Perl, possibly resulting in a broader proposal,
>> which might change how we think about trim.
>
> ...so I'd pick (5); I think there are various principles to be sorted
> out first, that would apply to deciding how 'trim' or other proposals
> should work.

As part of 5, I think special care should be made to delineate things,
like all the various ways they may be consistent or inconsistent with
existing idioms and functions; a lot of talking is focused on
non-quantifiable things. Developer "intent" is not quantifiable; a
matrix describing all methods and their properties (e.g., context,
return value, customizability, effect on input parameter, attempt to
demonstrate current WTDI*, etc) is. And the work can be accomplished in
parallel by anyone interested in doing so.

In the analysis, information I think would be valued, in order (my
preference most desirable to least):

a. attempt to demonstrate the degree to which it's solving an actual
problem - and what that problem actually is
b. idiomatic consistency (e.g., DWIM, etc), descriptiveness (including
in terms of accessing said feature)
c. progress towards larger design/consistency[internal&external] goals
d. a sufficiently bias-resistent** number of informed opinions on
implementation difficulties and impact on internals [. there are 'costs';
obvious ones are things like developer time; other costs come in forms
hindering of later/bigger goals, adding maintenance costs, breaking
consistency with idiomatic Perl, etc]
e. actual performance considerations (not algorithmic complexity, mind you)

I am happy to help facilitate anything related to 5 if and when that
time comes. While I think it's an excellent idea and *can* produce
extremely important information, I think it should be done as well as
possible *in* a bounded amount of time. This kind of thing could also
easily get hijacked, as can all bureaucratic processes [specifications,
standards, etc, etc] - I would not blame anyone for accusing me of doing
that very thing with this email, mind you (definitely not my intent).

Notes:

* 'costs' - anything from developer time to a demonstration of how
"astonishingly" consistent it is or is not with other parts of the language

** 'bias-resistent' - 10 people attempting to clearly explain that
something is "too hard" or "would necessarily break too many things" is
way more convincing than a couple of people hand waiving; even better,
the approach of "we could do this, but this is what it'd mean: <insert
internal/external ramifications here>". This just goes back to "doing
the right thing" is usually equivalent to "doing the hard thing". Why do
I say this? Because the "right thing" in many of these current
discussions is pretty apparent; unfortunately it seems other factors
like, "the works already been done" or "this would be easier if we made
XYZ concessions, etc", are at play. That's just my general gut feeling.

Brett

1 2  View All