How do I get the Perl version string without license and build information?
Asked Answered
S

4

5

perl --version prints all this:

This is perl 5, version 26, subversion 1 (v5.26.1) built for darwin-thread-multi-2level

Copyright 1987-2017, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

What I want is this:

5.26.1

Is there any out-of-the-box way to get that, or do I need to write my own regular expression to extract it from the verbose output?

Stevestevedore answered 26/3, 2018 at 21:10 Comment(0)
C
12

The variable $^V has that information. You can print it directly from inside Perl:

#!/usr/bin/perl

use strict;
use warnings;

print $^V;

or you can get it in bash:

perl -e 'print $^V'

Both versions print "v5.22.1" on my system.

Calamite answered 26/3, 2018 at 21:24 Comment(0)
G
7
perl -e'print substr($^V, 1)'      # 5.10+

perl -MConfig -e'print $Config{version}'
Grayling answered 26/3, 2018 at 21:34 Comment(0)
D
7

In addition to $^V and $Config{version}, there is also $], which holds a numeric value of the version.

$ perl -MConfig -E 'say for $], $^V, $Config{version}'
5.016003
v5.16.3
5.16.3
Drogue answered 26/3, 2018 at 22:49 Comment(0)
D
0

In a shell, do:

$ perl -e 'printf "%vd\n", $^V;'
5.26.1

This is a little more backwards compatible compared to the accepted answer, since it works with perl versions back to v5.6.0 (instead of just back to v5.10.0). This also doesn't have a leading v in the output.

From https://perldoc.perl.org/perlvar#$%5EV:

"This variable first appeared in perl v5.6.0... Before perl v5.10.0 $^V was represented as a v-string rather than a version object."

"to portably convert $^V into its string representation, use sprintf()'s "%vd" conversion, which works for both v-strings or version objects:"

Direction answered 28/9, 2023 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.