blib/lib/ExtUtils/Typemaps/Cmd.pm

filter: Statementstmt 93.2% 55 / 59 Branchbran 65.0% 13 / 20 Conditioncond 50.0% 4 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 82.3% 84 / 102 CC 18 SCAR 16.7
CC
18
Cov
83%
__ANON__
8.8
embeddable_typemap
5.4
_intuit_typemap_source
5.0
CRAP 5.3
« blib/lib/ExtUtils/Typemaps/Type.pm ↑ Summary blib/lib/ExtUtils/Typemaps/InputMap.pm »
1 package ExtUtils::Typemaps::Cmd;
2 1 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '3.64';
6  
7 1 ▶use ExtUtils::Typemaps;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 our @ISA = qw(Exporter);
12 1 our @EXPORT = qw(embeddable_typemap);
13 1 our %EXPORT_TAGS = (all => \@EXPORT);
14  
15 5 ▶sub embeddable_typemap {
Subroutine embeddable_typemap: called CC=5 SCAR=16.8
Pod documented
16 5 my @tms = @_;
17  
18 # Get typemap objects
19 5 my @tm_objs = map [$_, _intuit_typemap_source($_)], @tms;
20  
21 # merge or short-circuit
22 5 my $final_tm;
23 5 ▶ if (@tm_objs == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tm_objs == 1) { } 3 2 5
24 # just one, merge would be pointless
25 3 $final_tm = shift(@tm_objs)->[1];
26 }
27 else {
28 # multiple, need merge
29 2 $final_tm = ExtUtils::Typemaps->new;
30 2 foreach my $other_tm (@tm_objs) {
31 2 my ($tm_ident, $tm_obj) = @$other_tm;
32 eval {
33 2 $final_tm->merge(typemap => $tm_obj);
34 2 1
35 2 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $final_tm->merge("typemap", $tm_obj); 1 } } or do { my $err = $@ || "Zombie error"; die "Failed to merge typ" } 2 0 2
36 0 ▶ my $err = $@ || 'Zombie error';
Condition0%0/2
$@ || 'Zombie error'
l!l
00
MC/DC0%0/1
$@ || 'Zombie error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
37 0 die "Failed to merge typ";
38 }
39 }
40 }
41  
42 # stringify for embedding
43 5 return $final_tm->as_embedded_typemap();
44 }
45  
46 4 ▶sub _load_module {
Subroutine _load_module: called CC=1 SCAR=0.0
47 4 my $name = shift;
48 4 return eval "require $name; 1";
49 }
50  
51 SCOPE: {
52 my %sources = (
53 4 ▶ module => sub {
Subroutine __ANON__: called CC=8 SCAR=21.7
54 4 my $ident = shift;
55 4 my $tm;
56 4 ▶ if (/::/) { # looks like FQ module name, try that first
Branch: 1 branch
BranchTrueFalseTotal
if (/::/) { } 2 2 4
57 2 foreach my $module ($ident, "ExtUtils::Typemaps::$ident") {
58 2 ▶ if (_load_module($module)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_load_module($module)) 2 0 2
59 2 ▶ eval { $tm = $module->new }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $tm = $module->new } } and return $tm 2 0 2
60 and return $tm;
61 }
62 }
63 }
64 else {
65 2 foreach my $module ("ExtUtils::Typemaps::$ident", "$ident") {
66 2 ▶ if (_load_module($module)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_load_module($module)) 2 0 2
67 2 ▶ eval { $tm = $module->new }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $tm = $module->new } } and return $tm 2 0 2
68 and return $tm;
69 }
70 }
71 }
72 0 return();
73 },
74 1 ▶ file => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
75 1 my $ident = shift;
76 1 ▶ return unless -e $ident and -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $ident and -r _ 0 1 1
Condition33%1/3
-e $ident and -r _
!ll&&!rl&&r
001
MC/DC0%0/2
-e $ident and -r _
-e $ident -r _
Truth table33%1/3
A and B
A= -e $identB= -r _
ABresult
0X0
100
111
77 1 return ExtUtils::Typemaps->new(file => $ident);
78 },
79 1 );
80 # Try to find typemap either from module or file
81 5 ▶ sub _intuit_typemap_source {
Subroutine _intuit_typemap_source: called CC=5 SCAR=16.2
82 5 my $identifier = shift;
83  
84 5 my @locate_attempts;
85 5 ▶ if ($identifier =~ /::/ || $identifier !~ /[^\w_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($identifier =~ /::/ or not $identifier =~ /[^\w_]/) { } 4 1 5
Condition100%3/3
$identifier =~ /::/ or not $identifier =~ /[^\w_]/
l!l&&r!l&&!r
221
MC/DC100%2/2
$identifier =~ /::/ or not $identifier =~ /[^\w_]/
$identifier =~ /::/ not $identifier =~ /[^\w_]/
Truth table100%3/3
A or not(B)
A= $identifier =~ /::/B= not $identifier =~ /[^\w_]/
ABresult
1X1
011
000
86 4 @locate_attempts = qw(module file);
87 }
88 else {
89 1 @locate_attempts = qw(file module);
90 }
91  
92 5 foreach my $source (@locate_attempts) {
93 5 my $tm = $sources{$source}->($identifier);
94 5 ▶ return $tm if defined $tm;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tm 5 0 5
95 }
96  
97 0 die "Unable to find typemap for '$identifier': "
98 . "Tried to load both as file or module and failed.\n";
99 }
100 } # end SCOPE
101  
102 =head1 NAME
103  
104 ExtUtils::Typemaps::Cmd - Quick commands for handling typemaps
105  
106 =head1 SYNOPSIS
107  
108 From XS:
109  
110 INCLUDE_COMMAND: $^X -MExtUtils::Typemaps::Cmd \
111 -e "print embeddable_typemap(q{Excommunicated})"
112  
113 Loads C<ExtUtils::Typemaps::Excommunicated>, instantiates an object,
114 and dumps it as an embeddable typemap for use directly in your XS file.
115  
116 =head1 DESCRIPTION
117  
118 This is a helper module for L<ExtUtils::Typemaps> for quick
119 one-liners, specifically for inclusion of shared typemaps
120 that live on CPAN into an XS file (see SYNOPSIS).
121  
122 For this reason, the following functions are exported by default:
123  
124 =head1 EXPORTED FUNCTIONS
125  
126 =head2 embeddable_typemap
127  
128 Given a list of identifiers, C<embeddable_typemap>
129 tries to load typemaps from a file of the given name(s),
130 or from a module that is an C<ExtUtils::Typemaps> subclass.
131  
132 Returns a string representation of the merged typemaps that can
133 be included verbatim into XS. Example:
134  
135 print embeddable_typemap(
136 "Excommunicated", "ExtUtils::Typemaps::Basic", "./typemap"
137 );
138  
139 This will try to load a module C<ExtUtils::Typemaps::Excommunicated>
140 and use it as an C<ExtUtils::Typemaps> subclass. If that fails, it'll
141 try loading C<Excommunicated> as a module, if that fails, it'll try to
142 read a file called F<Excommunicated>. It'll work similarly for the
143 second argument, but the third will be loaded as a file first.
144  
145 After loading all typemap files or modules, it will merge them in the
146 specified order and dump the result as an embeddable typemap.
147  
148 =head1 SEE ALSO
149  
150 L<ExtUtils::Typemaps>
151  
152 L<perlxs>
153  
154 =head1 AUTHOR
155  
156 Steffen Mueller C<<smueller@cpan.org>>
157  
158 =head1 COPYRIGHT & LICENSE
159  
160 Copyright 2012 Steffen Mueller
161  
162 This program is free software; you can redistribute it and/or
163 modify it under the same terms as Perl itself.
164  
165 =cut
166  
167 1 1;
168  
« blib/lib/ExtUtils/Typemaps/Type.pm ↑ Summary blib/lib/ExtUtils/Typemaps/InputMap.pm »