DEV Community

Cover image for Browse CPAN modules per namespaces
Tib
Tib

Posted on • Edited on

Browse CPAN modules per namespaces

I once was looking to filter out CPAN modules per namespace. Namespaces generally have meaning in CPAN since there are conventions and Perl folks try to be rigourous and to fit well in the existing naming

So back again to my research, I was looking for a way to filter out CPAN modules per namespaces.

Become the master of MetaCPAN search

From METACPAN faq you can get some tips like the module: prefix
MetaCPAN tips

With this trick, you can search for instance all XML related modules

It works well, but does not achieve exactly what I want since it returns me also for instance the SVG::XML which is not actually prefixed by XML::
Problem

CPANMeta the great

By asking on the good old IRC, I get a solution thanks to CPANMeta

You can then list all XML::* modules
CPANMeta

Manual grep

The quick and dirty hack!

I downloaded the 02packages.details.txt file from CPAN.org

Then using a grep

grep "^XML::" 02packages.details.txt
Enter fullscreen mode Exit fullscreen mode

And the output is a long list of modules:

XML::XSS                         v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::Comment                v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::Document               v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::Element                v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::ProcessingInstruction  v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::Role::Renderer         v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::Role::StyleAttribute   v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::StyleAttribute         v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::Stylesheet::HTML2TD    v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::Template               v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XSS::Text                   v0.3.5  Y/YA/YANICK/XML-XSS-0.3.5.tar.gz
XML::XUpdate::LibXML           0.006000  P/PA/PAJAS/XML-XUpdate-LibXML-0.6.0.tar.gz
XML::YYLex                         0.04  B/BO/BOEWE/XML-YYLex-0.04.tar.gz
Enter fullscreen mode Exit fullscreen mode

More experiments

I tried also to use CPAN::02Packages::Search but it seems to only allow exact match.

Look at this working code

#!/usr/bin/env perl

use CPAN::02Packages::Search;
use Data::Dumper;

my $index = CPAN::02Packages::Search->new(file => './02packages.details.txt');
my $res = $index->search('Plack'); 

print Dumper($res);
Enter fullscreen mode Exit fullscreen mode

I tried to tweak it with $index->search('XML::*'); or $index->search('XML::.*'); but it is not valid 😕

CPAN::Common::Index::Mirror is promising to do the job according to the documentation
Index

But I was not able to make it work at first 😃

I started with this code snippet

#!/usr/bin/env perl

use CPAN::Common::Index::Mux::Ordered;
use Data::Dumper;

my $index = CPAN::Common::Index::Mux::Ordered->assemble(
    MetaDB => {},
    Mirror => { mirror => "http://cpan.cpantesters.org" },
);

my $result = $index->search_packages( { package => 'XML::LibXML' } );

print Dumper($result);
Enter fullscreen mode Exit fullscreen mode

And tried various XML::* and XML::.* and /^XML::.*/ and m/^XML::.*/ and the same with - instead of :: but no way 😬

Thanks to @grinnz , I get it!

#!/usr/bin/env perl

use CPAN::Common::Index::Mux::Ordered;
use Data::Dumper;

my $index = CPAN::Common::Index::Mux::Ordered->assemble(
    MetaDB => {},
    Mirror => { mirror => "http://cpan.cpantesters.org" },
);

my @result = $index->search_packages( { package => qr/^XML::/ });

print Dumper(@result);
Enter fullscreen mode Exit fullscreen mode

That will returns several packages:

$VAR2326 = {
             'version' => 'undef',
             'package' => 'XML::XSH::Parser',
             'uri' => 'cpan:///distfile/CHOROBA/XML-XSH-1.8.6.tar.gz'
           };
$VAR2327 = {
             'version' => '0.48',
             'package' => 'XML::XSLT',
             'uri' => 'cpan:///distfile/JSTOWE/XML-XSLT-0.48.tar.gz'
           };
$VAR2328 = {
             'package' => 'XML::XSLT::DOM::TextDOE',
             'version' => '0.31',
             'uri' => 'cpan:///distfile/MAHEX/XML-XSLT-0.31.tar.gz'
           };
Enter fullscreen mode Exit fullscreen mode

I see there are also some other possible alternatives like App::CPANIDX or maybe CPANDB?

MetaCPAN native treeview

I wonder if it should not simply be part of MetaCPAN. I other words a native tree view in MetaCPAN 💃

Maybe I'm missing a point, but a feature like this would make me happy for a while.

What about something like
CPAN treeview

What do you think?

Top comments (7)

Collapse
 
grinnz profile image
Dan

The common way to pass a regular expression to a function is with the qr operator, so it probably expects something like package => qr/^XML::/.

Collapse
 
thibaultduponchelle profile image
Tib

Thank you a lot, it works like a charm and I updated the post with:

my @result = $index->search_packages( { package => qr/^XML::/ });
Enter fullscreen mode Exit fullscreen mode
Collapse
 
szabgab profile image
Gabor Szabo

That treeview might be a nice addition, but I think if you are interested in having it you'll have to do it yourself. In any case check out the list of open issues here: github.com/metacpan/metacpan-web/i...

Collapse
 
thibaultduponchelle profile image
Tib

Yes, I imagine I would have to do it myself... But my first interrogation is about to know if it made sense for anybody else than me.

Collapse
 
billcosta profile image
Bill Costa

Glad to see it wasn't just me. I've been trying to do this for a while now and in desperation also did the same quick and dirty hack myself, all the while thinking that there is a proper way to do it but I was just too stupid to find it. My motivation is wanting to contribute my own modules and wanting to figure out the best and most logical place for the module to live in the existing CPAN namespace.

Collapse
 
thibaultduponchelle profile image
Tib

Happy to hear you had the same need. My use case was to list all Alien:: modules that are kind of special.

Collapse
 
szabgab profile image
Gabor Szabo

BTW the special searches are listed in the FAQ, I just opened an issue to link to that entry: github.com/metacpan/metacpan-web/i... please comment on it.