After reading existing PERL modules?, I decided to write a code to list all available modules without a dependence on any particular module.

#!/usr/bin/perl -w use strict; foreach (sort { lc($a) cmp lc($b) } map { `cd $_ && find . -name '*.pm +'` } @INC) { s/^\.\/(.*)\.pm$/$1/; s/\//::/g; print; }

I grant the above code isn't particularly obfuscated, but I was attempting to produce a minimalist one-liner. It ended up taking me the better half of a day, but I learned quite a bit about perl while trying to reduce my code.

Can someone make it even shorter still (preferably with an accompanying explanation if obfuscation warrants) so that I may learn from the code of my fellow monks of this hallowed monastery.

Replies are listed 'Best First'.
Re: Sorted List of Available Modules
by Jaap (Curate) on Apr 17, 2005 at 17:42 UTC
    Here's a cross-platform version that uses the File::Find core module:
    use File::Find; my @m; find(sub {if ($File::Find::name =~ m/^\Q$File::Find::topdir\E\/(.+)\.p +m$/i) { $_ = $1; s/\//::/g; push (@m, $_); }}, @INC); $, = "\n"; print sort @m;
    It's not very compact but that's mainly because of File::Find's annoying subref way of working. If you don't need the sorting, it can be made quite a bit more compact.