Simple.pm

filter: Statementstmt 74.7% 83 / 111 Branchbran 42.8% 6 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 83.3% 25 / 30 Podpod n/a totaltotal 74.3% 119 / 160 CC 10 SCAR 7.7
CC
10
Cov
72%
__ANON__
6.0
__ANON__
5.5
_setup_moose_param_role_method_sugar_ext
5.5
CRAP 2.2
↑ Summary Simple/V1.pm »
1 2 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 # ABSTRACT: DWIM syntax extensions
4  
5 package Syntax::Feature::Simple;
6 {
7 2 $Syntax::Feature::Simple::VERSION = '0.002';
8 }
9 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 $Syntax::Feature::Simple::AUTHORITY = 'cpan:PHAYLON';
11 }
12  
13 2 ▶use Syntax::Feature::Function 0.001;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Syntax::Feature::Method 0.001;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Syntax::Feature::Sugar::Callbacks 0.002;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Sub::Install 0.925 qw( reinstall_sub );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use syntax qw( method );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 my $role_meta = 'MooseX::Role::Parameterized::Meta::Role::Parameterizable';
22  
23 2 ▶method install ($class: %args) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine install: called CC=3 SCAR=11.0
24 3 ▶ croak q{You cannot use 'simple' as a syntax extension. You need to }
Branch: 1 branch
BranchTrueFalseTotal
if $class eq __PACKAGE__ 1 2 3
25 . q{select a specific version, for example 'simple/v1'}
26 if $class eq __PACKAGE__;
27 2 my $target = $args{into};
28 2 my @extensions = $class->_available_extensions;
29 $class->_setup_extension($_, $target)
30 2 for @extensions;
31 2 return 1;
32 2 }
33  
34 2 ▶method _check_is_moose_param_role ($class: $target) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _check_is_moose_param_role: called CC=1 SCAR=0.0
35 4 return $class->_check_meta_isa($target, $role_meta);
36 2 }
37  
38 2 ▶method _check_has_meta ($class: $target) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.6
39 return $INC{'Moose.pm'}
40 0 ▶ ? do { require Moose::Util; Moose::Util::find_meta($target) }
Branch: 1 branch
BranchTrueFalseTotal
$INC{'Moose.pm'} ? : 0 7 7
41 : undef;
42 2 }
43  
44 2 ▶method _check_meta_isa ($class: $target, $check) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _check_meta_isa: called CC=2 SCAR=7.2
45 4 ▶ my $meta = $class->_check_has_meta($target)
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $class->_check_has_meta($target) or return undef 0 4 4
46 or return undef;
47 0 return $meta->isa($check);
48 2 }
49  
50 2 ▶method _setup_extension ($class: $extension, $target) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _setup_extension: called CC=3 SCAR=11.0
51 9 my $setup_method = "_setup_${extension}_ext";
52 9 my $check_method = "_can${setup_method}";
53 return undef
54 9 ▶ if $class->can($check_method)
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($check_method) and not $class->$check_method($target) 6 3 9
Condition100%3/3
$class->can($check_method) and not $class->$check_method($target)
!ll&&!rl&&r
216
MC/DC100%2/2
$class->can($check_method) and not $class->$check_method($target)
$class->can($check_method) not $class->$check_method($target)
Truth table100%3/3
A and not(B)
A= $class->can($check_method)B= not $class->$check_method($target)
ABresult
0X0
100
111
55 and not $class->$check_method($target);
56 3 return $class->$setup_method($target);
57 2 }
58  
59 0 ▶method _setup_moose_param_role_body_sugar_ext ($class: $target) {
Subroutine BEGIN: called CC=1 SCAR=3.5
Subroutine _setup_moose_param_role_body_sugar_ext: not called CC=1 SCAR=4.1
60 0 Syntax::Feature::Sugar::Callbacks->install(
61 into => $target,
62 options => {
63 -invocant => '',
64 -callbacks => {
65 role => {
66 -only_anon => 1,
67 -stmt => 1,
68 -default => ['$parameter'],
69 },
70 },
71 },
72 );
73 2 }
74  
75 2 ▶method _setup_function_keyword_ext ($class: $target) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _setup_function_keyword_ext: called CC=1 SCAR=0.0
76 2 Syntax::Feature::Function->install(
77 into => $target,
78 options => { -as => 'fun' },
79 );
80 2 return 1;
81 2 }
82  
83 0 ▶method _setup_moose_param_role_method_sugar_ext ($class: $target) {
Subroutine BEGIN: called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=2 SCAR=17.0
Subroutine _setup_moose_param_role_method_sugar_ext: not called CC=2 SCAR=17.0
84 0 ▶ my $orig = $target->can('method')
Branch: 2 branches
BranchTrueFalseTotal
my $orig = $target->can("method") or croak("There is no 'method' callback installed in '${target}'") 0 0 0
my $orig = $target->can("method") or croak("There is no 'method' callback installed in '${target}'") 0 0 0
85 or croak qq{There is no 'method' callback installed in '$target'};
86 reinstall_sub {
87 into => $target,
88 as => 'method',
89 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
90 0 ▶ return $_[0] if ref $_[0] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "CODE" 0 0 0
91 0 goto $orig;
92 },
93 0 };
94 0 Syntax::Feature::Sugar::Callbacks->install(
95 into => $target,
96 options => {
97 -invocant => '$self',
98 -callbacks => {
99 method => { -allow_anon => 1 },
100 },
101 },
102 );
103 0 return 1;
104 2 }
105  
106 2 ▶method _setup_method_keyword_ext ($class: $target) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
107 1 Syntax::Feature::Method->install(
108 into => $target,
109 options => { -as => 'method' },
110 );
111 1 return 1;
112 2 }
113  
114 0 ▶method _setup_modifier_sugar_ext ($class: $target) {
Subroutine BEGIN: called CC=1 SCAR=3.5
Subroutine __ANON__: not called CC=1 SCAR=4.6
115 0 Syntax::Feature::Sugar::Callbacks->install(
116 into => $target,
117 options => {
118 -invocant => '$self',
119 -callbacks => {
120 before => {},
121 after => {},
122 around => { -before => ['$orig'] },
123 },
124 },
125 );
126 0 return 1;
127 2 }
128  
129 2 1;
130  
131  
132  
133 =pod
134  
135 =head1 NAME
136  
137 Syntax::Feature::Simple - DWIM syntax extensions
138  
139 =head1 VERSION
140  
141 version 0.002
142  
143 =head1 DESCRIPTION
144  
145 This is a more of a syntax extension package than a simple extension by
146 itself. It will detect what kind of package it is imported into, and setup
147 appropriate syntax extensions depending on the type.
148  
149 =head2 Moose Classes and Roles
150  
151 If a L<Moose> class or role is detected, this extension will setup a C<fun>
152 keyword for function declarations, a C<method> keyword, and one keyword
153 each for C<before>, C<after> and C<around>.
154  
155 The modifiers behave exactly like normal method declarations, except for
156 C<around> which will provide the original method in a lexical named C<$orig>.
157  
158 package MyProject::MooseClassOrRole;
159 use Moose;
160 # or use Moose::Role
161 # or use MooseX::Role::Parameterized,
162 # but with body inside role { ... }
163 use syntax qw( simple/v2 );
164  
165 fun foo ($x) { ... }
166 my $anon_f = fun ($x) { ... };
167  
168 method bar ($x) { $self->say($x) }
169 my $anon_m = method ($x) { $self->say($x) };
170  
171 before baz ($x) { $self->say($x) }
172 after baz ($x) { $self->say($x) }
173 around baz ($x) { $self->say($self->$orig($x)) }
174  
175 1;
176  
177 In case of a L<parameterizable role|MooseX::Role::Parameterized> the right
178 callback will be called, but compatibility with anonymous method declarations
179 will be preserved:
180  
181 package MyProject::ParamRole;
182 use MooseX::Role::Parameterized;
183 use syntax qw( simple/v2 );
184  
185 parameter method_name => (is => 'ro');
186  
187 # defaults to $parameter
188 role ($param) {
189 my $name = $param->method_name;
190 method "$name" ($n) { $self->say($n) }
191 my $anon = method ($n) { $self->say($n) };
192 }
193  
194 1;
195  
196 As of L<version 2|Syntax::Feature::Simple::V2> you will also get sugar for
197 the C<role> body that allows you to specify a signature. By default, the
198 parameter object will be available in a variable named C<$parameter>.
199  
200 =head2 Plain Packages
201  
202 By default, if no other kind of package type is detected, C<simple/v1> will
203 only setup the function syntax, while C<simple/v2> will setup the function
204 and the method extension.
205  
206 package MyProject::Util;
207 use strictures 1;
208 use syntax qw( simple/v2 );
209  
210 fun foo ($x) { ... }
211 my $anon_f = fun ($x) { ... };
212  
213 method bar ($class: $x, $y) { ... }
214 my $anon_m = method ($x) { ... };
215  
216 1;
217  
218 =head1 FUTURE CANDIDATES
219  
220 =head2 C<simple/v*> (basic set)
221  
222 =over
223  
224 =item * C<no indirect>
225  
226 =item * C<use true>
227  
228 =item * L<Try::Tiny>
229  
230 =back
231  
232 =head2 C<simple/x*> (extended set)
233  
234 =over
235  
236 =item * L<Smart::Match> if a valid Perl version was declared
237  
238 =back
239  
240 =head1 SEE ALSO
241  
242 =over
243  
244 =item L<Syntax::Feature::Simple::V1>
245  
246 Version 1 of the extension set.
247  
248 =item L<Syntax::Feature::Simple::V2>
249  
250 Version 2 of the extension set.
251  
252 =item L<syntax>
253  
254 The syntax dispatching module.
255  
256 =item L<Syntax::Feature::Simple>
257  
258 Contains general information about this extension.
259  
260 =item L<Syntax::Feature::Method>
261  
262 Specifics about the C<method> and modifier keywords.
263  
264 =item L<Syntax::Feature::Function>
265  
266 Specifics about the C<fun> function keyword.
267  
268 =item L<Moose>
269  
270 Post-modern object-orientation.
271  
272 =item L<MooseX::Role::Parameterized>
273  
274 Parameterizable roles for L<Moose>.
275  
276 =back
277  
278 =head1 BUGS
279  
280 Please report any bugs or feature requests to bug-syntax-feature-simple@rt.cpan.org or through the web interface at:
281 http://rt.cpan.org/Public/Dist/Display.html?Name=Syntax-Feature-Simple
282  
283 =head1 AUTHOR
284  
285 Robert 'phaylon' Sedlacek <rs@474.at>
286  
287 =head1 COPYRIGHT AND LICENSE
288  
289 This software is copyright (c) 2011 by Robert 'phaylon' Sedlacek.
290  
291 This is free software; you can redistribute it and/or modify it under
292 the same terms as the Perl 5 programming language system itself.
293  
294 =cut
295  
296  
297 __END__
↑ Summary Simple/V1.pm »