#!/usr/local/bin/perl use strict; use warnings; use File::Find qw(find); #TODO Some documentation. Any. #TODO use File::Find #TODO Clean this file. Right now it is functional but sloppy. unless (@ARGV) { print "Usage: pmversion [-e] \n"; print " -e : Edit the file(s) using \$EDITOR (defaults to vim)\n"; print " -* : Any other dash options will be passed as is to the editor. e.g. -o\n"; print "Examples: \n"; print " pmversion all\n"; print " pmversion CGI::Application\n"; print " pmversion CGI-Application-4.04.tar.gz\n"; print " pmversion -e CGI\n"; print " pmversion -e -o CGI CGI::Application\n"; } my @edit_args = (); my %options = (); @ARGV = grep { if ($_ !~ /^-(.+)/) {1} elsif ($_ eq '-e') {$options{edit}++; 0 } else {push @edit_args, $_; 0} } @ARGV; my %module_hash = (); my @module_list = (); my $output_all = 0; open STDERR, ">/dev/null"; if ($ARGV[0] eq 'all') { for my $path (@INC) { my $output = `find $path -name "*.pm" 2>&1`; for my $module (split/[\r\n]+/, $output) { chomp $module; $module =~ s/$path//; $module = get_module_name($module); next if exists $module_hash{$module}; $module_hash{$module} = $path; } } @module_list = sort keys %module_hash; $output_all = 1; } else { @module_list = map {get_module_name($_) } @ARGV; for my $module (@module_list) { for my $path (@INC) { my $file = $module; $file =~ s{::|-}{/}g; $file = "$path/$file.pm"; if ( -f $file ) { $module_hash{$module} = $path; last; } } } } for my $module (@module_list) { my $version = check_version($module); next if !defined $version; if ($options{edit}) { my $path = $module_hash{$module}; $module =~ s{::}{/}g; push @edit_args, "$path/$module.pm"; } else { printf qq{%-40s\t%10s\t%s\n}, $module, $version, $module_hash{$module}; } } if ($options{edit} && @edit_args) { my $editor = $ENV{EDITOR} || 'vim'; exec($editor, @edit_args); } sub get_module_name { my $orig = shift; my $module = $orig; if ($module =~ /^.+\/([-a-z]+(?:-\d)+)/i) { $module = $1 } if ($module =~ /^([-a-z]+)-\d/i) { $module = $1; $module =~ s/-/::/g; } $module =~ s{\/}{::}g; $module =~ s{^/|^::}{}; $module =~ s/\.pm$//g; #printf "%40s -> %s\n",$orig, $module; return $module; } sub check_version { my ($module) = @_; my $cmd = "use $module (); return \$$module\::VERSION;"; my $version = eval $cmd; if ($@) { return undef; } elsif ($version eq '') { $version = 'unknown'; } return $version; }