lib/Chj/constructorexporter.pm

filter: Statementstmt 95.5% 43 / 45 Branchbran 66.6% 4 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 88.8% 56 / 63 CC 6 SCAR 12.1
CC
6
Cov
91%
__ANON__
6.1
constructorexporter
1.0
BEGIN
1.0
CRAP 3.4
« lib/FP/Id.pm ↑ Summary lib/FP/Untainted.pm »
1 #
2 # Copyright (c) 2015 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 Chj::constructorexporter
13  
14 =head1 SYNOPSIS
15  
16 {
17 package Foo;
18 use Chj::constructorexporter;
19 *import = constructorexporter new => "Foo", new_from_string => "foo";
20 sub new { ... }
21 }
22 use Foo "foo", "foo"; # or ":all"; 'use Foo;' would not import anything
23 foo("abc") # calls Foo->new_from_string("abc")
24 Foo(1,2) # calls Foo->new(1,2)
25  
26 {
27 package Bar;
28 our @ISA = "Foo";
29 }
30 use Bar "foo"; # this exports a different "foo"!
31 foo("def") # calls Bar->new("def")
32  
33 # to import both (avoiding conflict):
34 use Foo qw(foo);
35 use Bar qw(foo -prefix bar_); # imports 'bar_foo'
36 # The position of the -prefix argument and its value within the
37 # import list is irrelevant.
38  
39 # Note that the exported constructor functions cannot be reached by
40 # full qualification: in this example Foo::foo is undefined (or it
41 # might instead be an unrelated method definition)!
42  
43  
44 =head1 DESCRIPTION
45  
46 This module might be evil: it helps writing OO modules that also
47 export functions. It only helps to export functions that are
48 constructors for the class in question, though, so its evilness might
49 be bounded.
50  
51 Subclasses that inherit (don't override) the import method will export
52 constructors for the subclass those are imported from. That might be
53 sensible or pure evil, the creator of this module isn't sure yet. If
54 you don't like this, either override 'import' in the subclass, or ask
55 for this to be changed.
56  
57 =head1 NOTE
58  
59 This is alpha software! Read the status section in the package README
60 or on the L<website|http://functional-perl.org/>.
61  
62 =cut
63  
64 package Chj::constructorexporter;
65 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 3 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 3 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 3 our @EXPORT = qw(constructorexporter);
71 3 our @EXPORT_OK = qw();
72 3 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
73  
74 3 ▶sub constructorexporter {
Subroutine constructorexporter: called CC=1 SCAR=0.0
Pod undocumented
75 3 my %exportdecl = @_;
76 6 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
77 6 my $class = shift;
78  
79 6 my ($all) = grep { $_ eq ":all" } @_;
80 6 my @rest = grep { $_ ne ":all" } @_;
81  
82 6 my $prefix = "";
83 6 my @names;
84 6 ▶ for (my $i = 0; $i < @rest; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @rest) 2 6 8
85 2 my $v = $rest[$i];
86 2 ▶ if ($v eq "-prefix") {
Branch: 1 branch
BranchTrueFalseTotal
if ($v eq '-prefix') { } 0 2 2
87 0 $i++;
88 0 $prefix = $rest[$i];
89 } else {
90 2 push @names, $v
91 }
92 }
93  
94 6 my $package = caller;
95  
96 my $exportdecl = +{
97 map {
98 6 my $methodname = $_;
99 6 my $exportname = $exportdecl{$methodname};
100 (
101 110 ▶ $exportname => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
102 110 $class->$methodname(@_)
103 }
104 )
105 6 } keys %exportdecl
106 };
107  
108 my $exports = (
109 $all ? $exportdecl : +{
110 map {
111 6 ▶ $_ => $$exportdecl{$_} // die "$_ not exported by $class"
Branch: 1 branch
BranchTrueFalseTotal
$all ? : 0 6 6
Condition50%1/2
$exportdecl->{$_} // die("$_ not exported by $class")
l!l
20
MC/DC0%0/1
$exportdecl->{$_} // die("$_ not exported by $class")
$exportdecl->{$_}
Truth table50%1/2
A
A= $exportdecl->{$_}
Aresult
11
00
112 } @names
113 }
114 );
115  
116 6 for my $name (keys %$exports) {
117 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
118 2 *{ $package . "::" . $prefix . $name } = $$exports{$name}
119 }
120 }
121 3 }
122  
123 3 1
« lib/FP/Id.pm ↑ Summary lib/FP/Untainted.pm »