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
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.

My vote: 2.

1. - also good, maybe better but keep "trim" name for one function.
3. - also good, btw, how is "chomp" implemented; I think "trim" should be
implemented and used in a similar way.
5. - definitely a good idea for future, but let's not miss this chance to
have "trim". The list of all "PHP" string functions distributed
earlier was really inspirational. Perl needs to its string-processing
strengths and even move towards more NLP.
4. - No. This is leaving status quo and it is the least preferred option.

Regards,
Vlado
Re: trim - wrapping up the discussion [ In reply to ]
On Wed, Mar 31, 2021 at 09:12:24PM +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.
>

I vote #3. I imagine I would typically use it in the while(<>) loop idiom.

--
Andy Dougherty doughera@lafayette.edu
Re: trim - wrapping up the discussion [ In reply to ]
On Thu, Apr 01, 2021 at 04:36:14AM -0500, B. Estrade wrote:
>
>
> 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

This problem is easy to solve (if someone better at words wants to
submit their own PR, I will withdraw mine).
(I didn't actually read the FAQ entry to know if it needed improvement
beyond the title)

https://github.com/Perl/perl5/pull/18675

l8rZ,
--
andrew - http://afresh1.com

"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots. So far, the Universe is
winning." -- Rich Cook
Re: trim - wrapping up the discussion [ In reply to ]
On Wed, Mar 31, 2021 at 09:12:24PM +0100, neilb@neilb.org wrote:

I realise that in starting to write this message, that as I agree with this:

> 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.

technically that might render all the other answers below moot. However, as
a chunk of decision making from me and others is "how do I think I would use
this?" the counter is that having something in blead and trying it out might
answer more than a *lot* of abstract thinking e-mail.


In the general case I'd also be concerned that there isn't an obvious way to
backport this such that CPAN code can depend on "core-or-pollyfill". But as
this one is a two-line implementation (for the efficient version) I don't
think that anyone will put in extra work to optionally use a core trim in
code published to CPAN.

I see trim as something to improve future programs, and address a small wart
in the language. If you need portability you'll still be "just inlining it"
for a good few years to come. But if your company codebase runs on something
modern, you're golden.


> What do you think we should do?

> 4. Leave it to CPAN

This is the "no change" option. I don't like this.

trim is a FAQ. Roughly, I see every "Frequently Asked Question" as
indication that there's something wrong with the language - ie the existence
of a FAQ implies a deficiency, that it might be good to fix.

This particular FAQ *starts*

=head2 How do I strip blank space from the beginning/end of a string?

(contributed by brian d foy)

A substitution can do this for you. For a single line, you want to
replace all the leading or trailing whitespace with nothing. You
can do that with a pair of substitutions:

s/^\s+//;
s/\s+$//;

You can also write that as a single substitution, although it turns
out the combined statement is slower than the separate ones. That
might not matter to you, though:

s/^\s+|\s+$//g;

(and continues with explaining the details here. To be clear - I think that
brian's answer is appropriate and on-target for the current situation)


The bit that always grates when I read this is that there is no single
*right* answer - there's the *faster* answer, and the 1-liner answer
(may or may not be viewed as prettier - again, no single right answer)

I'd like to be able to say

Use C<trim> and be done.

But if you need to support legacy versions...


(I'm aware that there's a joke that "for every problem there's an answer
which is simple, obvious and wrong" - I don't think that trap applies here)

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

Neil comments in an e-mail:

The built-in will (a) do what you meant, (b) be faster, and (c) be easier
to remember.


I don't like the idea of *two* versions. That's just even more things to
remember, without much functionality win - it's not going to make future
Perl easier to read for future newbies. Whilst technically we could have
infinite built in functions (all easy to search *if you already know the
name* and optimised for their task), I'd prefer that we add a few, well
chosen new builtins that combine to give maximum improvements, rather than
the same number of builtins that are mostly just variants of each other.

Someone else (Chris Prather?) took the quote

Perfection is achieved, not when there is nothing more to add, but when
there is nothing left to take away.

and observed that Antoine de Saint-Exup?ry was not famous for publishing
empty books. ie that this wasn't an argument that the only perfection was
zero.

To my mind, having two versions of "almost the same thing" can be improved
by choosing just one instead.


> 2. One trim() function that returns the trimmed string
> 3. One trim() function that edits in place

The choice between these two seems to come down to personal preference
between how one sees it being used. People are envisaging how they would
use it, and people are looking at what happens on CPAN.

I'm not convinced that the CPAN "results" showing more use "in place" are
actually useful - there wasn't enough context. (And getting context from
grep and analysing it is going to be extremely hard).

If one looks up the FAQ and then simply inlines it, that code is *in place*.
The FAQ doesn't mention s///r (and I think that it is correct not to), as
that would limit the code to newer Perl versions (5.14.0 or later, I think)
And I think that anyone "rolling their own" *for a module on CPAN* likely
will also be coding for the lowest version pre-req they can reasonably
target, to get the widest reach.


You can (mostly) implement one in terms of the other. The one thing that you
*can't* easily do is a trim analogy to `chomp @list` but I'm unaware of
every *seeing* code that had a list in an array and wanted to (just) do
this to it. Most/all code I see with chomp is doing it in a loop, on the
loop iterator, and chomp is the first step in a processing pipeline
*expressed as statements*.

ie I don't think that `trim @list` is ever going to be useful. Even less
useful than `study` or `reset`*

However most code I've encountered actually doing trim-like actions has
been doing it on return results from functions. Which are RVALUEs, and will
be assigned.

The "returns the trimmed string" version is also far cleaner in actual
functional code such as map.

You can implement either with the other. Tony C also notes that:

The existing PR does optimize on:

$y = trim $x;

where $y and $x, however complex they are as expressions, turn out to
be the same SV, but the code doesn't try to eliminate any other
duplication. eg. if $x and $y are lexicals, two padsv ops are produced.


So internally it can be (nearly) as performant as the "in place" version for
the "in place" case. For the general case, there's no way that perl's
peephole optimiser is good enough to make the reverse true for - spot all
the cases where there's an assignment first, and then generate a different
optree that eliminates that initial assignment.

Hence I'm preferring *just* the "returns the trimmed string" version.

Nicholas Clark

* They are useful. You can set a breakpoint on the OP in gdb, hack your
testcase to call one of these, and likely the only time your breakpoint is
hit is when the Perl code execution has reached the point you were
interested in. No, they are no longer useful for their intended purpose.
Re: trim - wrapping up the discussion [ In reply to ]
On 2021-04-03 8:07 a.m., Nicholas Clark wrote:
> In the general case I'd also be concerned that there isn't an obvious way to
> backport this such that CPAN code can depend on "core-or-pollyfill". But as
> this one is a two-line implementation (for the efficient version) I don't
> think that anyone will put in extra work to optionally use a core trim in
> code published to CPAN.

There is a way that is obvious to me, which I had previously shared, which is to
implement trim/etc soley as an addition to a dual-life module such as
Scalar::Util and include that version with the latest Perl.

Unless we decide to implement this as a modifier to regular expression syntax,
its just a plain old routine and there's no reason it has to be any more core
than a dual-life module.

-- Darren Duncan
Re: trim - wrapping up the discussion [ In reply to ]
One of the things that drives me nuts is the number of times I write

@foo = sort..map..grep...@source

and somewhere in there I do

{ chomp }

and it takes 30 minutes of head scratching to remember

{ chomp; $_}

I don't care if trim() ends up in core or Scalar::Util but please, please,
PLEASE, make the damn thing RETURN the trimmed arg. Honestly, I am about
thisclose to putting in a PR to Scalar::Utils for a function called
rchomp() - chomp that ALSO returns the chomped item.

Thank you.

On Sat, Apr 3, 2021 at 4:54 PM Darren Duncan <darren@darrenduncan.net>
wrote:

> On 2021-04-03 8:07 a.m., Nicholas Clark wrote:
> > In the general case I'd also be concerned that there isn't an obvious
> way to
> > backport this such that CPAN code can depend on "core-or-pollyfill". But
> as
> > this one is a two-line implementation (for the efficient version) I don't
> > think that anyone will put in extra work to optionally use a core trim in
> > code published to CPAN.
>
> There is a way that is obvious to me, which I had previously shared, which
> is to
> implement trim/etc soley as an addition to a dual-life module such as
> Scalar::Util and include that version with the latest Perl.
>
> Unless we decide to implement this as a modifier to regular expression
> syntax,
> its just a plain old routine and there's no reason it has to be any more
> core
> than a dual-life module.
>
> -- Darren Duncan
>


--
Matthew O. Persico
Re: trim - wrapping up the discussion [ In reply to ]
On 4/11/21 7:24 PM, Matthew Persico wrote:
> One of the things that drives me nuts is the number of times I write
>
> @foo = sort..map..grep...@source
>
> and somewhere in there I do
>
> { chomp }
>
> and it takes 30 minutes of head scratching to remember
>
> { chomp; $_}

I think this a case of it being astonishingly consistent.

map {
chomp;
} @source;

Is behaving as it would with:

while (<$fh>) {
chomp;
# .. do something with $_
}

So it seems the argument of "least astonishment" is necessary, but not
sufficient to cover all definitions of "astonishment".

It's a pickle, for sure.

Brett

>
> I don't care if trim() ends up in core or Scalar::Util but please,
> please, PLEASE, make the damn thing RETURN the trimmed arg. Honestly, I
> am about thisclose to putting in a PR to Scalar::Utils for a function
> called rchomp() - chomp that ALSO returns the chomped item.
>
> Thank you.
>
> On Sat, Apr 3, 2021 at 4:54 PM Darren Duncan <darren@darrenduncan.net
> <mailto:darren@darrenduncan.net>> wrote:
>
> On 2021-04-03 8:07 a.m., Nicholas Clark wrote:
> > In the general case I'd also be concerned that there isn't an
> obvious way to
> > backport this such that CPAN code can depend on
> "core-or-pollyfill". But as
> > this one is a two-line implementation (for the efficient version)
> I don't
> > think that anyone will put in extra work to optionally use a core
> trim in
> > code published to CPAN.
>
> There is a way that is obvious to me, which I had previously shared,
> which is to
> implement trim/etc soley as an addition to a dual-life module such as
> Scalar::Util and include that version with the latest Perl.
>
> Unless we decide to implement this as a modifier to regular
> expression syntax,
> its just a plain old routine and there's no reason it has to be any
> more core
> than a dual-life module.
>
> -- Darren Duncan
>
>
>
> --
> Matthew O. Persico
Re: trim - wrapping up the discussion [ In reply to ]
On Sun, 11 Apr 2021 20:24:52 -0400
Matthew Persico <matthew.persico@gmail.com> wrote:

> One of the things that drives me nuts is the number of times I write
>
> @foo = sort..map..grep...@source
>
> and somewhere in there I do
>
> { chomp }
>
> and it takes 30 minutes of head scratching to remember
>
> { chomp; $_}
>
> I don't care if trim() ends up in core or Scalar::Util but please,
> please, PLEASE, make the damn thing RETURN the trimmed arg. Honestly,
> I am about thisclose to putting in a PR to Scalar::Utils for a
> function called rchomp() - chomp that ALSO returns the chomped item.

Good idea.

If I remember, I'll pop that in when I'm next doing a release.

--
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 4/12/21 8:15 AM, Paul "LeoNerd" Evans wrote:
> On Sun, 11 Apr 2021 20:24:52 -0400
> Matthew Persico <matthew.persico@gmail.com> wrote:
>
>> One of the things that drives me nuts is the number of times I write
>>
>> @foo = sort..map..grep...@source
>>
>> and somewhere in there I do
>>
>> { chomp }
>>
>> and it takes 30 minutes of head scratching to remember
>>
>> { chomp; $_}
>>
>> I don't care if trim() ends up in core or Scalar::Util but please,
>> please, PLEASE, make the damn thing RETURN the trimmed arg. Honestly,
>> I am about thisclose to putting in a PR to Scalar::Utils for a
>> function called rchomp() - chomp that ALSO returns the chomped item.
>
> Good idea.
>
> If I remember, I'll pop that in when I'm next doing a release.
>

Pop what in where?

Brett
Re: trim - wrapping up the discussion [ In reply to ]
From the keyboard of Matthew Persico [11.04.21,20:24]:

> One of the things that drives me nuts is the number of times I write
>
> @foo = sort..map..grep...@source
>
> and somewhere in there I do 
>
> { chomp } 
>
> and it takes 30 minutes of head scratching to remember
>
> { chomp; $_}
>
> I don't care if trim() ends up in core or Scalar::Util but please, please,
> PLEASE, make the damn thing RETURN the trimmed arg. Honestly, I am about
> thisclose to putting in a PR to Scalar::Utils for a function called
> rchomp() - chomp that ALSO returns the chomped item.

Well, that's not a complling reason not to implement it context-aware.
If you can't remember how chomp works, it's not perls fault or an error
of implementation. You are telling us how /your/ brain works, not how
the language element should behave in a coherent way, given this is perl.

OTOH it feels rather silly to me to have to write

while (<>) {
$_ = trim $_;
...
}

which feels like back in the days of some unsuccessful BASIC where x=x+1
so I'd rather have trim() operate inplace in void context and return its
trimmed arguments otherwise.

What is more, I'd really like to have the perl context system expanded
making BOOLEAN a first-class context like VOID, SCALAR and LIST, so trim
in BOOLEAN context would work inplace and return TRUE iff its arg was
trimmed, e.g.

while (<>) {
if (trim) {
warn "superfluous surrounding whitespace at line $.\n";
}
... # do something with trimmed $_
}

The BOOLEAN context might be useful elsewhere. wantarray() could return
-1 in BOOLEAN context, or some such.

After all perl is a "linguistic" computer language; there could be more
useful contexts besides VOID SCALAR LIST and BOOLEAN, e.g. WITH¹ or REF.
Apologies if this has been discussed long ago and far away e.g. in the
Apoclypses leading to perl6/raku.

This opens the question: is context awareness a good thing to have and
likely to be expanded, or is it too hard to remember, too confusing
and thus likely to be phased out in the long run?

¹) I think of any builtin operating on $_ by default being in the WITH
context, so a builtin "with EXPR" would automagically set $_ to the
result of EXPR during its scope, even for builtins. EXPR might also be
a BLOCK being run when accessing $_, i.e. inside a map() or grep().

best regards,
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 B. Estrade [11.04.21,20:41]:

> I think this a case of it being astonishingly consistent.

No. Not at all.

> map {
> chomp;
> } @source;
>
> Is behaving as it would with:
>
> while (<$fh>) {
> chomp;
> # .. do something with $_

here ---^^^^^^^^^^^^^^^^^^^^

> }

Inside the while() you acually /do/ something with $_, whilst in the
map you do not. And chomp in the map actually returns something, which
is total number of characters removed.

If you don't do something with $_ in the while loop, the effect of
chomp is NIL. In the map, you also have to do something, at least state
$_. An empty map block is NIL also:

perl -E 'say map {;} (1,2,3)'

So, the behavior is consistent, anything else would be astonishing.

> So it seems the argument of "least astonishment" is necessary, but not
> sufficient to cover all definitions of "astonishment".
>
> It's a pickle, for sure.
>
> Brett
>
>>
>> I don't care if trim() ends up in core or Scalar::Util but please, please,
>> PLEASE, make the damn thing RETURN the trimmed arg. Honestly, I am about
>> thisclose to putting in a PR to Scalar::Utils for a function called
>> rchomp() - chomp that ALSO returns the chomped item.
>>
>> Thank you.
>>
>> On Sat, Apr 3, 2021 at 4:54 PM Darren Duncan <darren@darrenduncan.net
>> <mailto:darren@darrenduncan.net>> wrote:
>>
>> On 2021-04-03 8:07 a.m., Nicholas Clark wrote:
>> > In the general case I'd also be concerned that there isn't an
>> obvious way to
>> > backport this such that CPAN code can depend on
>> "core-or-pollyfill". But as
>> > this one is a two-line implementation (for the efficient version)
>> I don't
>> > think that anyone will put in extra work to optionally use a core
>> trim in
>> > code published to CPAN.
>>
>> There is a way that is obvious to me, which I had previously shared,
>> which is to
>> implement trim/etc soley as an addition to a dual-life module such as
>> Scalar::Util and include that version with the latest Perl.
>>
>> Unless we decide to implement this as a modifier to regular
>> expression syntax,
>> its just a plain old routine and there's no reason it has to be any
>> more core
>> than a dual-life module.
>>
>> -- Darren Duncan
>>
>>
>>
>> --
>> Matthew O. Persico
>

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 4/12/21 9:38 AM, shmem wrote:
> From the keyboard of B. Estrade [11.04.21,20:41]:
>
>> I think this a case of it being astonishingly consistent.
>
> No. Not at all.
>
>> map {
>>  chomp;
>> } @source;
>>
>> Is behaving as it would with:
>>
>> while (<$fh>) {
>>  chomp;
>>  # .. do something with $_
>
> here ---^^^^^^^^^^^^^^^^^^^^
>
>> }
>
> Inside the while() you acually /do/ something with $_, whilst in the
> map you do not. And chomp in the map actually returns something, which
> is total number of characters removed.
>
> If you don't do something with $_ in the while loop, the effect of
> chomp is NIL. In the map, you also have to do something, at least state
> $_. An empty map block is NIL also:
>
>     perl -E 'say map {;} (1,2,3)'
>
> So, the behavior is consistent, anything else would be astonishing.

