NAME

Aion::Types is a library of standard validators and is used to create new validators

SYNOPSIS

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

This module exports routines:

  • subtype, as, init_where, where, awhere, message - for creating validators.

  • SELF, ARGS, A, B, C, D, M, N - for use in validators of a type and its arguments.

  • coerce, from, via - to create a value converter from one class to another.

Validator hierarchy:

Any
	Control
		Union[A, B...]
		Intersection[A, B...]
		Exclude[A, B...]
		Option[A]
		Wantarray[A, S]
	Item
		Bool
		BoolLike
		Enum[A...]
		Maybe[A]
		Undef
		Defined
			Value
				Version
				Str
					Uni
					Bin
					NonEmptyStr
					StartsWith
					EndsWith
					Email
					Tel
					Url
					Path
					Html
					StrDate
					StrDateTime
					StrMatch[qr/.../]
					ClassName[A]
					RoleName[A]
					Rat
					Num
						PositiveNum
						Int
							PositiveInt
							Nat
			Ref
				Tied`[A]
				LValueRef
				FormatRef
				CodeRef`[name, proto]
					ReachableCodeRef`[name, proto]
					UnreachableCodeRef`[name, proto]
				RegexpRef
				ScalarRefRef`[A]
					RefRef`[A]
					ScalarRef`[A]
				GlobRef
					FileHandle
				ArrayRef`[A]
				HashRef`[H]
				Object`[O]
					Me
				Map[K, V]
				Tuple[A...]
				CycleTuple[A...]
				Dict[k => A, ...]
				RegexpLike
				CodeLike
				ArrayLike`[A]
					Lim[A, B?]
				HashLike`[A]
					HasProp[p...]
					LimKeys[A, B?]
			Like
				HasMethods[m...]
				Overload`[m...]
				InstanceOf[A...]
				ConsumerOf[A...]
				StrLike
					Len[A, B?]
				NumLike
					Float
					Double
					Range[from, to]
					Bytes[A, B?]
					PositiveBytes[A, B?]

SUBROUTINES

subtype ($name, @paraphernalia)

Creates a new type.

BEGIN {
	subtype One => where { $_ == 1 } message { "Actual 1 only!" };
}

1 ~~ One	 # -> 1
0 ~~ One	 # -> ""
eval { One->validate(0) }; $@ # ~> Actual 1 only!

where and message are syntactic sugar, and subtype can be used without them.

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)

Used with subtype to extend the created $super_type type.

init_where ($code)

Initializes a type with new arguments. Used with subtype.

BEGIN {
	subtype 'LessThen[A]',
		init_where { Num->validate(A, "Argument LessThen[A]") }
		where { $_ < A };
}

eval { LessThen["string"] }; $@  # ~> Argument LessThen\[A\]

5 ~~ LessThen[5]  # -> ""

where ($code)

Uses $code as a test. The value for the test is passed to $_.

BEGIN {
	subtype 'Two',
		where { $_ == 2 };
}

2 ~~ Two # -> 1
3 ~~ Two # -> ""

Used with subtype. Required if the type has arguments.

subtype 'Ex[A]' # @-> subtype Ex[A]: needs a where

awhere ($code)

Used with subtype.

If the type can be with or without arguments, then it is used to check the set with arguments, and where - without.

BEGIN {
	subtype 'GreatThen`[A]',
		where { $_ > 0 }
		awhere { $_ > A }
	;
}

0 ~~ GreatThen # -> ""
1 ~~ GreatThen # -> 1

3 ~~ GreatThen[3] # -> ""
4 ~~ GreatThen[3] # -> 1

Required if arguments are optional.

subtype 'Ex`[A]', where {} # @-> subtype Ex`[A]: needs a awhere
subtype 'Ex', awhere {} # @-> subtype Ex: awhere is excess

BEGIN {
	subtype 'MyEnum`[A...]',
		as Str,
		awhere { $_ ~~ scalar ARGS }
	;
}

"ab" ~~ MyEnum[qw/ab cd/] # -> 1

SELF

Current type. SELF is used in init_where, where and awhere.

ARGS

Arguments of the current type. In a scalar context, it returns a reference to an array, and in an array context, it returns a list. Used in init_where, where and awhere.

A, B, C, D

The first, second, third and fifth type arguments.

BEGIN {
	subtype "Seria[A,B,C,D]", where { A < B && B < $_ && $_ < C && C < D };
}

2.5 ~~ Seria[1,2,3,4] # -> 1

Used in init_where, where and awhere.

M, N

M and N are shorthand for SELF->{M} and SELF->{N}.

BEGIN {
	subtype "BeginAndEnd[A, B]",
		init_where {
			N = qr/^${\ quotemeta A}/;
			M = qr/${\ quotemeta B}$/;
		}
		where { $_ =~ N && $_ =~ M };
}

"Hi, my dear!" ~~ BeginAndEnd["Hi,", "!"]; # -> 1
"Hi my dear!" ~~ BeginAndEnd["Hi,", "!"];  # -> ""

"" . BeginAndEnd["Hi,", "!"] # => BeginAndEnd['Hi,', '!']

message ($code)

Used with subtype to print an error message if the value excludes the type. $code uses: SELF - the current type, ARGS, A, B, C, D - type arguments (if any) and a test value in $_. It can be converted to a string using SELF->val_to_str($_).

coerce ($type, from => $from, via => $via)

Adds a new cast ($via) to $type from $from type.

BEGIN {subtype Four => where {4 eq $_}}

"4a" ~~ Four # -> ""

Four->coerce("4a") # -> "4a"

coerce Four, from Str, via { 0+$_ };

Four->coerce("4a")	# -> 4

coerce Four, from ArrayRef, via { scalar @$_ };

Four->coerce([1,2,3])           # -> 3
Four->coerce([1,2,3]) ~~ Four   # -> ""
Four->coerce([1,2,3,4]) ~~ Four # -> 1

coerce throws exceptions:

eval {coerce Int, via1 => 1}; $@  # ~> coerce Int unused keys left: via1
eval {coerce "x"}; $@  # ~> coerce x not Aion::Type!
eval {coerce Int}; $@  # ~> coerce Int: from is'nt Aion::Type!
eval {coerce Int, from "x"}; $@  # ~> coerce Int: from is'nt Aion::Type!
eval {coerce Int, from Num}; $@  # ~> coerce Int: via is not subroutine!
eval {coerce Int, (from=>Num, via=>"x")}; $@  # ~> coerce Int: via is not subroutine!

Standard casts:

# Str from Undef — empty string
Str->coerce(undef) # -> ""

# Int from Num — rounded integer
Int->coerce(2.5)  # -> 3
Int->coerce(-2.5) # -> -3

# Bool from Any — 1 or ""
Bool->coerce([]) # -> 1
Bool->coerce(0)  # -> ""

from ($type)

Syntactic sugar for coerce.

via ($code)

Syntactic sugar for coerce.

ATTRIBUTES

:Isa (@signature)

Checks the signature of a subroutine: arguments and results.

sub minint($$) : Isa(Int => Int => Int) {
	my ($x, $y) = @_;
	$x < $y? $x : $y
}

minint 6, 5; # -> 5
eval {minint 5.5, 2}; $@ # ~> Arguments of method `minint` must have the type Tuple\[Int, Int\]\.

sub half($) : Isa(Int => Int) {
	my ($x) = @_;
	$x / 2
}

half 4; # -> 2
eval {half 5}; $@ # ~> Return of method `half` must have the type Int. The it is 2.5

TYPES

Any

The top level type in the hierarchy. Compares everything.

Control

The top-level type in hierarchy constructors creates new types from any types.

Union[A, B...]

Union of several types. Similar to the $type1 | $type2.

33  ~~ Union[Int, Ref] # -> 1
[]  ~~ Union[Int, Ref]	# -> 1
"a" ~~ Union[Int, Ref]	# -> ""

Intersection[A, B...]

The intersection of several types. Similar to the $type1 & $type2 operator.

15 ~~ Intersection[Int, StrMatch[/5/]] # -> 1

Exclude[A, B...]

Exclusion of several types. Similar to the ~$type operator.

-5  ~~ Exclude[PositiveInt] # -> 1
"a" ~~ Exclude[PositiveInt] # -> 1
5   ~~ Exclude[PositiveInt] # -> ""
5.5 ~~ Exclude[PositiveInt] # -> 1

If Exclude has many arguments, then it is analogous to ~ ($type1 | $type2 ...).

-5  ~~ Exclude[PositiveInt, Enum[-2]] # -> 1
-2  ~~ Exclude[PositiveInt, Enum[-2]] # -> ""
0   ~~ Exclude[PositiveInt, Enum[-2]] # -> ""

Option[A]

Additional keys in Dict.

{a=>55} ~~ Dict[a=>Int, b => Option[Int]]          # -> 1
{a=>55, b=>31} ~~ Dict[a=>Int, b => Option[Int]]   # -> 1
{a=>55, b=>31.5} ~~ Dict[a=>Int, b => Option[Int]] # -> ""

Wantarray[A, S]

If the routine returns different values in array and scalar contexts, then the Wantarray type is used with type A for the array context and type S for the scalar context.

sub arr : Isa(PositiveInt => Wantarray[ArrayRef[PositiveInt], PositiveInt]) {
	my ($n) = @_;
	wantarray? 1 .. $n: $n
}

my @a = arr(3);
my $s = arr(3);

\@a # --> [1,2,3]
$s  # -> 3

Item

The top-level type in the hierarchy of scalar types.

Bool

1 is true. 0, "" or undef is false.

1 ~~ Bool  # -> 1
0 ~~ Bool  # -> 1
undef ~~ Bool # -> 1
"" ~~ Bool # -> 1

2 ~~ Bool  # -> ""
[] ~~ Bool # -> ""

Enum[A...]

Enumeration.

3 ~~ Enum[1,2,3]   # -> 1
"cat" ~~ Enum["cat", "dog"] # -> 1
4 ~~ Enum[1,2,3]   # -> ""

Maybe[A]

undef or type in [].

undef ~~ Maybe[Int] # -> 1
4 ~~ Maybe[Int]     # -> 1
"" ~~ Maybe[Int]    # -> ""

Undef

Only undef.

undef ~~ Undef # -> 1
0 ~~ Undef     # -> ""

Defined

Everything except undef.

\0 ~~ Defined    # -> 1
undef ~~ Defined # -> ""

Value

Defined values without references.

3 ~~ Value  # -> 1
\3 ~~ Value    # -> ""
undef ~~ Value # -> ""

Len[A, B?]

Specifies a length value from A to B, or from 0 to A if B is missing.

"1234" ~~ Len[3]   # -> ""
"123" ~~ Len[3]    # -> 1
"12" ~~ Len[3]     # -> 1
"" ~~ Len[1, 2]    # -> ""
"1" ~~ Len[1, 2]   # -> 1
"12" ~~ Len[1, 2]  # -> 1
"123" ~~ Len[1, 2] # -> ""

Version

Perl version.

1.1.0 ~~ Version   # -> 1
v1.1.0 ~~ Version  # -> 1
v1.1 ~~ Version    # -> 1
v1 ~~ Version      # -> 1
1.1 ~~ Version     # -> ""
"1.1.0" ~~ Version # -> ""

Str

Strings, including numbers.

1.1 ~~ Str   # -> 1
"" ~~ Str    # -> 1
1.1.0 ~~ Str # -> ""

Uni

Unicode strings with the utf8 flag or if decoding to utf8 occurs without errors.

"↭" ~~ Uni # -> 1
123 ~~ Uni # -> ""
do {no utf8; "↭" ~~ Uni} # -> 1

Bin

Binary strings without the utf8 flag and octets with numbers less than 128.

123 ~~ Bin # -> 1
"z" ~~ Bin # -> 1
"↭" ~~ Bin # -> ""
do {no utf8; "↭" ~~ Bin }   # -> ""

StartsWith[S]

The line starts with S.

"Hi, world!" ~~ StartsWith["Hi,"] # -> 1
"Hi world!" ~~ StartsWith["Hi,"] # -> ""

EndsWith[S]

The line ends with S.

"Hi, world!" ~~ EndsWith["world!"] # -> 1
"Hi, world" ~~ EndsWith["world!"]  # -> ""

NonEmptyStr

A string containing one or more non-blank characters.

" " ~~ NonEmptyStr              # -> ""
" S " ~~ NonEmptyStr            # -> 1
" S " ~~ (NonEmptyStr & Len[2]) # -> ""

Email

Lines with @.

'@' ~~ Email     # -> 1
'a@a.a' ~~ Email # -> 1
'a.a' ~~ Email   # -> ""

Tel

The telephone format is a plus sign and seven or more digits.

"+1234567" ~~ Tel # -> 1
"+1234568" ~~ Tel # -> 1
"+ 1234567" ~~ Tel # -> ""
"+1234567 " ~~ Tel # -> ""

Url

Website URLs are a string prefixed with http:// or https://.

"http://" ~~ Url # -> 1
"http:/" ~~ Url  # -> ""

Path

Paths start with a slash.

"/" ~~ Path  # -> 1
"/a/b" ~~ Path  # -> 1
"a/b" ~~ Path   # -> ""

