Enterprise Linux Log

Oct 11 2007   2:41PM GMT

Script tracks Perl modules for you



Posted by: ITKE
Tags:
Linux basics
scripts

Michael Hurley shares a script that he wrote called modlister. I’ll let him explain:

It’s a script to tell you what Perl modules you have installed and where, to query whether you have a particular module installed, to see associated files, etc. For example:

    1. List all installed modules:
  1. modlister.pl

  2. Only show filenames (strip directories):
  3. modlister.pl -f

  4. See if Compress::Zlib is installed:
  5. modlister.pl -m Zlib

  6. See all the files associated with Zlib:
  7. modlister.pl -m Zlib -a

Thanks for the script, Michael.

Try this one out yourself. Tell us what you think or submit one of your own. If we use your script, you will receive a gift a Starbucks gift certificate. More scripting goodness after the jump…

#!/usr/bin/perl    

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

## modlister.pl    

##    

## Purpose:     List installed perl modules    

## Author:      Michael Hurley    

##              Copyright 2006    

##    

## modlister.pl is free software. you can redistribute it    

## and/or modify it under the terms of the GNU General Public    

## License as published by the Free Software Foundation;    

## either version 2, or (at your option) any later version.    

##    

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

use strict;    

use warnings;    

use File::Find;    

use Getopt::Std;    

use vars qw/%opt/;    

my $optstr = 'm:fah';    

&getopts("$optstr", %opt);    

&usage if $opt{h};    

my (%m, @mods);    

find(&wanted, @INC);    

if ($opt{m}) {    

    @mods = grep {/$opt{m}/} keys %m;    

} else {    

    @mods = keys %m;    

}    

for (@mods) {    

    print "$_n";    

}    

sub wanted {    

    if ($opt{f} && !$opt{a}) {    

        $m{$_} = 1 if /.pm$/;    

    } elsif ($opt{a} && !$opt{f}) {    

        $m{$File::Find::name} = 1;    

    } elsif ($opt{f} && $opt{a}) {    

        $m{$_} = 1;    

    } elsif (/.pm$/) {    

        $m{$File::Find::name} = 1;    

    }    

}    
 sub usage {
    my $msg = <<EoMSG
Usage: $0 [-m <arg>] [-f] [-a]
By default, $0 prints the full path for every   

.pm file in @INC. You can modify the behavior    

with these flags:    

    -m   List modules that match     

    -f        List filenames only (not the full path)    

    -a        List all files in @INC, not just .pm files    

    -h        Print this help message    

EoMSG    

    ;    

    die $msg;    

}    

__END__    

=head1 NAME    

modlister.pl - List installed perl modules    

=head1 SYNOPSIS    

modlister.pl [-m 
] [-f] [-a]    

=head1 DESCRIPTION    

modlister.pl prints installed perl modules. By default, modlister.pl    

will print the full path of any files in @INC that end in F<.pm>.    

Its behavior can be modified with these flags:    

=over 4    

=item B<-m> I    

Only print strings that match I 
.    

=item B<-f>    

Only print filenames; exclude directories.    

=item B<-a>    

Print all files in the directory, not just F<.pm> files.    

=back    

=head1 AUTHOR    

Michael Hurley    

=head1 LICENSE    

Copyright 2006 Michael Hurley.    

This is free software. You can redistribute it and/or modify    

it under the terms of the GNU General Public License as    

published by the Free Software Foundation; either version 2,    

or (at your option) any later version.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: