Smart::Args::TypeTiny - We are smart, smart for you
use Smart::Args::TypeTiny; sub func2 { args my $p => 'Int', my $q => {isa => 'Int', optional => 1}, ; } func2(p => 3, q => 4); # p => 3, q => 4 func2(p => 3); # p => 3, q => undef sub func3 { args my $p => {isa => 'Int', default => 3}; } func3(p => 4); # p => 4 func3(); # p => 3 package F; use Moo; use Smart::Args::TypeTiny; use Types::Standard -all; sub method { args my $self, my $p => Int, ; } sub class_method { args my $class => ClassName, my $p => Int, ; } sub simple_method { args_pos my $self, my $p; } my $f = F->new(); $f->method(p => 3); F->class_method(p => 3); F->simple_method(3);
Smart::Args::TypeTiny provides Smart::Args-like argument validator using Type::Tiny.
sub foo { args my $x => 'Str'; } sub bar { args_pos my $x => 'Str'; } foo(x => 'a', y => 'b'); # fatal: Unexpected parameter 'y' passed bar('a', 'b'); # fatal: Too many parameters passed
sub foo { args my $p => {isa => 'Int', optional => 1}; } foo(); # $p = undef foo(p => 1); # $p = 1 foo(p => undef); # $p = undef
sub foo { args my $p => {isa => 'Foo', default => create_foo}, # calls every time even if $p is passed my $q => {isa => 'Foo', default => sub { create_foo }}, # calls only when $p is not passed ; }
sub foo { args my $int => 'Int', my $foo => 'Foo', my $bar => {isa => 'Bar', default => sub { Bar->new }}, my $baz => {isa => 'Baz', optional => 1}, my $bool => {isa => 'Bool', default => 0}, ; ... } foo(int => 1, foo => Foo->new, bool => 1);
Check parameters and fills them into lexical variables. All the parameters are mandatory by default. The hashref of all parameters is returned.
$rule can be any one of type name, Type::Tiny's type constraint object, or hashref have these parameters:
$rule
Str|Object
Type name or Type::Tiny's type constraint.
args my $int => {isa => 'Int'};
Enable if Mouse.pm is loaded.
use Mouse::Util::TypeConstraints; subtype 'PositiveInt', as 'Int', where { $_ > 0 }, message { 'Must be greater than zero' }; args my $positive_int => {isa => 'PositiveInt'};
{ package Foo; ... } args my $foo => {isa => 'Foo'};
Role name or Type::Tiny's type constraint object.
Bool
The parameter doesn't need to be passed when optional is true.
Any|CodeRef
Default value for the parameter.
sub bar { args_pos my $x => 'Str', my $p => 'Int', ; ... } bar('abc', 123);
Same as args except take arguments instead of parameters.
args
For optimization calling subroutine in runtime type check, you can overwrite check_rule like following code:
check_rule
{ no warnings 'redefine'; sub Smart::Args::TypeTiny::check_rule { my ($rule, $value, $exists, $name) = @_; return $value; } }
Smart::Args, Data::Validator, Params::Validate, Params::ValidationCompiler
Type::Tiny, Types::Standard, Mouse::Util::TypeConstraints
Copyright (C) Takumi Akiyama.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Takumi Akiyama <t.akiym@gmail.com>
To install Smart::Args::TypeTiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Smart::Args::TypeTiny
CPAN shell
perl -MCPAN -e shell install Smart::Args::TypeTiny
For more information on module installation, please visit the detailed CPAN module installation guide.