Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / MooseX::Method::Signatures.3pm
1 .\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 'br\}
42 .\"
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
44 .ie \n(.g .ds Aq \(aq
45 .el       .ds Aq '
46 .\"
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD.  Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
51 .ie \nF \{\
52 .    de IX
53 .    tm Index:\\$1\t\\n%\t"\\$2"
54 ..
55 .    nr % 0
56 .    rr F
57 .\}
58 .el \{\
59 .    de IX
60 ..
61 .\}
62 .\"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
65 .    \" fudge factors for nroff and troff
66 .if n \{\
67 .    ds #H 0
68 .    ds #V .8m
69 .    ds #F .3m
70 .    ds #[ \f1
71 .    ds #] \fP
72 .\}
73 .if t \{\
74 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
75 .    ds #V .6m
76 .    ds #F 0
77 .    ds #[ \&
78 .    ds #] \&
79 .\}
80 .    \" simple accents for nroff and troff
81 .if n \{\
82 .    ds ' \&
83 .    ds ` \&
84 .    ds ^ \&
85 .    ds , \&
86 .    ds ~ ~
87 .    ds /
88 .\}
89 .if t \{\
90 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
96 .\}
97 .    \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 .    \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 .    \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
112 \{\
113 .    ds : e
114 .    ds 8 ss
115 .    ds o a
116 .    ds d- d\h'-1'\(ga
117 .    ds D- D\h'-1'\(hy
118 .    ds th \o'bp'
119 .    ds Th \o'LP'
120 .    ds ae ae
121 .    ds Ae AE
122 .\}
123 .rm #[ #] #H #V #F C
124 .\" ========================================================================
125 .\"
126 .IX Title "MooseX::Method::Signatures 3"
127 .TH MooseX::Method::Signatures 3 "2010-02-04" "perl v5.8.8" "User Contributed Perl Documentation"
128 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
130 .if n .ad l
131 .nh
132 .SH "NAME"
133 MooseX::Method::Signatures \- Method declarations with type constraints and no source filter
134 .SH "SYNOPSIS"
135 .IX Header "SYNOPSIS"
136 .Vb 1
137 \&    package Foo;
138 \&
139 \&    use Moose;
140 \&    use MooseX::Method::Signatures;
141 \&
142 \&    method morning (Str $name) {
143 \&        $self\->say("Good morning ${name}!");
144 \&    }
145 \&
146 \&    method hello (Str :$who, Int :$age where { $_ > 0 }) {
147 \&        $self\->say("Hello ${who}, I am ${age} years old!");
148 \&    }
149 \&
150 \&    method greet (Str $name, Bool :$excited = 0) {
151 \&        if ($excited) {
152 \&            $self\->say("GREETINGS ${name}!");
153 \&        }
154 \&        else {
155 \&            $self\->say("Hi ${name}!");
156 \&        }
157 \&    }
158 \&
159 \&    $foo\->morning(\*(AqResi\*(Aq);                          # This works.
160 \&
161 \&    $foo\->hello(who => \*(Aqworld\*(Aq, age => 42);         # This too.
162 \&
163 \&    $foo\->greet(\*(AqResi\*(Aq, excited => 1);              # And this as well.
164 \&
165 \&    $foo\->hello(who => \*(Aqworld\*(Aq, age => \*(Aqfortytwo\*(Aq); # This doesn\*(Aqt.
166 \&
167 \&    $foo\->hello(who => \*(Aqworld\*(Aq, age => \-23);        # This neither.
168 \&
169 \&    $foo\->morning;                                  # Won\*(Aqt work.
170 \&
171 \&    $foo\->greet;                                    # Will fail.
172 .Ve
173 .SH "DESCRIPTION"
174 .IX Header "DESCRIPTION"
175 Provides a proper method keyword, like \*(L"sub\*(R" but specifically for making methods
176 and validating their arguments against Moose type constraints.
177 .SH "SIGNATURE SYNTAX"
178 .IX Header "SIGNATURE SYNTAX"
179 The signature syntax is heavily based on Perl 6. However not the full Perl 6
180 signature syntax is supported yet and some of it never will be.
181 .SS "Type Constraints"
182 .IX Subsection "Type Constraints"
183 .Vb 3
184 \&    method foo (             $affe) # no type checking
185 \&    method bar (Animal       $affe) # $affe\->isa(\*(AqAnimal\*(Aq)
186 \&    method baz (Animal|Human $affe) # $affe\->isa(\*(AqAnimal\*(Aq) || $affe\->isa(\*(AqHuman\*(Aq)
187 .Ve
188 .SS "Positional vs. Named"
189 .IX Subsection "Positional vs. Named"
190 .Vb 3
191 \&    method foo ( $a,  $b,  $c) # positional
192 \&    method bar (:$a, :$b, :$c) # named
193 \&    method baz ( $a,  $b, :$c) # combined
194 .Ve
195 .SS "Required vs. Optional"
196 .IX Subsection "Required vs. Optional"
197 .Vb 2
198 \&    method foo ($a , $b!, :$c!, :$d!) # required
199 \&    method bar ($a?, $b?, :$c , :$d?) # optional
200 .Ve
201 .SS "Defaults"
202 .IX Subsection "Defaults"
203 .Vb 1
204 \&    method foo ($a = 42) # defaults to 42
205 .Ve
206 .SS "Constraints"
207 .IX Subsection "Constraints"
208 .Vb 1
209 \&    method foo ($foo where { $_ % 2 == 0 }) # only even
210 .Ve
211 .SS "Invocant"
212 .IX Subsection "Invocant"
213 .Vb 3
214 \&    method foo (        $moo) # invocant is called $self and is required
215 \&    method bar ($self:  $moo) # same, but explicit
216 \&    method baz ($class: $moo) # invocant is called $class
217 .Ve
218 .SS "Labels"
219 .IX Subsection "Labels"
220 .Vb 2
221 \&    method foo (:     $affe ) # called as $obj\->foo(affe => $value)
222 \&    method bar (:apan($affe)) # called as $obj\->foo(apan => $value)
223 .Ve
224 .SS "Traits"
225 .IX Subsection "Traits"
226 .Vb 2
227 \&    method foo (Affe $bar does trait)
228 \&    method foo (Affe $bar is trait)
229 .Ve
230 .PP
231 The only currently supported trait is \f(CW\*(C`coerce\*(C'\fR, which will attempt to coerce
232 the value provided if it doesn't satisfy the requirements of the type
233 constraint.
234 .SS "Placeholders"
235 .IX Subsection "Placeholders"
236 .Vb 1
237 \&    method foo ($bar, $, $baz)
238 .Ve
239 .PP
240 Sometimes you don't care about some params you're being called with. Just put
241 the bare sigil instead of a full variable name into the signature to avoid an
242 extra lexical variable to be created.
243 .SS "Complex Example"
244 .IX Subsection "Complex Example"
245 .Vb 3
246 \&    method foo ( SomeClass $thing where { $_\->can(\*(Aqstuff\*(Aq) }:
247 \&                 Str  $bar  = "apan",
248 \&                 Int :$baz! = 42 where { $_ % 2 == 0 } where { $_ > 10 } )
249 \&
250 \&    # the invocant is called $thing, must be an instance of SomeClass and
251 \&           has to implement a \*(Aqstuff\*(Aq method
252 \&    # $bar is positional, required, must be a string and defaults to "apan"
253 \&    # $baz is named, required, must be an integer, defaults to 42 and needs
254 \&    #      to be even and greater than 10
255 .Ve
256 .SH "BUGS, CAVEATS AND NOTES"
257 .IX Header "BUGS, CAVEATS AND NOTES"
258 This module is as stable now, but this is not to say that it is entirely bug
259 free. If you notice any odd behaviour (messages not being as good as they could
260 for example) then please raise a bug.
261 .SS "Fancy signatures"
262 .IX Subsection "Fancy signatures"
263 Parse::Method::Signatures is used to parse the signatures. However, some
264 signatures that can be parsed by it aren't supported by this module (yet).
265 .SS "No source filter"
266 .IX Subsection "No source filter"
267 While this module does rely on the hairy black magic of Devel::Declare it
268 does not depend on a source filter. As such, it doesn't try to parse and
269 rewrite your source code and there should be no weird side effects.
270 .PP
271 Devel::Declare only effects compilation. After that, it's a normal subroutine.
272 As such, for all that hairy magic, this module is surprisingly stable.
273 .SS "What about regular subroutines?"
274 .IX Subsection "What about regular subroutines?"
275 Devel::Declare cannot yet change the way \f(CW\*(C`sub\*(C'\fR behaves. However, the
276 signatures module can. Right now it only provides very basic
277 signatures, but it's extendable enough that plugging MooseX::Method::Signatures
278 signatures into that should be quite possible.
279 .SS "What about the return value?"
280 .IX Subsection "What about the return value?"
281 Type constraints for return values can be declared using
282 .PP
283 .Vb 1
284 \&  method foo (Int $x, Str $y) returns (Bool) { ... }
285 .Ve
286 .PP
287 however, this feature only works with scalar return values and is still
288 considered to be experimental.
289 .SS "Interaction with Moose::Role"
290 .IX Subsection "Interaction with Moose::Role"
291 \fIMethods not seen by a role's \f(CI\*(C`requires\*(C'\fI\fR
292 .IX Subsection "Methods not seen by a role's requires"
293 .PP
294 Because the processing of the MooseX::Method::Signatures
295 \&\f(CW\*(C`method\*(C'\fR and the Moose \f(CW\*(C`with\*(C'\fR keywords are both
296 done at runtime, it can happen that a role will require
297 a method before it is declared (which will cause
298 Moose to complain very loudly and abort the program).
299 .PP
300 For example, the following will not work:
301 .PP
302 .Vb 1
303 \&    # in file Canine.pm
304 \&
305 \&    package Canine;
306 \&
307 \&    use Moose;
308 \&    use MooseX::Method::Signatures;
309 \&
310 \&    with \*(AqWatchdog\*(Aq;
311 \&
312 \&    method bark { print "Woof!\en"; }
313 \&
314 \&    1;
315 \&
316 \&
317 \&    # in file Watchdog.pm
318 \&
319 \&    package Watchdog;
320 \&
321 \&    use Moose::Role;
322 \&
323 \&    requires \*(Aqbark\*(Aq;  # will assert! evaluated before \*(Aqmethod\*(Aq is processed
324 \&
325 \&    sub warn_intruder {
326 \&        my $self = shift;
327 \&        my $intruder = shift;
328 \&
329 \&        $self\->bark until $intruder\->gone;
330 \&    }
331 \&
332 \&    1;
333 .Ve
334 .PP
335 A workaround for this problem is to use \f(CW\*(C`with\*(C'\fR only
336 after the methods have been defined.  To take our previous
337 example, \fBCanine\fR could be reworked thus:
338 .PP
339 .Vb 1
340 \&    package Canine;
341 \&
342 \&    use Moose;
343 \&    use MooseX::Method::Signatures;
344 \&
345 \&    method bark { print "Woof!\en"; }
346 \&
347 \&    with \*(AqWatchdog\*(Aq;
348 \&
349 \&    1;
350 .Ve
351 .PP
352 A better solution is to use MooseX::Declare instead of plain
353 MooseX::Method::Signatures. It defers application of roles until the end
354 of the class definition. With it, our example would becomes:
355 .PP
356 .Vb 1
357 \&    # in file Canine.pm
358 \&
359 \&    use MooseX::Declare;
360 \&
361 \&    class Canine with Watchdog {
362 \&        method bark { print "Woof!\en"; }
363 \&    }
364 \&
365 \&    1;
366 \&
367 \&    # in file Watchdog.pm
368 \&
369 \&    use MooseX::Declare;
370 \&
371 \&    role Watchdog {
372 \&        requires \*(Aqbark\*(Aq;
373 \&
374 \&        method warn_intruder ( $intruder ) {
375 \&            $self\->bark until $intruder\->gone;
376 \&        }
377 \&    }
378 \&
379 \&    1;
380 .Ve
381 .PP
382 \fI\fISubroutine redefined\fI warnings\fR
383 .IX Subsection "Subroutine redefined warnings"
384 .PP
385 When composing a Moose::Role into a class that uses
386 MooseX::Method::Signatures, you may get a \*(L"Subroutine redefined\*(R"
387 warning. This happens when both the role and the class define a
388 method/subroutine of the same name. (The way roles work, the one
389 defined in the class takes precedence.) To eliminate this warning,
390 make sure that your \f(CW\*(C`with\*(C'\fR declaration happens after any
391 method/subroutine declarations that may have the same name as a
392 method/subroutine within a role.
393 .SH "SEE ALSO"
394 .IX Header "SEE ALSO"
395 MooseX::Declare
396 .PP
397 Method::Signatures::Simple
398 .PP
399 Method::Signatures
400 .PP
401 Perl6::Subs
402 .PP
403 Devel::Declare
404 .PP
405 Parse::Method::Signatures
406 .PP
407 Moose
408 .SH "AUTHOR"
409 .IX Header "AUTHOR"
410 Florian Ragwitz <rafl@debian.org>
411 .PP
412 With contributions from:
413 .IP "Ash Berlin <ash@cpan.org>" 4
414 .IX Item "Ash Berlin <ash@cpan.org>"
415 .PD 0
416 .IP "Cory Watson <gphat@cpan.org>" 4
417 .IX Item "Cory Watson <gphat@cpan.org>"
418 .IP "Hakim Cassimally <hakim.cassimally@gmail.com>" 4
419 .IX Item "Hakim Cassimally <hakim.cassimally@gmail.com>"
420 .IP "Jonathan Scott Duff <duff@pobox.com>" 4
421 .IX Item "Jonathan Scott Duff <duff@pobox.com>"
422 .IP "Justin Hunter <justin.d.hunter@gmail.com>" 4
423 .IX Item "Justin Hunter <justin.d.hunter@gmail.com>"
424 .IP "Kent Fredric <kentfredric@gmail.com>" 4
425 .IX Item "Kent Fredric <kentfredric@gmail.com>"
426 .IP "Maik Hentsche <maik.hentsche@amd.com>" 4
427 .IX Item "Maik Hentsche <maik.hentsche@amd.com>"
428 .IP "Matt Kraai <kraai@ftbfs.org>" 4
429 .IX Item "Matt Kraai <kraai@ftbfs.org>"
430 .IP "Rhesa Rozendaal <rhesa@cpan.org>" 4
431 .IX Item "Rhesa Rozendaal <rhesa@cpan.org>"
432 .IP "Ricardo \s-1SIGNES\s0 <rjbs@cpan.org>" 4
433 .IX Item "Ricardo SIGNES <rjbs@cpan.org>"
434 .IP "Steffen Schwigon <ss5@renormalist.net>" 4
435 .IX Item "Steffen Schwigon <ss5@renormalist.net>"
436 .IP "Yanick Champoux <yanick@babyl.dyndns.org>" 4
437 .IX Item "Yanick Champoux <yanick@babyl.dyndns.org>"
438 .IP "Nicholas Perez <nperez@cpan.org>" 4
439 .IX Item "Nicholas Perez <nperez@cpan.org>"
440 .PD
441 .SH "COPYRIGHT AND LICENSE"
442 .IX Header "COPYRIGHT AND LICENSE"
443 Copyright (c) 2008, 2009  Florian Ragwitz
444 .PP
445 Code based on the tests for Devel::Declare.
446 .PP
447 Documentation based on MooseX::Method and Method::Signatures.
448 .PP
449 Licensed under the same terms as Perl itself.