File Coverage

blib/lib/Syntax/Feature/Simple.pm
Criterion Covered Total %
statement 69 90 76.6
branch 6 12 50.0
condition 3 3 100.0
subroutine 25 29 86.2
pod n/a
total 103 134 76.8


line stmt bran cond sub pod time code
1 2     2   1680 use strictures 1;
  2         15  
  2         111  
2              
3             # ABSTRACT: DWIM syntax extensions
4              
5             package Syntax::Feature::Simple;
6             {
7             $Syntax::Feature::Simple::VERSION = '0.002';
8             }
9             BEGIN {
10 2     2   301 $Syntax::Feature::Simple::AUTHORITY = 'cpan:PHAYLON';
11             }
12              
13 2     2   1620 use Syntax::Feature::Function 0.001;
  2         11757  
  2         58  
14 2     2   1397 use Syntax::Feature::Method 0.001;
  2         36136  
  2         188  
15 2     2   2359 use Syntax::Feature::Sugar::Callbacks 0.002;
  2         31177  
  2         78  
16              
17 2     2   20 use Carp qw( croak );
  2         6  
  2         138  
18 2     2   15 use Sub::Install 0.925 qw( reinstall_sub );
  2         39  
  2         19  
19 2     2   456 use syntax qw( method );
  2         13  
  2         22  
20              
21             my $role_meta = 'MooseX::Role::Parameterized::Meta::Role::Parameterizable';
22              
23 2     2   2431 method install ($class: %args) {
  3     3   25  
  3         25  
  3         5  
24 3 100       200 croak q{You cannot use 'simple' as a syntax extension. You need to }
25             . q{select a specific version, for example 'simple/v1'}
26             if $class eq __PACKAGE__;
27 2         3 my $target = $args{into};
28 2         8 my @extensions = $class->_available_extensions;
29             $class->_setup_extension($_, $target)
30 2         15 for @extensions;
31 2         11 return 1;
32             }
33              
34 2     2   1023 method _check_is_moose_param_role ($class: $target) {
  4     4   7  
  4         6  
  4         4  
35 4         19 return $class->_check_meta_isa($target, $role_meta);
36             }
37              
38 2     2   832 method _check_has_meta ($class: $target) {
  7     7   9  
  7         9  
  7         9  
39             return $INC{'Moose.pm'}
40 7 50       77 ? do { require Moose::Util; Moose::Util::find_meta($target) }
  0         0  
  0         0  
41             : undef;
42             }
43              
44 2     2   1116 method _check_meta_isa ($class: $target, $check) {
  4     4   6  
  4         5  
  4         4  
45 4 50       18 my $meta = $class->_check_has_meta($target)
46             or return undef;
47 0         0 return $meta->isa($check);
48             }
49              
50 2     2   851 method _setup_extension ($class: $extension, $target) {
  9     9   15  
  9         12  
  9         14  
51 9         35 my $setup_method = "_setup_${extension}_ext";
52 9         14 my $check_method = "_can${setup_method}";
53             return undef
54 9 100 100     93 if $class->can($check_method)
55             and not $class->$check_method($target);
56 3         22 return $class->$setup_method($target);
57             }
58              
59 2     2   1498 method _setup_moose_param_role_body_sugar_ext ($class: $target) {
  0     0   0  
  0         0  
  0         0  
60 0         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             }
74              
75 2     2   1145 method _setup_function_keyword_ext ($class: $target) {
  2     2   5  
  2         3  
  2         12  
76 2         24 Syntax::Feature::Function->install(
77             into => $target,
78             options => { -as => 'fun' },
79             );
80 2         777 return 1;
81             }
82              
83 2     2   856 method _setup_moose_param_role_method_sugar_ext ($class: $target) {
  0     0   0  
  0         0  
  0         0  
84 0 0       0 my $orig = $target->can('method')
85             or croak qq{There is no 'method' callback installed in '$target'};
86             reinstall_sub {
87             into => $target,
88             as => 'method',
89             code => sub {
90 0 0   0   0 return $_[0] if ref $_[0] eq 'CODE';
91 0         0 goto $orig;
92             },
93 0         0 };
94 0         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         0 return 1;
104             }
105              
106 2     2   1046 method _setup_method_keyword_ext ($class: $target) {
  1     1   2  
  1         3  
  1         1  
107 1         8 Syntax::Feature::Method->install(
108             into => $target,
109             options => { -as => 'method' },
110             );
111 1         228 return 1;
112             }
113              
114 2     2   711 method _setup_modifier_sugar_ext ($class: $target) {
  0     0      
  0            
  0            
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             }
128              
129             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 class or role is detected, this extension will setup a C
152             keyword for function declarations, a C keyword, and one keyword
153             each for C, C and C.
154              
155             The modifiers behave exactly like normal method declarations, except for
156             C 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 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 you will also get sugar for
197             the C 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 will
203             only setup the function syntax, while C 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 (basic set)
221              
222             =over
223              
224             =item * C
225              
226             =item * C
227              
228             =item * L
229              
230             =back
231              
232             =head2 C (extended set)
233              
234             =over
235              
236             =item * L if a valid Perl version was declared
237              
238             =back
239              
240             =head1 SEE ALSO
241              
242             =over
243              
244             =item L
245              
246             Version 1 of the extension set.
247              
248             =item L
249              
250             Version 2 of the extension set.
251              
252             =item L
253              
254             The syntax dispatching module.
255              
256             =item L
257              
258             Contains general information about this extension.
259              
260             =item L
261              
262             Specifics about the C and modifier keywords.
263              
264             =item L
265              
266             Specifics about the C function keyword.
267              
268             =item L
269              
270             Post-modern object-orientation.
271              
272             =item L
273              
274             Parameterizable roles for L.
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
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__