Kavorka::Manual::Methods - method/classmethod/objectmethod keywords
Kavorka provides the method, classmethod and objectmethod keywords for the purpose of defining methods.
method
classmethod
objectmethod
The anatomy of a method:
The keyword introducing the method.
The method name (optional).
The signature (optional).
Traits (optional).
The prototype (optional).
The attribute list (optional).
The method body.
Example:
# (1) (2) (3) (4) (5) (6) (7) method foobar ($foo) is cool :($) :cached { return $foo + $self->bar } # (1) (6) my $m = method { return $_[0] + $self->bar };
By default, only the method keyword is exported. The others can be exported by request:
use Kavorka qw( method classmethod objectmethod );
If present, it specifies the name of the method being defined. If no name is present, the declaration is an expression that evaluates to a reference to the method in question.
Methods are added to the class at run-time. So this works:
if ($ENV{DEBUG}) { method foobar { ... } } else { method foobar { ... } }
It is possible to define lexical (i.e. truly private) methods using a lexical variable for a method name:
objectmethod get_name () { return $self->{name}; } objectmethod my $set_name (Str $new) { $self->{name} = $new; } $obj->$set_name("Bob"); $obj->get_name; # Bob
See also: Lexical::Accessor.
See Kavorka::Manual::Signature.
The method keyword has a default invocant called $self, but it does not have a type constraint, so can equally be used for class or object methods. The objectmethod keyword works the same, but does define a type constraint for $self, requiring it to be a blessed object. The classmethod keyword defines an invocant called $class which has a type constraint requiring it to be a string.
$self
$class
In any case, it is perfectly possible to define your own name and type constraint for the invocant:
method foo ( ClassName $me: Int $foo ) { ... }
See Kavorka::Manual::ExtendingKavorka.
See Kavorka::Manual::PrototypeAndAttributes.
Note however that prototypes are fairly useless for methods.
The method, objectmethod and classmethod keywords automatically add the :method attribute to methods.
:method
This is more or less what you'd expect from the method body you'd write with sub, however the lexical variables for parameters are pre-declared and pre-populated, and invocants have been shifted off @_.
@_
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Kavorka.
Kavorka::Manual, Kavorka::Manual::Signature, Kavorka::Manual::PrototypeAndAttributes, Kavorka::Manual::MultiSubs, Kavorka::Manual::MethodModifiers.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
To install Kavorka, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Kavorka
CPAN shell
perl -MCPAN -e shell install Kavorka
For more information on module installation, please visit the detailed CPAN module installation guide.