Back in Automated module install, I started to look into installing modules from local tarballs directly. It's more-or-less working now, but I really would like some extra eyes to code-review part of this because it's confusing as heck.
This snippet is intended to simply take a tarball name, extract the package and version numbers from it, and return true if the module is already installed and its version is at least the version given. I am absolutely unconcerned about speed or memory usage, only about correctness. Assume @INC is set up properly. That is, I'm installing to a local directory, and that directory is already in @INC. CODE refs that should be in @INC are there, etc.. (Actually, I'm not using any of those, but leaving the problem somewhat generic...).
Assume, further, that strict and warnings are enabled higher up in the script, since they are.
It seems to work with trivial tests so far, but as more tarballs are added to the mix, I just want to make sure it stays working without having to come back in here and modify anything, if I can help it.
=item is_module_installed Tries to guess whether a module is installed (yet) or not. =cut sub is_module_installed { my $module_name = shift; my $mod = $module_name; $mod =~ s/-([\d\.]+\d).*$//; my $min_ver = $1; (my $mod_name = $mod) =~ s/-/::/g; $mod =~ s/(?:-|::)/\//g; $mod .= '.pm'; eval { require $mod; 1} or return 0; if ($min_ver or scalar @_) { $min_ver ||= shift; $min_ver = normalise_version($min_ver); my $mod_ver = do { no strict 'refs'; normalise_version(${"${mod_name}::VERSION"}); }; if ($mod_ver and $mod_ver lt $min_ver) { return 0; } } 1 } sub normalise_version { my $version = shift; my @subs; if ($version =~ /\./) { @subs = split /\./, $version; } else { @subs = map { ord } split //, $version; } join '.', sprintf(join('', ("%03d")x@subs), @subs); }
The usage is something like this:
(That's not the exact code I'm using, but without the rest of the context, this is close enough.)my @tarballs = grep { not is_module_installed($_); } glob '*.tar*';
I'm open to any suggestions. Well, other than brace style. :-)
Thanks,
In reply to Checking module (of a level) being installed by Tanktalus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |