Gentoo Archives: gentoo-dev

From: kentnl@g.o
To: gentoo-dev@l.g.o
Cc: perl@g.o, Kent Fredric <kentnl@g.o>
Subject: [gentoo-dev] [PATCH 3/6] perl-functions.eclass: add perl_get_module_version
Date: Tue, 24 Jan 2017 15:24:09
Message-Id: 20170124152201.15415-4-kentnl@gentoo.org
In Reply to: [gentoo-dev] [PATCH 0/6] perl-functions.eclass: new utility functions by kentnl@gentoo.org
1 From: Kent Fredric <kentnl@g.o>
2
3 This utility provides informational data describing the given module
4 names state of installation, either as a version, or as an error
5 message describing the grade of failure incurred in module loading.
6
7 It has the side effect that it most load the module (and its
8 dependencies) into memory to give a report value, and can be expensive
9 and have side-effects from Perl code execuring while the module loads,
10 including (but not limited to) people calling POSIX::_exit
11
12 This is the slowest way of inspecting state about a module, as
13 it must load the module
14 ---
15 eclass/perl-functions.eclass | 51 ++++++++++++++++++++++++++++++++++++++++++++
16 1 file changed, 51 insertions(+)
17
18 diff --git a/eclass/perl-functions.eclass b/eclass/perl-functions.eclass
19 index c3a0e1ee30..027cb0cf7e 100644
20 --- a/eclass/perl-functions.eclass
21 +++ b/eclass/perl-functions.eclass
22 @@ -380,3 +380,54 @@ perl_has_module_version() {
23 } ? 0 : 1 )' "$@"
24 }
25
26 +# @FUNCTION: perl_get_module_version
27 +# @USAGE: MODVER=$(perl_get_module_version "Test::Simple")
28 +# @DESCRIPTION:
29 +# Query the installed system perl to report the version of the installed
30 +# module.
31 +#
32 +# Note this should be strictly for diagnostic purposes to the end user,
33 +# and may be of selective use in pkg_info to enhance
34 +# emerge --info reports.
35 +#
36 +# Anything that does version comparisons **must not** use the return value
37 +# from this function
38 +#
39 +# Also note that this **must** at least attempt load the module in
40 +# question as part of its operation, and is subsequently prone to SLOWness.
41 +#
42 +# Return codes return error in both compilation-failure and not-installed cases.
43 +
44 +perl_get_module_version() {
45 + debug-print-function $FUNCNAME "$@"
46 +
47 + [[ $# -gt 0 ]] || die "${FUNCNAME}: No module name provided"
48 + [[ $# -lt 2 ]] || die "${FUNCNAME}: Too many parameters ($#)"
49 +
50 + if ! perl_has_module "$@" ; then
51 + echo "(Not Installed)";
52 + return 1;
53 + fi
54 +
55 + # Todo: What do we do if require fails? spew to stderr
56 + # or stay silent?
57 +
58 + perl -we 'my $mn = $ARGV[0];
59 + $mn =~ s{(::|\x{27})}{/}g;
60 + local $@;
61 + eval { require qq[${mn}.pm]; 1 } or do {
62 + print q[(Compilation failed in require)];
63 + exit 1;
64 + };
65 + my $stash = \%{ $ARGV[0] . q[::] };
66 + if ( not exists $stash->{VERSION} ) {
67 + print q[(No VERSION property)];
68 + exit 0;
69 + }
70 + if ( not defined ${$stash->{VERSION}} ) {
71 + print q[(undef)];
72 + exit 0;
73 + }
74 + print ${$stash->{VERSION}};
75 + exit 0; ' "$@"
76 +}
77 --
78 2.11.0