| Module-Extract-VERSION documentation | Contained in the Module-Extract-VERSION distribution. |
Module::Extract::VERSION - Extract a module version without running code
use Module::Extract::VERSION; my $version # just the version = Module::Extract::VERSION->parse_version_safely( $file ); my @version_info # extra info = Module::Extract::VERSION->parse_version_safely( $file );
This module lets you pull out of module source code the version number
for the module. It assumes that there is only one $VERSION
in the file.
Given a module file, return the module version. This works just like
mldistwatch in PAUSE. It looks for the single line that has the
$VERSION statement, extracts it, evals it, and returns the result.
In scalar context, it returns just the version as a string. In list context, it returns the list of:
sigil fully-qualified variable name version value file name line number of $VERSION
This code is in Github:
git://github.com/briandfoy/module-extract-version.git
brian d foy, <bdfoy@cpan.org>
I stole the some of this code from mldistwatch in the PAUSE
code by Andreas König, but I've moved most of it around.
Copyright (c) 2008-2011, brian d foy, All Rights Reserved.
You may redistribute this under the same terms as Perl itself.
| Module-Extract-VERSION documentation | Contained in the Module-Extract-VERSION distribution. |
package Module::Extract::VERSION; use strict; use warnings; no warnings; use subs qw(); use vars qw($VERSION); use Carp qw(carp); $VERSION = '1.01';
sub parse_version_safely # stolen from PAUSE's mldistwatch, but refactored { my $class = shift; my $file = shift; local $/ = "\n"; local $_; # don't mess with the $_ in the map calling this my $fh; unless( open $fh, "<", $file ) { carp( "Could not open file [$file]: $!\n" ); return; } my $in_pod = 0; my( $sigil, $var, $version, $line_number ); while( <$fh> ) { $line_number++; #print STDERR "Read: $_"; chomp; $in_pod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $in_pod; next if $in_pod || /^\s*#/; next unless /([\$*])(([\w\:\']*)\bVERSION)\b.*\=/; ( $sigil, $var ) = ( $1, $2 ); #print STDERR "Got $1 and $2\n"; $version = $class->_eval_version( $_, $sigil, $var ); last; } $line_number = undef if eof($fh) && ! defined( $version ); close $fh; return wantarray ? ( $sigil, $var, $version, $file, $line_number ) : $version; } sub _eval_version { my $class = shift; my( $line, $sigil, $var ) = @_; #print STDERR "_eval_version called with @_\n"; my $eval = qq{ package ExtUtils::MakeMaker::_version; local $sigil$var; \$$var=undef; do { $line }; \$$var }; my $version = do { local $^W = 0; no strict; eval( $eval ); }; #print STDERR "Version is $version\n"; return $version; }
1;