1Method::Signatures::SimUpsleer(3C)ontributed Perl DocumeMnettahtoido:n:Signatures::Simple(3)
2
3
4
6 Method::Signatures::Simple - Basic method declarations with signatures,
7 without source filters
8
10 version 1.07
11
13 # -- a basic class -- #
14 package User;
15 use Method::Signatures::Simple;
16
17 method new ($class: $name, $email) {
18 my $user = {
19 id => new_id(42),
20 name => $name,
21 email => $email,
22 };
23 bless $user, $class;
24 }
25
26 func new_id ($seed) {
27 state $id = $seed;
28 $id++;
29 }
30
31 method name { $self->{name}; }
32 method email { $self->{email}; }
33 1;
34
35
36 # -- other features -- #
37 # attributes
38 method foo : lvalue { $self->{foo} }
39
40 # change invocant name
41 use Method::Signatures::Simple invocant => '$this';
42 method foo ($bar) { $this->bar($bar) }
43 method bar ($class: $bar) { $class->baz($bar) }
44
45 # use a different function keyword
46 use Method::Signatures::Simple function_keyword => 'fun';
47 fun triple ($num) { 3 * $num }
48
49 # use a different method keyword
50 use Method::Signatures::Simple method_keyword => 'action';
51 action foo { $self->bar }
52
54 This module provides basic "method" and "func" keywords with simple
55 signatures. It's intentionally simple, and is supposed to be a stepping
56 stone for its bigger brothers MooseX::Method::Signatures and
57 Method::Signatures. It only has a small benefit over regular subs, so
58 if you want more features, look at those modules. But if you're
59 looking for a small amount of syntactic sugar, this might just be
60 enough.
61
63 · invocant
64
65 The "method" keyword automatically injects the annoying "my $self =
66 shift;" for you. You can rename the invocant with the first
67 argument, followed by a colon:
68
69 method ($this:) {}
70 method ($this: $that) {}
71
72 The "func" keyword doesn't inject an invocant, but does do the
73 signature processing below:
74
75 func ($that) {}
76
77 · signature
78
79 The signature "($sig)" is transformed into "my ($sig) = \@_;". That
80 way, we mimic perl's usual argument handling.
81
82 method foo ($bar, $baz, %opts) {
83 func xyzzy ($plugh, @zorkmid) {
84
85 # becomes
86
87 sub foo {
88 my $self = shift;
89 my ($bar, $baz, %opts) = @_;
90
91 sub xyzzy {
92 my ($plugh, @zorkmid) = @_;
93
95 Since this module subclasses Devel::Declare::MethodInstaller::Simple,
96 you can change the keywords and the default invocant with import
97 arguments. These changes affect the current scope.
98
99 · change the invocant name
100
101 use Method::Signatures::Simple invocant => '$this';
102 method x { $this->{x} }
103 method y { $this->{y} }
104
105 # and this of course still works:
106 method z ($self:) { $self->{z} }
107
108 · change the keywords
109
110 You can install a different keyword (instead of the default
111 'method' and 'func'), by passing names to the "use" line:
112
113 use Method::Signatures::Simple method_keyword => 'action',
114 function_keyword => 'thing';
115
116 action foo ($some, $args) { ... }
117 thing bar ($whatever) { ... }
118
119 One benefit of this is that you can use this module together with
120 e.g. MooseX::Declare:
121
122 # untested
123 use MooseX::Declare;
124
125 class Foo {
126 use Method::Signatures::Simple method_keyword => 'routine';
127 method x (Int $x) { ... } # from MooseX::Method::Signatures
128 routine y ($y) { ... } # from this module
129 }
130
131 If you specify neither "method_keyword" nor "function_keyword",
132 then we default to injecting "method" and "func". If you only
133 specify one of these options, then we only inject that one keyword
134 into your scope.
135
136 Examples:
137
138 # injects 'method' and 'func'
139 use Method::Signatures::Simple;
140
141 # only injects 'action'
142 use Method::Signatures::Simple method_keyword => 'action';
143
144 # only injects 'procedure'
145 use Method::Signatures::Simple function_keyword => 'procedure';
146
147 # injects 'action' and 'function'
148 use Method::Signatures::Simple method_keyword => 'action',
149 function_keyword => 'function';
150
151 · install several keywords
152
153 You're not limited to a single "use" line, so you can install
154 several keywords with the same semantics as 'method' into the
155 current scope:
156
157 use Method::Signatures::Simple; # provides 'method' and 'func'
158 use Method::Signatures::Simple method_keyword => 'action';
159
160 method x { ... }
161 func y { ... }
162 action z { ... }
163
165 Rhesa Rozendaal, "<rhesa at cpan.org>"
166
168 Please report any bugs or feature requests to
169 "bug-method-signatures-simple at rt.cpan.org", or through the web
170 interface at
171 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Method-Signatures-Simple>.
172 I will be notified, and then you'll automatically be notified of
173 progress on your bug as I make changes.
174
176 You can find documentation for this module with the perldoc command.
177
178 perldoc Method::Signatures::Simple
179
180 You can also look for information at:
181
182 · RT: CPAN's request tracker
183
184 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Method-Signatures-Simple>
185
186 · AnnoCPAN: Annotated CPAN documentation
187
188 <http://annocpan.org/dist/Method-Signatures-Simple>
189
190 · CPAN Ratings
191
192 <http://cpanratings.perl.org/d/Method-Signatures-Simple>
193
194 · Search CPAN
195
196 <http://search.cpan.org/dist/Method-Signatures-Simple>
197
199 · MSTROUT
200
201 For writing Devel::Declare and providing the core concepts.
202
203 · MSCHWERN
204
205 For writing Method::Signatures and publishing about it. This is
206 what got my attention.
207
208 · FLORA
209
210 For helping me abstracting the Devel::Declare bits and suggesting
211 improvements.
212
213 · CHIPS
214
215 For suggesting we add a 'func' keyword.
216
218 Devel::Declare, Method::Signatures, MooseX::Method::Signatures.
219
221 Copyright 2011 Rhesa Rozendaal, all rights reserved.
222
223 This program is free software; you can redistribute it and/or modify it
224 under the same terms as Perl itself.
225
226
227
228perl v5.32.0 2020-07-28 Method::Signatures::Simple(3)