| 1 | 3 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Module::Extract::VERSION; | ||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Carp qw(carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | our $VERSION = '1.123'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =encoding utf8 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Module::Extract::VERSION - Extract a module version safely | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use Module::Extract::VERSION; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my $version # just the version | ||||||||||||||||||||||||||||
| 25 | = Module::Extract::VERSION->parse_version_safely( $file ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | my @version_info # extra info | ||||||||||||||||||||||||||||
| 28 | = Module::Extract::VERSION->parse_version_safely( $file ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This module lets you pull out of module source code the version number | ||||||||||||||||||||||||||||
| 33 | for the module. It assumes that there is only one C<$VERSION> | ||||||||||||||||||||||||||||
| 34 | in the file and the entire C<$VERSION> statement is on the same line. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head2 Class methods | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =over 4 | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =item $class->parse_version_safely( FILE ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Given a module file, return the module version. This works just like | ||||||||||||||||||||||||||||
| 45 | C<mldistwatch> in PAUSE. It looks for the single line that has the | ||||||||||||||||||||||||||||
| 46 | C<$VERSION> statement, extracts it, evals it in a Safe compartment, | ||||||||||||||||||||||||||||
| 47 | and returns the result. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | In scalar context, it returns just the version as a string. In list | ||||||||||||||||||||||||||||
| 50 | context, it returns the list of: | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sigil | ||||||||||||||||||||||||||||
| 53 | fully-qualified variable name | ||||||||||||||||||||||||||||
| 54 | version value | ||||||||||||||||||||||||||||
| 55 | file name | ||||||||||||||||||||||||||||
| 56 | line number of $VERSION | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | my( $version_pattern, $package_block_pattern); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 3 | $version_pattern = qr/ | |||||||||||||||||||||||||||
| 64 | (?<sigil> | ||||||||||||||||||||||||||||
| 65 | [\$*] | ||||||||||||||||||||||||||||
| 66 | ) | ||||||||||||||||||||||||||||
| 67 | (?<var> | ||||||||||||||||||||||||||||
| 68 | (?<package> | ||||||||||||||||||||||||||||
| 69 | [\w\:\']* | ||||||||||||||||||||||||||||
| 70 | ) | ||||||||||||||||||||||||||||
| 71 | \b | ||||||||||||||||||||||||||||
| 72 | VERSION | ||||||||||||||||||||||||||||
| 73 | ) | ||||||||||||||||||||||||||||
| 74 | \b | ||||||||||||||||||||||||||||
| 75 | .*? | ||||||||||||||||||||||||||||
| 76 | \= | ||||||||||||||||||||||||||||
| 77 | (?<rhs> | ||||||||||||||||||||||||||||
| 78 | .* | ||||||||||||||||||||||||||||
| 79 | ) | ||||||||||||||||||||||||||||
| 80 | /x; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | $package_block_pattern = qr/ | |||||||||||||||||||||||||||
| 83 | \b package \s+ | ||||||||||||||||||||||||||||
| 84 | (?<package> \w[\w\:\']* ) \s+ | ||||||||||||||||||||||||||||
| 85 | (?<rhs> \S+ ) \s* [;{] | ||||||||||||||||||||||||||||
| 86 | /x; | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 13 | ▶ | sub parse_version_safely { # stolen from PAUSE's mldistwatch, but refactored | ||||||||||||||||||||||||||
|
Subroutine
parse_version_safely: called CC=13 SCAR=26.6
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 13 | my( $class, $file ) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 13 | local $/ = "\n"; | |||||||||||||||||||||||||||
| 93 | 13 | local $_; # don't mess with the $_ in the map calling this | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 13 | my $fh; | |||||||||||||||||||||||||||
| 96 | 13 | ▶ | unless( open $fh, "<", $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | carp( "Could not open file [$file]: $!\n" ); | |||||||||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 13 | my $in_pod = 0; | |||||||||||||||||||||||||||
| 102 | 13 | my( $sigil, $var, $version, $line_number, $rhs ); | |||||||||||||||||||||||||||
| 103 | 13 | ▶ | LINE: while( <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 110 | $line_number++; | |||||||||||||||||||||||||||
| 105 | 110 | chomp; | |||||||||||||||||||||||||||
| 106 | 110 | ▶ | $in_pod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $in_pod; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 107 | 110 | ▶ | next if $in_pod || /^\s*#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$in_pod or /^\s*#/
MC/DC50%1/2
$in_pod or /^\s*#/
$in_pod
/^\s*#/
Truth table66%2/3
A or B
A= $in_podB= /^\s*#/
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # package NAMESPACE VERSION <-- we handle that | ||||||||||||||||||||||||||||
| 110 | # package NAMESPACE VERSION BLOCK | ||||||||||||||||||||||||||||
| 111 | 95 | ▶ | next unless( m/$version_pattern/ || m/$package_block_pattern/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
/$version_pattern/x or /$package_block_pattern/x
MC/DC100%2/2
/$version_pattern/x or /$package_block_pattern/x
/$version_pattern/x
/$package_block_pattern/x
Truth table100%3/3
A or B
A= /$version_pattern/xB= /$package_block_pattern/x
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 13 | ( $sigil, $var, $rhs ) = @+{ qw(sigil var rhs) }; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 13 | ▶ | if ($sigil) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 9 | $version = $class->_eval_version( $_, @+{ qw(sigil var rhs) } ); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||
| 119 | 4 | $version = $class->_eval_version( $_, '$', 'VERSION', qq('$rhs') ); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 13 | last LINE; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 13 | ▶ | $line_number = undef if eof($fh) && ! defined( $version ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
eof $fh and not defined $version
MC/DC0%0/2
eof $fh and not defined $version
eof $fh
not defined $version
Truth table66%2/3
A and not(B)
A= eof $fhB= not defined $version
| |||||||||||||||||||||||||||||
| 125 | 13 | close $fh; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | return wantarray ? | ||||||||||||||||||||||||||||
| 128 | 13 | ▶ | ( $sigil, $var, $version, $file, $line_number ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | : | ||||||||||||||||||||||||||||
| 130 | $version; | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 13 | ▶ | sub _eval_version { | ||||||||||||||||||||||||||
|
Subroutine
_eval_version: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 134 | 13 | my( $class, $line, $sigil, $var, $rhs ) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 13 | state $rc = require Safe; | |||||||||||||||||||||||||||
| 137 | 13 | state $rc = require version; | |||||||||||||||||||||||||||
| 138 | 13 | local $^W = 0; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 13 | my $s = Safe->new; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 13 | ▶ | if( $rhs =~ m/\h* qv \( \h* (['"]) ([0-9.]+) \1 \h* \)/x ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 143 | 3 | $rhs = qq('$2'); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | elsif($rhs =~ m/version->declare\( \s* (['"]) v? (?<v>.+) \1 \s* \)/x ) { | ||||||||||||||||||||||||||||
| 146 | 2 | $rhs = eval { sprintf q('%s'), version->declare("$+{'v'}") }; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 13 | ▶ | if( defined $Devel::Cover::VERSION ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 13 | $s->share_from('main', ['&Devel::Cover::use_file']); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 13 | my $command = '$VERSION = ' . $rhs; | |||||||||||||||||||||||||||
| 154 | 13 | $s->reval($command); | |||||||||||||||||||||||||||
| 155 | 13 | my $version = $s->reval('$VERSION'); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 13 | return $version; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =back | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 SOURCE AVAILABILITY | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | This code is in Github: | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | https://github.com/briandfoy/module-extract-version.git | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | brian d foy, C<< <briandfoy@pobox.com> >> | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | I stole the some of this code from C<mldistwatch> in the PAUSE | ||||||||||||||||||||||||||||
| 173 | code by Andreas König, but I've moved most of it around. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Andrey Starodubtsev added code to handle the v5.12 and v5.14 | ||||||||||||||||||||||||||||
| 176 | C<package> syntax. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Copyright © 2008-2026, brian d foy C<< <briandfoy@pobox.com> >>. All rights reserved. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | You may redistribute this under the Artistic License 2.0. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 3 | 1; | |||||||||||||||||||||||||||