Skip to content

Instantly share code, notes, and snippets.

@ochaton
Created April 9, 2025 02:24
Show Gist options
  • Save ochaton/212cfce50baef32460e95493f90a8c36 to your computer and use it in GitHub Desktop.
Save ochaton/212cfce50baef32460e95493f90a8c36 to your computer and use it in GitHub Desktop.
Perl script which prints out which core modules are installed with their help
use strict;
use warnings;
use feature 'say';
use Module::CoreList;
use Pod::Simple::SimpleTree;
use List::Util qw(first);
# 1 - only standard level modules
# 2 - mostly all packages
# 3 - subpackages of packages
# 4 - internals of subpackages
my $LEVEL = 2;
BEGIN {
eval { require List::MoreUtils; List::MoreUtils->import('after'); 1; } or do {
*after = sub (&@) {
my $test = shift;
my $started;
my $lag;
grep $started ||= do { my $x = $lag; $lag = $test->(); $x }, @_;
}
}
}
sub _build_desc { join '', map { ref $_ ? _build_desc(@$_) : $_ } splice @_, 2 }
sub pod_name_section {
my ($path) = @_;
my $tree = Pod::Simple::SimpleTree->new->parse_file($path)->root;
my $node = first { 1 }
after { $_->[0] eq 'head1' and uc($_->[2]) eq 'NAME' }
grep { ref $_ eq 'ARRAY' } @$tree;
return '' unless $node && $node->[0] eq 'Para';
return _build_desc(@$node)
}
my @modules;
my %core2ver = %{ $Module::CoreList::version{$]} };
for my $mod (sort keys %core2ver) {
my $ver = $core2ver{$mod} or next;
(my $file = "$mod.pm") =~ s{::}{/}g;
next if $file =~ m{(/[^/]+){$LEVEL,}};
my $path = first { -e } map { "$_/$file" } @INC or next;
my $pod = $path =~ s/\.pm$/.pod/r;
my $desc = pod_name_section(-e $pod ? $pod : $path) or next;
push @modules, [$mod, $ver, $desc];
}
if (eval { require Term::Table; Term::Table->import; 1 }) {
my $table = Term::Table->new(
sanitize => 1,
header => [ 'Module', 'Version', 'Description' ],
rows => [ @modules ],
);
say $_ for $table->render;
} else {
printf "%-40s %-14s %s\n", @$_ for @modules;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment