-
-
29 Oct 2017 13:50:26 UTC
- Distribution: Smart-Args-TypeTiny
- Module version: 0.12
- Source (raw)
- Browse (raw)
- Changes
- Homepage
- How to Contribute
- Repository
- Issues
- Testers (1275 / 0 / 0)
- Kwalitee
Bus factor: 1- 94.12% Coverage
- License: perl_5
- Activity
24 month- Tools
- Download (14.5KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
NAME
Smart::Args::TypeTiny - We are smart, smart for you
SYNOPSIS
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);
DESCRIPTION
Smart::Args::TypeTiny provides Smart::Args-like argument validator using Type::Tiny.
IMCOMPATIBLE CHANGES WITH Smart::Args
- Unexpected parameters will be a fatal error
-
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
- Optional allows to pass undef to parameter
-
sub foo { args my $p => {isa => 'Int', optional => 1}; } foo(); # $p = undef foo(p => 1); # $p = 1 foo(p => undef); # $p = undef
- Default parameter can take coderef as lazy value
-
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 ; }
FUNCTIONS
- my $args = args my $var[, $rule], ...;
-
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:- isa
Str|Object
-
Type name or Type::Tiny's type constraint.
- Types::Standard
-
args my $int => {isa => 'Int'};
- Mouse::Util::TypeConstraints
-
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'};
- class name
-
{ package Foo; ... } args my $foo => {isa => 'Foo'};
- does
Str|Object
-
Role name or Type::Tiny's type constraint object.
- optional
Bool
-
The parameter doesn't need to be passed when optional is true.
- default
Any|CodeRef
-
Default value for the parameter.
- isa
- my $args = args_pos my $var[, $rule], ...;
-
sub bar { args_pos my $x => 'Str', my $p => 'Int', ; ... } bar('abc', 123);
Same as
args
except take arguments instead of parameters.
TIPS
SKIP TYPE CHECK
For optimization calling subroutine in runtime type check, you can overwrite
check_rule
like following code:{ no warnings 'redefine'; sub Smart::Args::TypeTiny::check_rule { my ($rule, $value, $exists, $name) = @_; return $value; } }
SEE ALSO
Smart::Args, Data::Validator, Params::Validate, Params::ValidationCompiler
Type::Tiny, Types::Standard, Mouse::Util::TypeConstraints
LICENSE
Copyright (C) Takumi Akiyama.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
Takumi Akiyama <t.akiym@gmail.com>
Module Install Instructions
To install Smart::Args::TypeTiny, copy and paste the appropriate command in to your terminal.
cpanm Smart::Args::TypeTiny
perl -MCPAN -e shell install Smart::Args::TypeTiny
For more information on module installation, please visit the detailed CPAN module installation guide.