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

[Summary] Looking for someone to adopt adding trim() to core

Thread Next
From:
Christian Walde
Date:
February 15, 2021 18:42
Subject:
[Summary] Looking for someone to adopt adding trim() to core
Message ID:
op.0yvcwnsqydyjqt@xenwide.vpn.udag.de
After more discussion on IRC, another use case for trim that dmq was assuming but had not detailed, was found. As such a summary. First the use cases, then below proposed implementations:

#### 1 MODERN PERL ####

     eval "trim(2)";
     print 2 if $@;

     use v7;
     print trim(" 2 ");

     __END__
     2

This one is easy to describe the motivation for. Modern Perl code should automatically have a trim function built in, because right now there exists 1600+ clones of this function on CPAN alone, indicating a much larger need for it in DarkPAN.

dmq has given assent of having no objections to this, but doesn't care if it is not fulfilled.

Many people consider this to be the main goal.

#### 2 CPAN MODULE ####

     eval "trim(2)";
     print 2 if $@;

     use Backcompat::Trim;
     print trim(" 2 ");

     __END__
     2

This one is also relatively easy to describe. CPAN modules that want to work on all perl versions should be able to depend on a module that provides a trim implementation on old perl interpreters and loads core's implementation when available (to take advantage of e.g. unicode bug fixes).

dmq considers this to be important, but to my knowledge hasn't elaborated. (I may have overlooked something.)

Quite a few people consider this to be not very important, because CPAN authors could right now be adding the dependency Text::Trim to their code, but aren't; so it is questionable whether they would add Backcompat::Trim.

#### 3 DMQ MONKEYPATCH CODE ####

     package main {
         use MonkeyPatch::Trim;
     }

     package Work {
         print scalar::trim(" 2 ");
     }

     __END__
     2

This needs to work on all perl versions. dmq's reaction to case #2 described above was:

     18:04 <dmq> I dont want to write that

So he wants to be able to monkeypatch trim() into scalar::trim by loading a module one single time, and not needing to import it anywhere.

This is motivated by dmq having coworkers that have little experience with Perl, and by having thousands of systems with different perls.

Technically he could just monkeypatch trim in manually and go with that, but he also wants to have core trim available if it exists, in case of bugfixes to unicode etc.

#### 4 DMQ ONELINER ####

     # cat ???/sitecustomize.pl
     [...]
     use MonkeyPatch::Trim;
     [...]

     # perl -e 'print scalar::trim(" 2 ")'
     2

This one-ups #3.

Similar to it, he wants to use sitecustomize.pl to monkeypatch it in on older perls so it will work in oneliners as well across thousands of systems.

#############################################################
#############################################################
#############################################################

Implementation methods and questions:

#### CORE::trim ####

Fulfills #1:

This is the classical way. Put trim in as an experimental feature, automatically available as CORE::trim and importable first via `use feature 'trim';` and once tested and found for good as `use v7;`.

Fulfills #2:

Implementation for the backcompat module is easy:

     package Backport::Trim;
     use Exporter qw(import);
     our @EXPORT_OK=qw(trim);
     *trim= defined &CORE::trim ? \&CORE::trim : sub { ... };
     1;

Fulfills #3 maybe: ??? A ???
Fulfills #4 maybe: ??? A ???

There may be issues with this, but this should work for all Perls.

     package MonkeyPatch::Trim; # for sitecustomize.pl so oneliners can have scalar::trim
     *scalar::trim = defined &CORE::trim ? \&CORE::trim : sub { ... };
     1;

#### scalar::trim ####

Maybe fulfills #1: ??? B ???

Technically it is possible to get `use v7;` to export scalar::trim as trim, but currently nobody knows if so and how so and how much effort it would be.

Fulfills #2:

Implementation for the backcompat module is easy:

     package Backport::Trim;
     use Exporter qw(import);
     our @EXPORT_OK=qw(trim);
     *trim= defined &scalar::trim ? \&scalar::trim : sub { ... };
     1;

Fulfills #3:
Fulfills #4:

     package MonkeyPatch::Trim; # for sitecustomize.pl so oneliners can have scalar::trim
     *scalar::trim = sub { ... } if !defined &scalar::trim;
     1;

#############################################################
#############################################################
#############################################################

So, unless i made mistakes, and ignoring ideological concerns, the question are:

Does the proposed answer to question A work on a technical level?

Is the implementation of the answer to question B easy or maybe already done?



If the answer to A is "yes", then CORE::trim is the easiest way forward that gives everyone what they want.

If the answer to B is "yes", then scalar::trim is an easy way forward and also satisfies some ideological concerns.

-- 
With regards,
Christian Walde

Thread Next


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