File Coverage

blib/lib/ExtUtils/FindFunctions.pm
Criterion Covered Total %
statement 18 30 60.0
branch 4 8 50.0
condition 4 5 80.0
subroutine 5 5 100.0
pod 1 1 100.0
total 32 49 65.3


line stmt bran cond sub pod time code
1             package ExtUtils::FindFunctions;
2 2     2   21448 use strict;
  2         6  
  2         105  
3 2     2   13 use Carp;
  2         4  
  2         150  
4 2     2   12 use DynaLoader;
  2         7  
  2         60  
5             require Exporter;
6              
7 2     2   9 { no strict;
  2         3  
  2         815  
8             $VERSION = '0.02';
9             @ISA = qw(Exporter);
10             @EXPORT = qw(&have_functions);
11             }
12              
13             =head1 NAME
14              
15             ExtUtils::FindFunctions - Find functions in external libraries
16              
17             =head1 VERSION
18              
19             Version 0.02
20              
21             =head1 SYNOPSIS
22              
23             use ExtUtils::FindFunctions;
24              
25             my @check = qw(pcap_findalldevs pcap_open_dead pcap_setnonblock pcap_lib_version);
26             my @funcs = have_functions(libs => '-lpcap', funcs => \@check, return_as => 'array');
27              
28             =head1 DESCRIPTION
29              
30             This module provides the C function which can be used to
31             check if given functions are provided by an external library. Its aim is
32             to be used as an embedded library by F which needs such
33             facilities. Use the B command to embed it
34             in your distribution.
35              
36             =head1 EXPORT
37              
38             The module exports by default the C function.
39              
40             =head1 FUNCTIONS
41              
42             =head2 have_functions()
43              
44             Load the specified libraries and search for the given functions names.
45             The results are returned as an array or as an hash depending on the
46             C parameter.
47              
48             B
49              
50             =over
51              
52             =item *
53              
54             C - specify the libraries to load; this argument will be given
55             to C
56              
57             =item *
58              
59             C - a reference to the list of functions to search
60              
61             =item *
62              
63             C - specify the type of the result, either as an C
64             or as a C. As an array, only the functions found in the libraries
65             are returned. As a hash, the keys are the function names and their value
66             indicates if the function is present or not.
67              
68             =back
69              
70             =cut
71              
72             sub have_functions {
73 5     5 1 5264 my %args = @_;
74 5         10 my %funcs = ();
75              
76             # check params
77 5   66     559 defined $args{$_} or croak "error: Missing parameter '$_'.\n" for qw(libs funcs return_as);
78              
79 2   100     14 $args{return_as} ||= 'array';
80 2 100       181 $args{return_as} =~ /^(?:array|hash)$/
81             or croak "error: Incorrect value for parameter 'return_as'.\n";
82              
83 0           my @libs = ref $args{libs} eq '' ? $args{libs}
84 1 50       185 : ref $args{libs} eq 'ARRAY' ? @{$args{libs}}
    50          
85             : croak "error: Incorrect argument for parameter 'libs'.\n";
86              
87             # search for functions
88 0           for my $lib (@libs) {
89 0           my @paths = DynaLoader::dl_findfile($lib);
90              
91 0           for my $path (@paths) {
92 0           my $libref = DynaLoader::dl_load_file($path);
93              
94 0           for my $func (@{$args{funcs}}) {
  0            
95 0           my $symref = DynaLoader::dl_find_symbol($libref, $func);
96 0           $funcs{$func} = ! ! defined $symref;
97             }
98              
99 0           DynaLoader::dl_unload_file($libref);
100             }
101             }
102              
103 0           return $args{return_as} eq 'hash' ? %funcs
104 0 0         : grep { $funcs{$_} } sort keys %funcs;
105             }
106              
107              
108             =head1 AUTHOR
109              
110             SEbastien Aperghis-Tramoni, C<< >>
111              
112              
113             =head1 BUGS
114              
115             Please report any bugs or feature requests to
116             C, or through the web interface at
117             L.
118             I will be notified, and then you'll automatically be notified of progress on
119             your bug as I make changes.
120              
121              
122             =head1 SUPPORT
123              
124             You can find documentation for this module with the perldoc command.
125              
126             perldoc ExtUtils::FindFunctions
127              
128             You can also look for information at:
129              
130             =over
131              
132             =item * AnnoCPAN: Annotated CPAN documentation
133              
134             L
135              
136             =item * CPAN Ratings
137              
138             L
139              
140             =item * RT: CPAN's request tracker
141              
142             L
143              
144             =item * Search CPAN
145              
146             L
147              
148             =back
149              
150              
151             =head1 COPYRIGHT & LICENSE
152              
153             Copyright 2006 SEbastien Aperghis-Tramoni, all rights reserved.
154              
155             This program is free software; you can redistribute it and/or modify it
156             under the same terms as Perl itself.
157              
158             =cut
159              
160             1; # End of ExtUtils::FindFunctions