Html

HTML starts with <!doctype html or <html.

"<HTML" ~~ Html   # -> 1
" <html" ~~ Html     # -> 1
" <!doctype html>" ~~ Html # -> 1
" <html1>" ~~ Html   # -> ""

StrDate

Date in yyyy-mm-dd format.

"2001-01-12" ~~ StrDate # -> 1
"01-01-01" ~~ StrDate   # -> ""

StrDateTime

Date and time in the format yyyy-mm-dd HH:MM:SS.

"2012-12-01 00:00:00" ~~ StrDateTime  # -> 1
"2012-12-01 00:00:00 " ~~ StrDateTime # -> ""

StrMatch[qr/.../]

Matches a string against a regular expression.

' abc ' ~~ StrMatch[qr/abc/]  # -> 1
' abbc ' ~~ StrMatch[qr/abc/] # -> ""

ClassName

The class name is a package with a new method.

'Aion::Type' ~~ ClassName  # -> 1
'Aion::Types' ~~ ClassName # -> ""

RoleName

The role name is a package without the new method, with @ISA, or with any one method.

package ExRole1 {
	sub any_method {}
}

package ExRole2 {
	our @ISA = qw/ExRole1/;
}


'ExRole1' ~~ RoleName    # -> 1
'ExRole2' ~~ RoleName    # -> 1
'Aion::Type' ~~ RoleName # -> ""
'Nouname::Empty::Package' ~~ RoleName # -> ""

Rat

Rational numbers.

"6/7" ~~ Rat  # -> 1
"-6/7" ~~ Rat # -> 1
6 ~~ Rat      # -> 1
"inf" ~~ Rat  # -> 1
"+Inf" ~~ Rat # -> 1
"NaN" ~~ Rat  # -> 1
"-nan" ~~ Rat # -> 1
6.5 ~~ Rat    # -> 1
"6.5 " ~~ Rat # -> ''

Num

Numbers.

-6.5 ~~ Num   # -> 1
6.5e-7 ~~ Num # -> 1
"6.5 " ~~ Num # -> ""

PositiveNum

Positive numbers.

0 ~~ PositiveNum    # -> 1
0.1 ~~ PositiveNum  # -> 1
-0.1 ~~ PositiveNum # -> ""
-0 ~~ PositiveNum   # -> 1

Float

A machine floating point number is 4 bytes.

-4.8 ~~ Float             # -> 1
-3.402823466E+38 ~~ Float # -> 1
+3.402823466E+38 ~~ Float # -> 1
-3.402823467E+38 ~~ Float # -> ""

Double

A machine floating point number is 8 bytes.

use Scalar::Util qw//;

                      -4.8 ~~ Double # -> 1
'-1.7976931348623157e+308' ~~ Double # -> 1
'+1.7976931348623157e+308' ~~ Double # -> 1
'-1.7976931348623159e+308' ~~ Double # -> ""

Range[from, to]

Numbers between from and to.

1 ~~ Range[1, 3]   # -> 1
2.5 ~~ Range[1, 3] # -> 1
3 ~~ Range[1, 3]   # -> 1
3.1 ~~ Range[1, 3] # -> ""
0.9 ~~ Range[1, 3] # -> ""

Int

Whole numbers.

123 ~~ Int	# -> 1
-12 ~~ Int	# -> 1
5.5 ~~ Int	# -> ""

Bytes[N]

Calculates the maximum and minimum numbers that will fit in N bytes and checks the constraint between them.

-129 ~~ Bytes[1] # -> ""
-128 ~~ Bytes[1] # -> 1
127 ~~ Bytes[1]  # -> 1
128 ~~ Bytes[1]  # -> ""

# 2 bits power of (8 bits * 8 bytes - 1)
my $N = 1 << (8*8-1);
(-$N-1) ~~ Bytes[8] # -> ""
(-$N) ~~ Bytes[8]   # -> 1
($N-1) ~~ Bytes[8]  # -> 1
$N ~~ Bytes[8]      # -> ""

require Math::BigInt;

my $N17 = 1 << (8*Math::BigInt->new(17) - 1);

((-$N17-1) . "") ~~ Bytes[17] # -> ""
(-$N17 . "") ~~ Bytes[17]     # -> 1
(($N17-1) . "") ~~ Bytes[17]  # -> 1
($N17 . "") ~~ Bytes[17]      # -> ""

PositiveInt

Positive integers.

+0 ~~ PositiveInt # -> 1
-0 ~~ PositiveInt # -> 1
55 ~~ PositiveInt # -> 1
-1 ~~ PositiveInt # -> ""

PositiveBytes[N]

Calculates the maximum number that will fit in N bytes (assuming there is no negative bit in the bytes) and checks the limit from 0 to that number.

-1 ~~ PositiveBytes[1]  # -> ""
0 ~~ PositiveBytes[1]   # -> 1
255 ~~ PositiveBytes[1] # -> 1
256 ~~ PositiveBytes[1] # -> ""

-1 ~~ PositiveBytes[8]   # -> ""
1.01 ~~ PositiveBytes[8] # -> ""
0 ~~ PositiveBytes[8]    # -> 1

my $N8 = 2 ** (8*Math::BigInt->new(8)) - 1;

$N8 . "" ~~ PositiveBytes[8]     # -> 1
($N8+1) . "" ~~ PositiveBytes[8] # -> ""

-1 ~~ PositiveBytes[17] # -> ""
0 ~~ PositiveBytes[17]  # -> 1

Nat

Integers 1+.

0 ~~ Nat	# -> ""
1 ~~ Nat	# -> 1

Ref

Link.

\1 ~~ Ref # -> 1
[] ~~ Ref # -> 1
1 ~~ Ref  # -> ""

Tied`[A]

Link to the associated variable.

package TiedHash { sub TIEHASH { bless {@_}, shift } }
package TiedArray { sub TIEARRAY { bless {@_}, shift } }
package TiedScalar { sub TIESCALAR { bless {@_}, shift } }

tie my %a, "TiedHash";
tie my @a, "TiedArray";
tie my $a, "TiedScalar";
my %b; my @b; my $b;

\%a ~~ Tied # -> 1
\@a ~~ Tied # -> 1
\$a ~~ Tied # -> 1

\%b ~~ Tied  # -> ""
\@b ~~ Tied  # -> ""
\$b ~~ Tied  # -> ""
\\$b ~~ Tied # -> ""

ref tied %a     # => TiedHash
ref tied %{\%a} # => TiedHash

\%a ~~ Tied["TiedHash"]   # -> 1
\@a ~~ Tied["TiedArray"]  # -> 1
\$a ~~ Tied["TiedScalar"] # -> 1

\%a ~~ Tied["TiedArray"]   # -> ""
\@a ~~ Tied["TiedScalar"]  # -> ""
\$a ~~ Tied["TiedHash"]    # -> ""
\\$a ~~ Tied["TiedScalar"] # -> ""

LValueRef

The function allows assignment.

ref \substr("abc", 1, 2) # => LVALUE
ref \vec(42, 1, 2) # => LVALUE

\substr("abc", 1, 2) ~~ LValueRef # -> 1
\vec(42, 1, 2) ~~ LValueRef # -> 1

But it doesn't work with :lvalue.

sub abc: lvalue { $_ }

abc() = 12;
$_ # => 12
ref \abc()  # => SCALAR
\abc() ~~ LValueRef	# -> ""


package As {
	sub x : lvalue {
		shift->{x};
	}
}

my $x = bless {}, "As";
$x->x = 10;

$x->x # => 10
$x	# --> bless {x=>10}, "As"

ref \$x->x			 # => SCALAR
\$x->x ~~ LValueRef # -> ""

And on the end:

\1 ~~ LValueRef	# -> ""

my $x = "abc";
substr($x, 1, 1) = 10;

$x # => a10c

LValueRef->include( \substr($x, 1, 1) )	# => 1

FormatRef

Format.

format EXAMPLE_FMT =
@<<<<<<   @||||||   @>>>>>>
"left",   "middle", "right"
.

*EXAMPLE_FMT{FORMAT} ~~ FormatRef   # -> 1
\1 ~~ FormatRef				# -> ""

CodeRef`[name, proto]

Subroutine.

sub {} ~~ CodeRef	# -> 1
\1 ~~ CodeRef		# -> ""

sub code_ex ($;$) { ... }

\&code_ex ~~ CodeRef['main::code_ex']         # -> 1
\&code_ex ~~ CodeRef['code_ex']               # -> ""
\&code_ex ~~ CodeRef[qr/_/]                   # -> 1
\&code_ex ~~ CodeRef[undef, '$;$']            # -> 1
\&code_ex ~~ CodeRef[undef, qr/^(\$;\$|\@)$/] # -> 1
\&code_ex ~~ CodeRef[undef, '@']              # -> ""
\&code_ex ~~ CodeRef['main::code_ex', '$;$']  # -> 1

