cobbled this together some many moons ago, and have twiddled it into 
something I like at this point, although I'd like to further re-arrange 
it so that you can control the use of Storable via the switches as well, 
which I will work on depending on the level of interest. 

Any suggestions are more than welcome. I've thought about using perl's 
FORMAT for the output sections as well.

Excuse the somewhat bizzarre formatting. I have an 1152x870 display and 
tend to code wide, because I have the room to. :-) I've reformatted it 
somewhat to fit in the newsreader's posting conventions. 

#!/usr/bin/perl -w
# visualized and created by Scott R. Godin, 12/30/2000 
# last update May 8, 2001
# contact: webmaster (at) webdragon.net
######
## Command-line Options:
#  moduletest -r switch causes moduletest to ignore the storefile and 
#                recheck even if it exists
#  moduletest -f [filename] switch causes moduletest to write output to 
#                           that file, defaults to "ModuleCheck.txt"

## NOTE that the .stor file is currently dumped in whatever directory 
## you execute the command in, so under Unix, if you add it to your
## ~/bin/* you could concievably wind up with several .stor files lying 
## around if you're not careful. ;) I'll have to get around to fixing 
## that at some point, if it becomes necessary.

require 5.004;

use strict;
use vars qw/%opts/;
use Getopt::Std;
use Storable; #*** uncomment if you want to use Storable, and see below, 
              #    swapping comments as necessary
use File::Spec; 
use CPAN;# 
$| = 1;

BEGIN {
   getopts('rf:', \%opts);
}

# sets to 0 if you don't specify -f switch to have the output 
# redirected to a file.
use constant USE_SAVEFILE => exists($opts{'f'}) ? 1 : 0; 

my $savefile  = File::Spec->catfile( File::Spec->curdir(), 
                defined($opts{'f'}) ? $opts{'f'} : 'ModuleCheck.txt' );

my $storefile = File::Spec->catfile( File::Spec->curdir(),   
                'installed_modules.stor' );

sub checkmodule () 
{
    my %installed_modules = ();
    print "Checking Modules...Please stand by.. the cursor will spin for 
a minute or so.\n";
    print "GO get some coffee! You Deserve it! :-D\n";
    for my $mod (CPAN::Shell->expand("Module","/./"))
    {
        next unless $mod->inst_file;
        # "undef" = MakeMaker convention for undefined $VERSION:
        $installed_modules{ $mod->id } = 
          [ (($mod->inst_version eq "undef") ? '' : $mod->inst_version), 
            (($mod->cpan_version eq "undef") ? '' : $mod->cpan_version), 
          ];
    }

    store \%installed_modules, $storefile; # ***storable
    print "Done and stored.\n\n";
#    return \%installed_modules; # ***non-storable 
#(uncomment if you DON'T want storable) 
}

sub readstored (;$) 
{
#    my $ref = shift; # ***non-storable
    my $ref = retrieve($storefile) or die "$!"; # ***storable again
    my $last = '';
    
    # did this because not enough buffer space to display entire list in 
    # MacPerl. bleh.include for MacOS as there's no way to redirect
    # buffer output to a file, otherwise send to buffer and let user 
    # direct it to a file if they so choose, via ./moduletest -f [file] 
    # or usual redirect
    if (USE_SAVEFILE or $^O eq 'MacOS') 
    {
        print "Now retrieving Module data; output sent to $savefile\n";
        open STDOUT, ">$savefile" or die "$!";
    }
    
    print "Listing all Installed Modules and Versions where 
available...\n\n";

    foreach  (sort {uc($a) cmp uc($b)} keys %{$ref}) 
    {
         /^(\w+)\:{0,2}.*$/;
        my $this = $1;
        print  "-" x 87, "\n" if ($last ne $this);
        printf "Installed Module: %-35s", $_, ","; 
        printf " Version: %-9s", $ref->{$_}[0], ",";
        printf " CPAN: %-10s", $ref->{$_}[1];
        print  "\n";
        $last = $this;
    }
}

# if the storefile already exists, use it to save time unless -r 
# (recheck) switch is used  
if (-e $storefile and !$opts{'r'}) 
{
    print "Using existing .stor file for brevity.\n",
          "Either delete '$storefile' or re-run the script with the -r 
switch if you wish to update it.\n\n";
    readstored;
} 
else 
{
#    my $ref = checkmodule; # ***non-storable
#    readstored($ref); # ***non-storable
    checkmodule; # ***storable
    readstored; # ***storable
}

exit 0;

-- 
Scott R. Godin            | e-mail : [EMAIL PROTECTED]
Laughing Dragon Services  |    
It is not necessary to cc: me via e-mail unless you mean to speak off-group.
I read these via nntp.perl.org, so as to get the stuff OUT of my mailbox. :-)

Reply via email to