MooseX::Extended::Manual::Includes - An overview of MooseX::Extended optional features
version 0.32
includes
Some experimental features are useful, but might not be quite what you want.
By default, MooseX::Extended tries to be relatively conservative. However, you might want to turn it up to 11. There are optional, EXPERIMENTAL features you can use for this. They're turned by the includes flag.
method
package My::Names { use MooseX::Extended types => [qw(compile NonEmptyStr Str )], includes => 'method'; param _name => ( isa => NonEmptyStr, init_arg => 'name' ); param title => ( isa => Str, required => 0, predicate => 1 ); method name() { my $title = $self->title; # $self is injected for you my $name = $self->_name; return $title ? "$title $name" : $name; } }
Adds a method keyword and injects $self into the method body. Requires Function::Parameters.
$self
Note: this is equivalent to writing:
use Function::Parameters 'method';
The other features of Function::Parameters are not available.
This feature does not work with the optional multi keyword. Thus, if you do this:
multi
use MooseX::Extended includes => [qw/multi method/];
You cannot do multi method. You'll have to fall back to multi sub.
multi method
multi sub
use MooseX::Extended includes => 'multi'; multi sub foo ($self, $x) { ... } multi sub foo ($self, $x, $y ) { ... }
Allows you to redeclare a method (or subroutine) and the dispatch will use the number of arguments to determine which subroutine to use. Note that "slurpy" arguments such as arrays or hashes will take precedence over scalars if they are declared first:
multi sub foo ($self, @x) { ... } multi sub foo ($self, $x) { ... } # will never be called
Thus, the following probably doesn't do what you want.
package Foo { use MooseX::Extended includes => 'multi'; multi sub foo ($self, @bar) { return '@bar' } multi sub foo ($self, $bar) { return '$bar' } } say +Foo->new->foo(1); say +Foo->new->foo(1,2,3);
Both of the above will print the string @bar. The second definition of foo is effectively lost.
@bar
foo
You must declare slurpy methods last for them to work correctly:
multi sub foo ($self, $bar) { return '$bar' } multi sub foo ($self, @bar) { return '@bar' }
See https://rt.cpan.org/Ticket/Display.html?id=144171 for more information.
Only available on Perl v5.26.0 or higher. Requires Syntax::Keyword::MultiSub.
This feature does not work with the optional method keyword. Thus, if you do this:
async
package My::Thing { use MooseX::Extended types => 'Str', includes => 'async'; use IO::Async::Loop; field output => ( is => 'rw', isa => Str, default => '' ); async sub doit ( $self, @list ) { my $loop = IO::Async::Loop->new; $self->output('> '); foreach my $item (@list) { await $loop->delay_future( after => 0.01 ); $self->output( $self->output . "$item " ); } } }
Allows you to write asynchronous code with async and await.
await
Only available on Perl v5.26.0 or higher. Requires Future::AsyncAwait.
try
package My::Try { use MooseX::Extended includes => 'try'; sub reciprocal ( $self, $num ) { try { return 1 / $num; } catch { croak "Could not calculate reciprocal of $num: $@"; } } }
Allows you to use try/catch blocks, via Syntax::Keyword::Try.
Only available on Perl v5.24.0 or higher. Requires Syntax::Keyword::Try.
Curtis "Ovid" Poe <curtis.poe@gmail.com>
This software is Copyright (c) 2022 by Curtis "Ovid" Poe.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install MooseX::Extended, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Extended
CPAN shell
perl -MCPAN -e shell install MooseX::Extended
For more information on module installation, please visit the detailed CPAN module installation guide.