We agree.

Sorry if I was not clear. That's what I was saying.

"astonishingly consistent" = "the behavior is consistent, but I see how
if used in map it could be surprising".

It's also notable that chomp does return something already:

perldoc -f chomp
...
It returns the total number of characters removed from
all its arguments.

Also, I am not sure what was meant in the earlier response regarding:

>
> I don't care if trim() ends up in core or Scalar::Util but please,
> please, PLEASE, make the damn thing RETURN the trimmed arg. Honestly,
> I am about thisclose to putting in a PR to Scalar::Utils for a
> function called rchomp() - chomp that ALSO returns the chomped item.
>>
>> Good idea.
>>
>> If I remember, I'll pop that in when I'm next doing a release.
>>

A clarification on this would be appreciated.

Brett

>
>> So it seems the argument of "least astonishment" is necessary, but not
>> sufficient to cover all definitions of "astonishment".
>>
>> It's a pickle, for sure.
>>
>> Brett
>>
>>>
>>> I don't care if trim() ends up in core or Scalar::Util but please,
>>> please, PLEASE, make the damn thing RETURN the trimmed arg. Honestly,
>>> I am about thisclose to putting in a PR to Scalar::Utils for a
>>> function called rchomp() - chomp that ALSO returns the chomped item.
>>>
>>> Thank you.
>>>
>>> On Sat, Apr 3, 2021 at 4:54 PM Darren Duncan <darren@darrenduncan.net
>>> <mailto:darren@darrenduncan.net>> wrote:
>>>
>>>     On 2021-04-03 8:07 a.m., Nicholas Clark wrote:
>>>      > In the general case I'd also be concerned that there isn't an
>>>     obvious way to
>>>      > backport this such that CPAN code can depend on
>>>     "core-or-pollyfill". But as
>>>      > this one is a two-line implementation (for the efficient version)
>>>     I don't
>>>      > think that anyone will put in extra work to optionally use a core
>>>     trim in
>>>      > code published to CPAN.
>>>
>>>     There is a way that is obvious to me, which I had previously shared,
>>>     which is to
>>>     implement trim/etc soley as an addition to a dual-life module
>>> such as
>>>     Scalar::Util and include that version with the latest Perl.
>>>
>>>     Unless we decide to implement this as a modifier to regular
>>>     expression syntax,
>>>     its just a plain old routine and there's no reason it has to be any
>>>     more core
>>>     than a dual-life module.
>>>
>>>     -- Darren Duncan
>>>
>>>
>>>
>>> --
>>> Matthew O. Persico
>>
>
> 0--gg-
>