dump of module version information

wickline on 2002-07-10T14:03:07

mako132 was looking for a way to get version info for each module use'd by his script.

http://use.perl.org/user/mako132/journal/6244

I replied with a ~20 line END block which should do the trick. I thought I might find the code handy myself one day, so I thought I'd stash it in my journal as well for ease of finding. ((code at end of journal entry))

Maybe that's a use for this journal... post odd bits of code that don't have any associated project, invite any readers to critique/improve/ridicule said code, look at code years in the future when I can better do the same to it.

-matt (still deciding what to do with this journal thing)

END { no strict 'refs'; for my $k ( sort keys %INC ) { ( my $module = $k ) =~ s{[:/\\]}{::}g; $module =~ s{\.pm$}{}; my $version = ${"${module}::VERSION"}; if ( defined $version ) { $version = "\$${module}::VERSION = $version\n"; } else { # maybe they didn't use ALLCAPS my @found = ( map { defined ${"${module}::$_"} ? qq(\$${module}::$_ = ${"${module}::$_"}) : () } grep { m/^version$/i } keys %{"${module}::"} ); $version = !@found ? "($module version number not found)\n" : join( "\n", @found ); } # if you want, print this to a log file instead: print "$version $module loaded from $INC{$k}\n\n"; } }


Module version

IlyaM on 2002-08-13T11:06:04

You can use $module->VERSION instead of ${"${module}::VERSION"};. And yes, it handles the case when package uses lowercase name of version variable.

Re:Module version

wickline on 2002-08-14T19:52:10

That's handy to know :) ...and that makes things even tidier...
(code below not yet tested...could have typos, etc)
END {
    for my $k ( sort keys %INC ) {
        ( my $module = $k ) =~ s{[:/\\]}{::}g;
        $module =~ s{\.pm$}{};
        my $version = $module->VERSION;
        $version = defined $version
            ? "\$${module}::VERSION = $version\n";
            : "($module  version number not found)\n";
        # if you want, print this to a log file instead:
        print "$version $module  loaded from  $INC{$k}\n\n";
    }
}
... or maybe it needs to be defined and length instead of just defined. Guess I'll have to try it to see what gets returned when there is no version...
-matt

Re:Module version

IlyaM on 2002-08-15T08:32:07

END {
    for my $k ( sort keys %INC ) {
        ( my $module = $k ) =~ s{[:/\\]}{::}g;
        $module =~ s{\.pm$}{};
        my $version = $module->VERSION;
        $version = defined $version
            ? "\$${module}::VERSION = $version\n";
               ^^^^^^^^^^^^^^^^^^^^
replace it with $version
            : "($module  version number not found)\n";
        # if you want, print this to a log file instead:
        print "$version $module  loaded from  $INC{$k}\n\n";
    }
}

Ignore my port - I've misread your code

IlyaM on 2002-08-15T08:34:56

Ignore my post - I've misread your code.

Re:Module version

wickline on 2002-11-05T17:04:16

note to self: there's a stray semicolon there at the end of

        ? "\$${module}::VERSION = $version\n"

-matt