blib/lib/FindApp/Utils/Package.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 32 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 182 CC 2 SCAR 8.8
CC
2
Cov
0%
stashes
6.0
export_ok
2.0
export_function
2.0
CRAP 2.4
1 package FindApp::Utils::Package;
2  
3 use v5.10;
4 use strict;
5 use warnings;
6  
7 use Carp qw(carp cluck confess croak);
8 use FindApp::Utils::Assert qw(:all);
9 use FindApp::Utils::Debug qw(:all);
10 use FindApp::Utils::Syntax qw(:all);
11  
12 # Chicken-and-egg scrambles subuse "Object";
13 use FindApp::Utils::Package::Object <{UN,}PACKAGE>;
14  
15 sub export_alias ( $$ ) ;
16 sub export_function ( $$ ) ;
17 sub export_ok ;
18 sub sort_packages_lexically ;
19 sub sort_packages_numerically ;
20 sub stash ( _ ) ;
21 sub stashes ;
22 sub subs_in ;
23  
24 #################################################################
25  
26 use Exporter qw(import);
27 our $VERSION = v1.0;
28 our @EXPORT = <{,UN}PACKAGE>;
29 our @EXPORT_OK = (
30 <{,UN}PACKAGE>,
31 qw(
32 stashes
33 subs_in
34 ),
35 <sort_packages{,_{lex,numer}ically}>,
36 );
37  
38 our %EXPORT_TAGS = (
39 all => \@EXPORT_OK,
40 );
41  
42 sub export_ok {
43 push @EXPORT_OK, @_;
44 }
45  
46 sub export_function($$) {
47 my($name, $code) = @_;
48 function($name, $code);
49 export_ok($name);
50 }
51  
52 sub export_alias($$) {
53 my($name, $code) = @_;
54 no strict "refs";
55 *$name = $code;
56 export_ok($name);
57 }
58  
59 sub sort_packages_lexically {
60 wantarray || croak "need list context for sorting";
61 map { UNPACKAGE }
62 sort #{ $a cmp $b } # already default
63 map { PACKAGE($_) }
64 @_ ;
65 }
66  
67 export_alias sort_packages => \&sort_packages_lexically;
68  
69 sub sort_packages_numerically {
70 wantarray || croak "need list context for sorting";
71 map { UNPACKAGE }
72 sort { $a <=> $b }
73 map { PACKAGE($_) }
74 @_;
75 }
76  
77  
78 sub stash(_) { PACKAGE(shift)->stash };
79  
80 sub stashes {
81 my @stashes = map { stash } @_;
82 return wantarray ? @stashes : $stashes[0];
83 };
84  
85 sub subs_in {
86 my %seen;
87 for my $stash (stashes @_) {
88 while (my($name, $glob) = each %$stash) {
89 ref \$glob eq "GLOB" && # weird things find their way into the stashes
90 *$glob{CODE} &&
91 $seen{$name}++
92 ## && warn "already saw $name";
93 }
94 }
95 return sort keys(%seen);
96 }
97  
98 my %prefix_methods = qw(
99 sib super
100 sub self
101 top left
102 );
103  
104 while (my($prefix, $SUBMETH) = each %prefix_methods) {
105 # BUILD: subpackage, sibpackage, toppackage
106 export_function "${prefix}package" => sub { &ENTER_TRACE;
107 wantarray // croak "useless use of ${prefix}package in void context";
108 croak "need list context for multiple arguments" if @_ > 1 && !wantarray;
109 croak "need arguments for list context" if @_ == 0 && wantarray;
110 @_ = ("") unless @_;
111 my @packages = PACKAGE(caller)->$SUBMETH->add_all_unblessed(@_);
112 return wantarray ? @packages : $packages[0];
113 };
114 # BUILD: subuse, sibuse, topuse
115 export_function "${prefix}use" => sub { &ENTER_TRACE;
116 my($pack, @imports) = @_;
117 my $caller = PACKAGE(caller);
118 my $package = $caller->$SUBMETH->add($pack);
119 my $import = "";
120 if (@imports) {
121 $import = (@imports == 1 && !defined $imports[0])
122 ? "()" # hack to allow a null import list
123 : "qw(@imports)"
124 }
125 eval qq{ package $caller; use $package $import; 1 } || die;
126 };
127 }
128  
129 # The with behaviors have to come first so that the
130 # base class doesn't have to know they exist, since
131 # a new one may override something that doesn't know
132 # it's being overridden.
133 export_function implements => sub { &ENTER_TRACE;
134 return @_ => <Class State Behavior>;
135 };
136  
137 export_function implementation => sub { &ENTER_TRACE;
138 wantarray || panic "need list context for implementation";
139 PACKAGE(caller)->add_all_unblessed(&implements);
140 };
141  
142 export_function with => sub { &ENTER_TRACE;
143 wantarray || panic "need list context for role loading";
144 @_ || panic "with what?";
145 PACKAGE("Behavior")->add_all_unblessed(@_);
146 };
147  
148 1;
149  
150 ################################################################
151  
152 ################################################################
153  
154 =encoding utf8
155  
156 =head1 NAME
157  
158 FindApp::Utils::Package - FIXME
159  
160 =head1 SYNOPSIS
161  
162 use FindApp::Utils::Package;
163  
164 =head1 DESCRIPTION
165  
166 =head2 Public Methods
167  
168 =over
169  
170 =item FIXME
171  
172 =back
173  
174 =head2 Exports
175  
176 =over
177  
178 =item export_alias
179  
180 =item export_function
181  
182 =item export_ok
183  
184 =item implementation
185  
186 =item implements
187  
188 =item sibpackage
189  
190 =item sibuse
191  
192 =item sort_packages
193  
194 =item sort_packages_lexically
195  
196 =item sort_packages_numerically
197  
198 =item stash
199  
200 =item stashes
201  
202 =item subs_in
203  
204 =item subpackage
205  
206 =item subuse
207  
208 =item toppackage
209  
210 =item topuse
211  
212 =item with
213  
214 =back
215  
216 =head1 EXAMPLES
217  
218 =head1 ENVIRONMENT
219  
220 =head1 SEE ALSO
221  
222 =over
223  
224 =item L<FindApp>
225  
226 =back
227  
228 =head1 CAVEATS AND PROVISOS
229  
230 =head1 BUGS AND LIMITATIONS
231  
232 =head1 HISTORY
233  
234 =head1 AUTHOR
235  
236 Tom Christiansen << <tchrist@perl.com> >>
237  
238 =head1 LICENCE AND COPYRIGHT
239  
240 Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>.
241 All Rights Reserved.
242  
243 This module is free software; you can redistribute it and/or
244 modify it under the same terms as Perl itself. See L<perlartistic>.
245  
246 =cut
247