blib/lib/Alien/Judy.pm

filter: Statementstmt 100% 62 / 62 Branchbran 50.0% 5 / 10 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 88.3% 83 / 94 CC 9 SCAR 9.3
CC
9
Cov
90%
_dl_load_libjudy
5.1
dl_load_libjudy
3.1
inc_dirs
2.0
CRAP 2.5
↑ Summary
1 # This is the Alien::Judy module, a way to ensure that users who don't
2 # natively have libJudy on their system can still get one. It provides
3 # libJudy.so and Judy.h at the path $Config{sitearch}/Alien/Judy.
4 package Alien::Judy;
5 # ABSTRACT: A wrapper for installing the Judy library
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use vars qw( $VERSION $DEBUG $HANDLE );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use DynaLoader ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # This module allows users to import its three public functions
16 # inc_dirs(), lib_dirs(), and dl_load_libjudy().
17 1 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 exports => [qw( inc_dirs lib_dirs dl_load_libjudy )]
19 1 };
20  
21 # The provided functions inc_dirs() and lib_dirs() are currently
22 # identical. Initially, they weren't.
23 1 *lib_dirs = \&inc_dirs;
24  
25 # TODO: add literate documentation
26 1 ▶sub inc_dirs {
Subroutine inc_dirs: called CC=2 SCAR=7.1
Pod documented
27 # Find files from ., $sitearch and @INC.
28 my @dirs =
29 13 ▶ grep { defined() && length() }
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
30 1 @Config::Config{qw(sitearchexp sitearch)},
31 @INC,
32 Cwd::getcwd();
33  
34 # But first try to find them in $_/Alien/Judy/
35 unshift @dirs,
36 1 map { File::Spec->catdir( $_, 'Alien', 'Judy' ) }
37 @dirs;
38  
39 # Return the unique-ified list
40 1 my %seen;
41 return
42 1 grep { ! $seen{$_}++ }
43 @dirs;
44 }
45  
46 # This module depends on libJudy from
47 # http://judy.sourceforge.net. Either I can find it as a
48 # system-installed library:
49 #
50 # apt-get install libjudydebian1 # for libJudy.so
51 # apt-get install libjudy-dev # for Judy.h
52 #
53 # Or I can get it by the perl CPAN module Alien::Judy which builds and
54 # installs Judy.h and libJudy.so into $Config{sitearch}/Alien/Judy.
55 #
56 # CPAN testers however won't have actually installed libJudy so I'll
57 # need to find it in @INC as set by $ENV{PERL5LIB} with a typical
58 # value of:
59 #
60 # $INC[...] = '/home/josh/.cpan/build/Alien-Judy-0.01/blib/arch'
61 #
62 # but the files Judy.h and libJudy.so are a couple levels deeper at:
63 #
64 # $INC[...] = '/home/josh/.cpan/build/Alien-Judy-0.01/blib/arch/Alien/Judy'
65 #
66 1 ▶sub _libjudy_candidates {
Subroutine _libjudy_candidates: called CC=2 SCAR=7.0
67 # Get a list of possible libJudy.so files.
68 #
69 # When writing this module, I found it would occasionally not only
70 # find libJudy.so but also blib/arch/Judy/Judy.so which is the
71 # Perl XS module. That was when this -lJudy resolving code was
72 # directly in the Judy cpan module though which has a lib/Judy.xs
73 # file. It's plausible that it's entirely irrelevant now that this
74 # is in Alien::Judy.
75 #
76 1 my @candidate_libs = DynaLoader::dl_findfile('-lJudy');
77 1 ▶ if ( $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Alien::Judy::DEBUG) 1 0 1
78 1 printf STDERR "candidates=@candidate_libs at %s line %d.\n", __FILE__, __LINE__;
79 }
80  
81 # I found that Solaris would find libJudy.so with DynaLoader but
82 # ld.so.1 when loading libJudy.so for Judy.pm would fail to find
83 # the right library to link against.
84 #
85 # I don't particularly understand it however what worked was to
86 # attempt to load libJudy.so.1 first.
87 my @dot_one =
88 1 grep { -f }
89 1 map { "$_.1" }
90 @candidate_libs;
91  
92 1 unshift @candidate_libs, @dot_one;
93  
94 1 return @candidate_libs;
95 }
96  
97 1 ▶sub _dl_load_libjudy {
Subroutine _dl_load_libjudy: called CC=5 SCAR=16.3
98 1 my @candidate_libs = @_;
99  
100 # The libJudy I find must provide the base functions from the
101 # libJudy library. This is to possibly skip "wrong" libJudy
102 # libraries.
103 # @DynaLoader::dl_require_symbols = 'Judy1Test';
104  
105 # Attempt to load each candidate until something succeeds. If one
106 # of the candidates happens to be the Perl XS module
107 # blib/arch/Judy/Judy.so then I'd like loading to keep trying and
108 # not fail. If I know how to predictably filter
109 # blib/arch/Judy/Judy.so out of this list I'd do that.
110 1 my $libjudy_loaded;
111 CANDIDATE_LIBRARY:
112 1 for my $libjudy_file ( @candidate_libs ) {
113 1 my $ok = eval {
114 1 $HANDLE = DynaLoader::dl_load_file( $libjudy_file, 0x01 );
115 1 1;
116 };
117 1 ▶ if ( $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Alien::Judy::DEBUG) 1 0 1
118 1 ▶ my $msgf =
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 1 0 1
119 $ok
120 ? "Loaded $libjudy_file at %s line %d.\n"
121 : "Couldn't load $libjudy_file: $@ at %s line %d.\n";
122 1 printf STDERR $msgf, __FILE__, __LINE__;
123 }
124  
125 1 ▶ if ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 1 0 1
126 1 $libjudy_loaded = 1;
127 1 last CANDIDATE_LIBRARY;
128 }
129 }
130  
131 1 return $libjudy_loaded;
132 }
133  
134 1 ▶sub dl_load_libjudy {
Subroutine dl_load_libjudy: called CC=3 SCAR=11.2
Pod documented
135 1 local @DynaLoader::dl_library_path = (
136 @DynaLoader::dl_library_path,
137 lib_dirs()
138 );
139  
140 # Enable DynaLoader debugging along with Judy debugging
141 1 local $DynaLoader::dl_debug = $DynaLoader::dl_debug;
142 1 ▶ if ( $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Alien::Judy::DEBUG) 1 0 1
143 1 ▶ $DynaLoader::dl_debug ||= 1;
Condition50%1/2
$DynaLoader::dl_debug ||= 1
l!l
01
MC/DC0%0/1
$DynaLoader::dl_debug ||= 1
$DynaLoader::dl_debug
Truth table50%1/2
A
A= $DynaLoader::dl_debug
Aresult
11
00
144 }
145  
146 1 my @libjudy_files = _libjudy_candidates();
147  
148 1 my $ok = _dl_load_libjudy( @libjudy_files );
149  
150 1 return $ok;
151 }
152  
153 1 $VERSION = '0.26';
154  
155 1 1;
↑ Summary