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:

my @tarballs = grep { not is_module_installed($_); } glob '*.tar*';
(That's not the exact code I'm using, but without the rest of the context, this is close enough.)

I'm open to any suggestions. Well, other than brace style. :-)

Thanks,


In reply to Checking module (of a level) being installed by Tanktalus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.