ReachableCodeRef`[name, proto]

Subroutine with body.

sub code_forward ($;$);

\&code_ex ~~ ReachableCodeRef['main::code_ex']        # -> 1
\&code_ex ~~ ReachableCodeRef['code_ex']              # -> ""
\&code_ex ~~ ReachableCodeRef[qr/_/]                  # -> 1
\&code_ex ~~ ReachableCodeRef[undef, '$;$']           # -> 1
\&code_ex ~~ CodeRef[undef, qr/^(\$;\$|\@)$/]         # -> 1
\&code_ex ~~ ReachableCodeRef[undef, '@']             # -> ""
\&code_ex ~~ ReachableCodeRef['main::code_ex', '$;$'] # -> 1

\&code_forward ~~ ReachableCodeRef # -> ""

UnreachableCodeRef`[name, proto]

Subroutine without body.

\&nouname ~~ UnreachableCodeRef # -> 1
\&code_ex ~~ UnreachableCodeRef # -> ""
\&code_forward ~~ UnreachableCodeRef['main::code_forward', '$;$'] # -> 1

Isa[A...]

A link to a subroutine with the corresponding signature.

sub sig_ex :Isa(Int => Str) {}

\&sig_ex ~~ Isa[Int => Str]        # -> 1
\&sig_ex ~~ Isa[Int => Str => Num] # -> ""
\&sig_ex ~~ Isa[Int => Num]        # -> ""

Subroutines without a body are not wrapped in a signature handler, and the signature is remembered to validate the conformity of a subsequently declared subroutine with a body. Therefore the function has no signature.

sub unreachable_sig_ex :Isa(Int => Str);

\&unreachable_sig_ex ~~ Isa[Int => Str] # -> ""

RegexpRef

Regular expression.

qr// ~~ RegexpRef # -> 1
\1 ~~ RegexpRef   # -> ""

ScalarRefRef`[A]

A reference to a scalar or a reference to a reference.

\12    ~~ ScalarRefRef                    # -> 1
\12    ~~ ScalarRefRef                    # -> 1
\-1.2  ~~ ScalarRefRef[Num]               # -> 1
\\-1.2 ~~ ScalarRefRef[ScalarRefRef[Num]] # -> 1

ScalarRef`[A]

Reference to a scalar.

\12   ~~ ScalarRef      # -> 1
\\12  ~~ ScalarRef      # -> ""
\-1.2 ~~ ScalarRef[Num] # -> 1

RefRef`[A]

Link to link.

\12    ~~ RefRef                 # -> ""
\\12   ~~ RefRef                 # -> 1
\-1.2  ~~ RefRef[Num]            # -> ""
\\-1.2 ~~ RefRef[ScalarRef[Num]] # -> 1

GlobRef

Link to global

\*A::a ~~ GlobRef # -> 1
*A::a ~~ GlobRef  # -> ""

FileHandle

File descriptor.

\*A::a ~~ FileHandle         # -> ""
\*STDIN ~~ FileHandle        # -> 1

open my $fh, "<", "/dev/null";
$fh ~~ FileHandle	         # -> 1
close $fh;

opendir my $dh, ".";
$dh ~~ FileHandle	         # -> 1
closedir $dh;

use constant { PF_UNIX => 1, SOCK_STREAM => 1 };

socket my $sock, PF_UNIX, SOCK_STREAM, 0;
$sock ~~ FileHandle	         # -> 1
close $sock;

ArrayRef`[A]

Array references.

[] ~~ ArrayRef	# -> 1
{} ~~ ArrayRef	# -> ""
[] ~~ ArrayRef[Num]	# -> 1
{} ~~ ArrayRef[Num]	# -> ''
[1, 1.1] ~~ ArrayRef[Num]	# -> 1
[1, undef] ~~ ArrayRef[Num]	# -> ""

Lim[A, B?]

Limits arrays from A to B elements, or from 0 to A if B is missing.

[] ~~ Lim[5]     # -> 1
[1..5] ~~ Lim[5] # -> 1
[1..6] ~~ Lim[5] # -> ""

[1..5] ~~ Lim[1,5] # -> 1
[1..6] ~~ Lim[1,5] # -> ""

[1] ~~ Lim[1,5] # -> 1
[] ~~ Lim[1,5]  # -> ""

