Scroll to navigation

Kavorka::Manual::ExtendingKavorka(3pm) User Contributed Perl Documentation Kavorka::Manual::ExtendingKavorka(3pm)

NAME

Kavorka::Manual::ExtendingKavorka - extending Kavorka with traits and new keywords

DESCRIPTION

Traits

Many of the code constructs handled by Kavorka allow a list of traits to be given. Subs can have traits:

   method xyz ($x, $y, $z) is friendly { ... }

So can parameters within signatures:

   method xyz ($x is hot, $y does warm, $z but cool) { ... }

And return types:

   method get_age (Date $date → Num $age is years) { ... }

When a trait is used which isn't natively handled by Kavorka::Sub, Kavorka::Parameter or Kavorka::ReturnType, Kavorka will attempt to load a Moo::Role to handle the trait. These will be loaded from the following namespaces, as appropriate:

  • "Kavorka::TraitFor::Sub::*"
  • "Kavorka::TraitFor::Parameter::*"
  • "Kavorka::TraitFor::ReturnType::*"

For example, "Kavorka::TraitFor::Sub::friendly".

Trait Parameters

Although none of Kavorka's native traits make use of this syntax, traits can be followed by trait parameters in parentheses:

   BEGIN {
      package Kavorka::TraitFor::Parameter::debug;
      use Moo::Role;
      around injection => sub
      {
         my $next = shift;
         my $self = shift;
         my $code = $self->$next(@_);
         $code .= sprintf(
            "printf STDERR %s, %s, %s;",
            B::perlstring($self->traits->{debug}[0]),
            B::perlstring($self->name),
            $self->name,
         );
         return $code;
      };
   }
   
   use Kavorka;
   
   fun foo ( $x but debug("%s is %s\n") ) {
      ## Injected:
      ## printf STDERR "%s is %s\n", "\$x", $x;
      return $x;
   }
   
   foo(42);  # says to STDERR: '$x is 42'

Keywords

Traits are not applied to subs until after they've been parsed, which means that traits cannot, say, alter how the signature is parsed, because the signature occurs before the traits.

For more advanced control over the parsing and behaviour of subs, you would need to create a new keyword. A keyword is just a Moo class which consumes the Kavorka::Sub role. Kavorka::Sub::Method and Kavorka::Sub::Fun are simple examples of such keyword classes.

People can use your new keyword like this:

   use Kavorka yourkeyword => { implementation => "Your::Class" };

BUGS

Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Kavorka>.

SEE ALSO

Kavorka::Manual.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2013-2014, 2017 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.

DISCLAIMER OF WARRANTIES

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.

2019-01-17 perl v5.28.1