From c1761c2546202c242a82af0eef9ded793facd6fa Mon Sep 17 00:00:00 2001 From: Nigel Horne Date: Fri, 25 Oct 2024 09:44:11 -0400 Subject: [PATCH] Optimize code for checking modules --- gedcom | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/gedcom b/gedcom index c6e7624..058179c 100755 --- a/gedcom +++ b/gedcom @@ -133,8 +133,8 @@ BEGIN { if(scalar(@needfull)) { my $list = join(' ', @needfull); print "Installing $list\n"; - # system("cpan -i $list"); $ENV{'PERL_MM_USE_DEFAULT'} = 1; + # system("cpan -i $list"); system('perl -MCPAN -e \'CPAN::Shell->install("' . join('", "', @needfull) . '")\''); } # $Devel::FIXME::REPAIR_INC = 1; @@ -160,26 +160,21 @@ BEGIN { }; } -foreach my $module(@needfull) { - my $version; - if($module =~ /(.+)\s(.+)$/) { - $module = $1; - $version = $2; - } - if($module =~ /.+\/(.+)/) { - $module = $1; - } - $module =~ s/\-/::/g; - $module =~ s/\.tar\.gz$//; - $module =~ s/::(\d)+$/ $1/; - eval "require $module"; - if($@) { - die $@; - } - $module->import(); - # if($version && ($module::VERSION < $version)) { - # die "$module: need $version got ", $module::VERSION; - # } +foreach my $module (@needfull) { + my ($name, $version) = $module =~ /(.+?)\s(.+)?$/; + $name ||= $module; + + # Extract module name, replacing hyphens with `::` and removing extensions + $name =~ s{.+/}{}; # Remove path if present + $name =~ s{-}{::}g; # Convert hyphens to `::` + $name =~ s{\.tar\.gz$}{}; # Remove file extension + $name =~ s{::(\d+)$}{ $1}; # Adjust version formatting if embedded in module name + + eval "require $name" or die $@; + $name->import(); + + # Uncomment to check version if needed + # die "$name: need $version, got " . $name->VERSION if $version && $name->VERSION < $version; } # use IPC::System::Simple;