HashRef`[H]

Links to hashes.

{} ~~ HashRef # -> 1
\1 ~~ HashRef # -> ""

[]  ~~ HashRef[Int]           # -> ""
{x=>1, y=>2}  ~~ HashRef[Int] # -> 1
{x=>1, y=>""} ~~ HashRef[Int] # -> ""

Object`[O]

Blessed links.

bless(\(my $val=10), "A1") ~~ Object # -> 1
\(my $val=10) ~~ Object              # -> ""

bless(\(my $val=10), "A1") ~~ Object["A1"] # -> 1
bless(\(my $val=10), "A1") ~~ Object["B1"] # -> ""

Me

Blessed references to objects of the current package.

package A1 {
 use Aion;
 bless({}, __PACKAGE__) ~~ Me  # -> 1
 bless({}, "A2") ~~ Me         # -> ""
}

Map[K, V]

Like HashRef, but with a key type.

{} ~~ Map[Int, Int]               # -> 1
{5 => 3} ~~ Map[Int, Int]         # -> 1
+{5.5 => 3} ~~ Map[Int, Int]      # -> ""
{5 => 3.3} ~~ Map[Int, Int]       # -> ""
{5 => 3, 6 => 7} ~~ Map[Int, Int] # -> 1

Tuple[A...]

Tuple.

["a", 12] ~~ Tuple[Str, Int]    # -> 1
["a", 12, 1] ~~ Tuple[Str, Int] # -> ""
["a", 12.1] ~~ Tuple[Str, Int]  # -> ""

CycleTuple[A...]

Tuple repeated one or more times.

["a", -5] ~~ CycleTuple[Str, Int] # -> 1
["a", -5, "x"] ~~ CycleTuple[Str, Int] # -> ""
["a", -5, "x", -6] ~~ CycleTuple[Str, Int] # -> 1
["a", -5, "x", -6.2] ~~ CycleTuple[Str, Int] # -> ""

Dict[k => A, ...]

Dictionary.

{a => -1.6, b => "abc"} ~~ Dict[a => Num, b => Str] # -> 1

{a => -1.6, b => "abc", c => 3} ~~ Dict[a => Num, b => Str] # -> ""
{a => -1.6} ~~ Dict[a => Num, b => Str] # -> ""

{a => -1.6} ~~ Dict[a => Num, b => Option[Str]] # -> 1

HasProp[p...]

A hash has the following properties. In addition to them, he may have others.

[0, 1] ~~ HasProp[qw/0 1/] # -> ""

{a => 1, b => 2, c => 3} ~~ HasProp[qw/a b/] # -> 1
{a => 1, b => 2} ~~ HasProp[qw/a b/] # -> 1
{a => 1, c => 3} ~~ HasProp[qw/a b/] # -> ""

bless({a => 1, b => 3}, "A") ~~ HasProp[qw/a b/] # -> 1

Like

Object or string.

"" ~~ Like # -> 1
1 ~~ Like  # -> 1
bless({}, "A") ~~ Like # -> 1
bless([], "A") ~~ Like # -> 1
bless(\(my $str = ""), "A") ~~ Like # -> 1
\1 ~~ Like  # -> ""

HasMethods[m...]

An object or class has listed methods. In addition to them, there may be others.

package HasMethodsExample {
	sub x1 {}
	sub x2 {}
}

"HasMethodsExample" ~~ HasMethods[qw/x1 x2/]			# -> 1
bless({}, "HasMethodsExample") ~~ HasMethods[qw/x1 x2/] # -> 1
bless({}, "HasMethodsExample") ~~ HasMethods[qw/x1/]	# -> 1
"HasMethodsExample" ~~ HasMethods[qw/x3/]				# -> ""
"HasMethodsExample" ~~ HasMethods[qw/x1 x2 x3/]			# -> ""
"HasMethodsExample" ~~ HasMethods[qw/x1 x3/]			# -> ""

