Aion
view release on metacpan or search on metacpan
lib/Aion/Types.md view on Meta::CPAN
!ru:en
# NAME
Aion::Types - библиоÑека ÑÑандаÑÑнÑÑ
валидаÑоÑов и ÑлÑÐ¶Ð¸Ñ Ð´Ð»Ñ ÑÐ¾Ð·Ð´Ð°Ð½Ð¸Ñ Ð½Ð¾Ð²ÑÑ
валидаÑоÑов
# SYNOPSIS
```perl
use Aion::Types;
BEGIN {
subtype SpeakOfKitty => as StrMatch[qr/\bkitty\b/i],
message { "Speak is'nt included kitty!" };
}
"Kitty!" ~~ SpeakOfKitty # -> 1
"abc" ~~ SpeakOfKitty # -> ""
SpeakOfKitty->validate("abc", "This") # @-> Speak is'nt included kitty!
BEGIN {
subtype IntOrArrayRef => as (Int | ArrayRef);
}
[] ~~ IntOrArrayRef # -> 1
35 ~~ IntOrArrayRef # -> 1
"" ~~ IntOrArrayRef # -> ""
coerce IntOrArrayRef, from Num, via { int($_ + .5) };
IntOrArrayRef->coerce(5.5) # => 6
```
# DESCRIPTION
ÐÑÐ¾Ñ Ð¼Ð¾Ð´ÑÐ»Ñ ÑкÑпоÑÑиÑÑÐµÑ Ð¿Ð¾Ð´Ð¿ÑогÑаммÑ:
* `subtype`, `as`, `init_where`, `where`, `awhere`, `message` â Ð´Ð»Ñ ÑÐ¾Ð·Ð´Ð°Ð½Ð¸Ñ Ð²Ð°Ð»Ð¸Ð´Ð°ÑоÑов.
* `SELF`, `ARGS`, `A`, `B`, `C`, `D`, `M`, `N` â Ð´Ð»Ñ Ð¸ÑполÑÐ·Ð¾Ð²Ð°Ð½Ð¸Ñ Ð² валидаÑоÑаÑ
Ñипа и его аÑгÑменÑов.
* `coerce`, `from`, `via` â Ð´Ð»Ñ ÑÐ¾Ð·Ð´Ð°Ð½Ð¸Ñ ÐºÐ¾Ð½Ð²ÐµÑÑоÑа знаÑений из одного клаÑÑа в дÑÑгой.
ÐеÑаÑÑ
Ð¸Ñ Ð²Ð°Ð»Ð¸Ð´Ð°ÑоÑов:
```text
Any
Control
Union[A, B...]
Intersection[A, B...]
Exclude[A...]
Option[A]
Wantarray[A, B]
Item
External[type]
Bool
BoolLike
Enum[e...]
Maybe[A]
Undef
Defined
Value
Version
Str
Uni
Bin
NonEmptyStr
StartsWith[start]
EndsWith[end]
Email
Tel
Url
Path
Html
StrDate
StrDateTime
StrMatch[regexp]
ClassName
RoleName
lib/Aion/Types.md view on Meta::CPAN
Ref
Tied`[class]
LValueRef
FormatRef
CodeRef
NamedCode[subname]
ProtoCode[prototype]
ForwardRef
ImplementRef
Isa[A...]
RegexpRef
ValueRef`[A]
ScalarRef`[A]
RefRef`[A]
GlobRef
FileHandle
ArrayRef`[A]
HashRef`[A]
Object`[class]
Me
Rat
Map[A => B]
Tuple[A...]
CycleTuple[A...]
Dict[k => A, ...]
RegexpLike
CodeLike
ArrayLike`[A]
Lim[from, to?]
HashLike`[A]
HasProp[p...]
LimKeys[from, to?]
Like
HasMethods[m...]
Overload`[m...]
InstanceOf[class...]
ConsumerOf[role...]
StrLike
Len[from, to?]
NumLike
Float
Double
Range[from, to]
Bytes[n]
PositiveBytes[n]
```
# SUBROUTINES
## subtype ($name, @paraphernalia)
СоздаÑÑ Ð½Ð¾Ð²Ñй Ñип.
```perl
BEGIN {
subtype One => where { $_ == 1 } message { "Actual 1 only!" };
}
1 ~~ One # -> 1
0 ~~ One # -> ""
eval { One->validate(0) }; $@ # ~> Actual 1 only!
```
`where` и `message` â ÑÑо ÑинÑакÑиÑеÑкий ÑаÑ
аÑ, а `subtype` можно иÑполÑзоваÑÑ Ð±ÐµÐ· ниÑ
.
```perl
BEGIN {
subtype Many => (where => sub { $_ > 1 });
}
2 ~~ Many # -> 1
eval { subtype Many => (where1 => sub { $_ > 1 }) }; $@ # ~> subtype Many unused keys left: where1
eval { subtype 'Many' }; $@ # ~> subtype Many: main::Many exists!
```
## as ($super_type)
ÐÑполÑзÑеÑÑÑ Ñ `subtype` Ð´Ð»Ñ ÑаÑÑиÑÐµÐ½Ð¸Ñ Ñоздаваемого Ñипа `$super_type`.
## init_where ($code)
ÐниÑиализиÑÑÐµÑ Ñип Ñ Ð½Ð¾Ð²Ñми аÑгÑменÑами. ÐÑполÑзÑеÑÑÑ Ñ `subtype`.
```perl
BEGIN {
subtype 'LessThen[n]',
init_where { Num->validate(A, "Argument LessThen[n]") }
where { $_ < A };
}
eval { LessThen["string"] }; $@ # ^=> Argument LessThen[n]
5 ~~ LessThen[5] # -> ""
```
## where ($code)
ÐÑполÑзÑÐµÑ `$code` как ÑеÑÑ. ÐнаÑение Ð´Ð»Ñ ÑеÑÑа пеÑедаÑÑÑÑ Ð² `$_`.
```perl
BEGIN {
subtype 'Two',
where { $_ == 2 };
}
2 ~~ Two # -> 1
3 ~~ Two # -> ""
```
## awhere ($code)
ÐÑполÑзÑеÑÑÑ Ñ `subtype`.
ÐÑли Ñип Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ñ Ð°ÑгÑменÑами и без, Ñо иÑполÑзÑеÑÑÑ Ð´Ð»Ñ Ð¿ÑовеÑки набоÑа Ñ Ð°ÑгÑменÑами, а `where` â без.
```perl
BEGIN {
subtype 'GreatThen`[num]',
where { $_ > 0 }
awhere { $_ > A }
;
}
0 ~~ GreatThen # -> ""
1 ~~ GreatThen # -> 1
3 ~~ GreatThen[3] # -> ""
4 ~~ GreatThen[3] # -> 1
```
ÐеобÑ
одимо, еÑли аÑгÑменÑÑ Ð½ÐµÐ¾Ð±ÑзаÑелÑнÑ.
```perl
subtype 'Ex`[a]', where {} # @-> subtype Ex`[a]: needs an awhere
subtype 'Ex', awhere {} # @-> subtype Ex: awhere is excess
BEGIN {
subtype 'MyEnum`[item...]',
as Str,
awhere { $_ ~~ scalar ARGS }
;
}
"ab" ~~ MyEnum[qw/ab cd/] # -> 1
```
## SELF
( run in 3.703 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )