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 1/6] perl-functions.eclass: Add perl_has_module
Date: Tue, 24 Jan 2017 15:23:09
Message-Id: 20170124152201.15415-2-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 is an incredibly fast way to check if Perl considers a module
4 of the given name installed in any capacity, including broken.
5
6 As long as "Foo.pm" is somewhere in @INC, `perl_has_module Foo` will
7 return true.
8
9 Even `perl_has_module threads` will return true on non-threaded perls,
10 due to that module still being present, and the module only fataling
11 when loaded.
12
13 Whereas `perl_has_module_version threads 0` will always fail on
14 non-threaded perls.
15 ---
16 eclass/perl-functions.eclass | 28 ++++++++++++++++++++++++++++
17 1 file changed, 28 insertions(+)
18
19 diff --git a/eclass/perl-functions.eclass b/eclass/perl-functions.eclass
20 index 1542b98cd4..10d19e859d 100644
21 --- a/eclass/perl-functions.eclass
22 +++ b/eclass/perl-functions.eclass
23 @@ -319,3 +319,31 @@ perl_doexamples() {
24
25 # is there a way to undo "docinto" ?
26 }
27 +
28 +# @FUNCTION: perl_has_module
29 +# @USAGE: perl_has_module "Test::Tester"
30 +# @DESCRIPTION:
31 +# Query the installed system Perl to see if a given module is installed.
32 +# This does **not** load the module in question, only anticipates if it *might* load.
33 +#
34 +# This is primarily for the purposes of dependency weakening so that conditional
35 +# behaviour can be triggered without adding dependencies to portage which would confuse
36 +# a dependency resolver.
37 +#
38 +# returns 'true' if the module is available, returns error if the module is not available
39 +
40 +perl_has_module() {
41 + debug-print-function $FUNCNAME "$@"
42 +
43 + [[ $# -gt 0 ]] || die "${FUNCNAME}: No module name provided"
44 + [[ $# -lt 2 ]] || die "${FUNCNAME}: Too many parameters ($#)"
45 +
46 + perl -we 'my $mn = $ARGV[0];
47 + $mn =~ s{(::|\x{27})}{/}g;
48 + for(@INC){
49 + next if ref $_;
50 + exit 0 if -r $_ . q[/] . $mn . q[.pm]
51 + }
52 + exit 1' "$@";
53 +}
54 +
55 --
56 2.11.0