Overload`[op...]

An object or class with overloaded operators.

package OverloadExample {
	use overload '""' => sub { "abc" };
}

"OverloadExample" ~~ Overload            # -> 1
bless({}, "OverloadExample") ~~ Overload # -> 1
"A" ~~ Overload                          # -> ""
bless({}, "A") ~~ Overload               # -> ""

And it has operators specified operators.

"OverloadExample" ~~ Overload['""'] # -> 1
"OverloadExample" ~~ Overload['|']  # -> ""

InstanceOf[A...]

A class or object inherits classes from a list.

package Animal {}
package Cat { our @ISA = qw/Animal/ }
package Tiger { our @ISA = qw/Cat/ }


"Tiger" ~~ InstanceOf['Animal', 'Cat'] # -> 1
"Tiger" ~~ InstanceOf['Tiger']         # -> 1
"Tiger" ~~ InstanceOf['Cat', 'Dog']    # -> ""

ConsumerOf[A...]

A class or object has the specified roles.

package NoneExample {}
package RoleExample { sub DOES { $_[1] ~~ [qw/Role1 Role2/] } }

'RoleExample' ~~ ConsumerOf[qw/Role1/] # -> 1
'RoleExample' ~~ ConsumerOf[qw/Role2 Role1/] # -> 1
bless({}, 'RoleExample') ~~ ConsumerOf[qw/Role3 Role2 Role1/] # -> ""

'NoneExample' ~~ ConsumerOf[qw/Role1/] # -> ""

BoolLike

Tests for 1, 0, "", undef, or an object with an overloaded bool or 0+ operator as JSON::PP::Boolean. In the second case, it calls the 0+ operator and checks the result as Bool.

BoolLike calls the 0+ operator and checks the result.

package BoolLikeExample {
	use overload '0+' => sub { ${$_[0]} };
}

bless(\(my $x = 1 ), 'BoolLikeExample') ~~ BoolLike # -> 1
bless(\(my $x = 11), 'BoolLikeExample') ~~ BoolLike # -> ""

1 ~~ BoolLike     # -> 1
0 ~~ BoolLike     # -> 1
"" ~~ BoolLike    # -> 1
undef ~~ BoolLike # -> 1

package BoolLike2Example {
	use overload 'bool' => sub { ${$_[0]} };
}

bless(\(my $x = 1 ), 'BoolLike2Example') ~~ BoolLike # -> 1
bless(\(my $x = 11), 'BoolLike2Example') ~~ BoolLike # -> 1

StrLike

A string or object overloaded with the "" operator.

"" ~~ StrLike # -> 1

package StrLikeExample {
	use overload '""' => sub { "abc" };
}

bless({}, "StrLikeExample") ~~ StrLike # -> 1

{} ~~ StrLike # -> ""

RegexpLike

A regular expression or object with an overload of the qr operator.

ref(qr//)  # => Regexp
Scalar::Util::reftype(qr//) # => REGEXP

my $regex = bless qr//, "A";
Scalar::Util::reftype($regex) # => REGEXP

$regex ~~ RegexpLike # -> 1
qr// ~~ RegexpLike   # -> 1
"" ~~ RegexpLike     # -> ""

package RegexpLikeExample {
 use overload 'qr' => sub { qr/abc/ };
}

"RegexpLikeExample" ~~ RegexpLike # -> ""
bless({}, "RegexpLikeExample") ~~ RegexpLike # -> 1

CodeLike

A subroutine or object with an overload of the &{} operator.

sub {} ~~ CodeLike     # -> 1
\&CodeLike ~~ CodeLike # -> 1
{} ~~ CodeLike         # -> ""

ArrayLike`[A]

Arrays or objects with an overloaded operator or @{}.

{} ~~ ArrayLike      # -> ""
{} ~~ ArrayLike[Int] # -> ""

[] ~~ ArrayLike # -> 1

package ArrayLikeExample {
	use overload '@{}' => sub {
		shift->{array} //= []
	};
}

my $x = bless {}, 'ArrayLikeExample';
$x->[1] = 12;
$x->{array} # --> [undef, 12]

$x ~~ ArrayLike # -> 1

$x ~~ ArrayLike[Int] # -> ""

$x->[0] = 13;
$x ~~ ArrayLike[Int] # -> 1

HashLike`[A]

Hashes or objects with the %{} operator overloaded.

{} ~~ HashLike  # -> 1
[] ~~ HashLike  # -> ""
[] ~~ HashLike[Int] # -> ""

package HashLikeExample {
	use overload '%{}' => sub {
		shift->[0] //= {}
	};
}

my $x = bless [], 'HashLikeExample';
$x->{key} = 12.3;
$x->[0]  # --> {key => 12.3}

$x ~~ HashLike      # -> 1
$x ~~ HashLike[Int] # -> ""
$x ~~ HashLike[Num] # -> 1

AUTHOR

Yaroslav O. Kosmina Lmailto:dart@cpan.org

LICENSE

GPLv3

COPYRIGHT

The Aion::Types module is copyright © 2023 Yaroslav O. Kosmina. Rusland. All rights reserved.