Smart::Args::TypeTiny - We are smart, smart for you
use Smart::Args::TypeTiny; use Types::Standard -all; 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.
This code is expected $p as InstanceOf['Int'], you should specify Type::Tiny's type constraint.
$p
use Types::Standard -all; sub foo { args my $p => 'Int', # :( InstanceOf['Int'] my $q => Int, # :) Int my $r => 'Foo', # :) InstanceOf['Foo'] }
sub foo { args my $p => {isa => 'Foo', default => create_foo}, # :( create_foo is called every time even if $p is passed my $q => {isa => 'Foo', default => sub { create_foo }}, # :) create_foo is called only when $p is not passed ; }
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, Params::Validate, Params::ValidationCompiler
Type::Tiny, Types::Standard
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.