Perl Module(s) Of The Week - 2019 Week 02 - Module::CoreList

One of the issue when using Perl is the abundant list of Perl's modules. Finding the right modules can be overwhelming and sometimes frustrating. Duplicating, deprecating, or abandon modules is typical situation for a programming language which is 31 years old. Hence, which recommended Perl's module should you uses then? Start with the modules that came default with the Perl installation, the core list. How? By using the Module::CoreList module.

Installation and quick check of the module.
$ cpanm Module::CoreList
$ perl -e 'use Module::CoreList'

Now, let's write some code (`ex1.pl`) to list out all the core modules that came with Perl 5.29.6.
use strict;
use warnings;
use feature qw|say|;

use Module::CoreList;

say $_ foreach sort @{[Module::CoreList->find_modules(qr/.*/i, 5.029006)]};

Running the code and show the first ten results.
$ perl ex1.pl | head
Amiga::ARexx
Amiga::Exec
AnyDBM_File
App::Cpan
App::Prove
App::Prove::State
App::Prove::State::Result
App::Prove::State::Result::Test
Archive::Tar
Archive::Tar::Constant

So far so good. But do we need to write some code in order to make any query? No, there is a console utility, corelist that will do that. For corresponding command almost similar to our Perl's code before this.
$ corelist -v 5.29.6 | head

The following modules were in perl 5.29.6 CORE
Amiga::ARexx                                 0.04
Amiga::Exec                                  0.02
AnyDBM_File                                  1.01
App::Cpan                                    1.671
App::Prove                                   3.42
App::Prove::State                            3.42
App::Prove::State::Result                    3.42
App::Prove::State::Result::Test              3.42

Now some code reading. What interesting stuff can we learn from reading the source code of Module::CoreList?

(1) All the data, for example, Perl's version, starting from Perl 5 was hard-coded as large hash (see the `%released` hash).

(2) To prevent duplication, the changes (see `delta_from`, `changed`, and `removed` hash) of modules for each Perl version were stored as delta hash (see `%delta` hash).

(3) These use of `@_` pass through from one subroutine to another subroutine as shown below. See the `first_release_raw` subroutine.
sub a { say @_; }
sub b { &a; }
sub c { &a(); }
sub d { a(@_); }
sub e { &a(@_); }

b 1, 2, 3; # 123
c 1, 2, 3; # nothing is printed
d 1, 2, 3; # 123
e 1, 2, 3; # 123

(4) The END block is not a subroutine but a block of code that executed after all codes have been ran and before the Perl interpreter exited.

How about source code from corelist?

(1) *nix piping in Perl. See the example below.
    my @bundles =  map { $_->[0] }
                  sort { $b->[1] <=> $a->[1] }
                   map { [$_, numify_version($_)] }
                  grep { not /[^0-9.]/ }
                  keys %feature::feature_bundle;

No comments:

Post a Comment