File Coverage

blib/lib/Syntax/Feature/Method.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition 4 4 100.0
subroutine 9 9 100.0
pod 1 1 100.0
total 41 41 100.0


line stmt bran cond sub pod time code
1 1     1   59861 use strict;
  1         3  
  1         42  
2 1     1   6 use warnings;
  1         2  
  1         63  
3              
4             # ABSTRACT: Provide a method keyword
5              
6             package Syntax::Feature::Method;
7             BEGIN {
8 1     1   18 $Syntax::Feature::Method::VERSION = '0.001';
9             }
10              
11 1     1   1179 use Method::Signatures::Simple ();
  1         48797  
  1         35  
12 1     1   12 use B::Hooks::EndOfScope;
  1         3  
  1         9  
13 1     1   111 use Carp ();
  1         3  
  1         23  
14              
15 1     1   8 use namespace::clean;
  1         2  
  1         11  
16              
17             $Carp::Internal{ +__PACKAGE__ }++;
18              
19             sub install {
20 4     4 1 676 my ($class, %args) = @_;
21              
22 4         14 my $target = $args{into};
23 4   100     24 my $name = $args{options}{ -as } || 'method';
24 4   100     18 my $invocant = $args{options}{ -invocant } || '$self';
25              
26             # install keyword
27 4         20 Method::Signatures::Simple->import(
28             into => $target,
29             name => $name,
30             invocant => $invocant,
31             );
32              
33             # remove runtime handler at end of scope
34             on_scope_end {
35 4     4   2080 namespace::clean->clean_subroutines($target, $name);
36 4         1038 };
37              
38 4         55 return 1;
39             }
40              
41             1;
42              
43              
44              
45             =pod
46              
47             =head1 NAME
48              
49             Syntax::Feature::Method - Provide a method keyword
50              
51             =head1 VERSION
52              
53             version 0.001
54              
55             =head1 SYNOPSIS
56              
57             use syntax 'method';
58              
59             method foo ($n) { $n * $self->bar }
60              
61             my $method = method ($msg) {
62             print "$msg\n";
63             };
64              
65             =head1 DESCRIPTION
66              
67             This module will install the L syntax extension
68             into the requesting namespace.
69              
70             You can import the keyword multiple times under different names with different
71             options:
72              
73             use syntax
74             'method',
75             'method' => {
76             -as => 'classmethod',
77             -invocant => '$class',
78             };
79              
80             =head1 OPTIONS
81              
82             =head2 -as
83              
84             use syntax method => { -as => 'provide' };
85              
86             provide addition ($n, $m) { $n + $m }
87              
88             The C<-as> keyword allows you to install the keyword under a different name.
89             This is especially useful if you want a separate keyword for class methods with
90             a different L.
91              
92             =head2 -invocant
93              
94             use syntax method => { -invocant => '$me' };
95              
96             method sum { $me->foo + $me->bar }
97              
98             Allows you to set a different default invocant. Useful if you want to import
99             a second keyword for class methods that has a C<$class> invocant.
100              
101             =head1 METHODS
102              
103             =head2 install
104              
105             Called by the L dispatcher to install the exntension into the
106             requesting package.
107              
108             =head1 SEE ALSO
109              
110             L,
111             L
112              
113             =head1 BUGS
114              
115             Please report any bugs or feature requests to bug-syntax-feature-method@rt.cpan.org or through the web interface at:
116             http://rt.cpan.org/Public/Dist/Display.html?Name=Syntax-Feature-Method
117              
118             =head1 AUTHOR
119              
120             Robert 'phaylon' Sedlacek
121              
122             =head1 COPYRIGHT AND LICENSE
123              
124             This software is copyright (c) 2010 by Robert 'phaylon' Sedlacek.
125              
126             This is free software; you can redistribute it and/or modify it under
127             the same terms as the Perl 5 programming language system itself.
128              
129             =cut
130              
131              
132             __END__