Type::Tiny::Manual::UsingWithOther - Online Linux Manual PageSection : 3pm
Updated : 2021-02-24
Source : perl v5.32.1
Note : User Contributed Perl Documentation
NAMEType::Tiny::Manual::UsingWithOther − using Type::Tiny with Class::InsideOut, Params::Check, and Object::Accessor.
MANUALThe antlers crew aren't the only object-oriented programming toolkits in Perl town. Although Type::Tiny might have been built with Moose, Mouse, and Moo in mind, it can be used with other toolkits. These toolkits are... well... hmm... okay... they exist. If you are starting a new project, there's very little reason not to use Class::Tiny, Moo, or Moose. So you're probably okay to skip this part of the fine manual and go straight to Type::Tiny::Manual::UsingWithTestMore.
Class::InsideOutYou want Class::InsideOut 1.13 or above, which has support for blessed and overloaded objects (including Type::Tiny type constraints) for the get_hook and set_hook options. package Person {
use Class::InsideOut qw( public );
use Types::Standard qw( Str Int );
use Types::Common::Numeric qw( PositiveInt );
use Type::Params qw( compile );
# Type checks are really easy.
# Just supply the type as a set hook.
public name => my %_name, {
set_hook => Str,
};
# Define a type that silently coerces negative values
# to positive. It's silly, but it works as an example!
my $Years = PositiveInt−>plus_coercions(Int, q{ abs($_) });
# Coercions are more annoying, but possible.
public age => my %_age, {
set_hook => sub { $_ = $Years−>assert_coerce($_) },
};
# Parameter checking for methods is as expected.
sub get_older {
state $check = compile( $Years );
my $self = shift;
my ($years) = $check−>(@_);
$self−>_set_age($self−>age + $years);
}
}
Params::Check and Object::AccessorThe Params::Check allow() function, the allow option for the Params::Check check() function, and the input validation mechanism for Object::Accessor all work in the same way, which is basically a limited pure-Perl implementation of the smart match operator. While this doesn't directly support Type::Tiny constraints, it does support coderefs. You can use Type::Tiny's compiled_check method to obtain a suitable coderef. Param::Check example: my $tmpl = {
name => { allow => Str−>compiled_check },
age => { allow => Int−>compiled_check },
};
check($tmpl, { name => "Bob", age => 32 })
or die Params::Check::last_error();
Object::Accessor example: my $obj = Object::Accessor−>new;
$obj−>mk_accessors(
{ name => Str−>compiled_check },
{ age => Int−>compiled_check },
);
Caveat: Object::Accessor doesn't die when a value fails to meet its type constraint; instead it outputs a warning to STDERR. This behaviour can be changed by setting $Object::Accessor::FATAL = 1.
Class::StructThis is proof-of-concept of how Type::Tiny can be used to constrain attributes for Class::Struct. It's probably not a good idea to use this in production as it slows down UNIVERSAL::isa globally. use Types::Standard −types;
use Class::Struct;
{
my %MAP;
my $orig_isa = \&UNIVERSAL::isa;
*UNIVERSAL::isa = sub {
return $MAP{$1}−>check($_[0])
if $_[1] =~ /^CLASSSTRUCT::TYPETINY::(.+)$/ && exists $MAP{$1};
goto $orig;
};
my $orig_dn = \&Type::Tiny::display_name;
*Type::Tiny::display_name = sub {
if (caller(1) eq 'Class::Struct') {
$MAP{$_[0]{uniq}} = $_[0];
return "CLASSSTRUCT::TYPETINY::".$_[0]{uniq};
}
goto $orig_dn;
};
}
struct Person => [ name => Str, age => Int ];
my $bob = Person−>new(
name => "Bob",
age => 21,
);
$bob−>name("Robert"); # okay
$bob−>name([]); # dies
NEXT STEPSHere's your next step: • Type::Tiny::Manual::UsingWithTestMore Type::Tiny for test suites.
AUTHORToby Inkster <tobyink@cpan.org>.
COPYRIGHT AND LICENCEThis software is copyright (c) 2013−2014, 2017−2021 by Toby Inkster. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
DISCLAIMER OF WARRANTIESTHIS PACKAGE IS PROVIDED ‟AS IS” AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 0
Johanes Gumabo
Data Size : 14,387 byte
man-Type::Tiny::Manual::UsingWithOther.3pmBuild : 2024-12-05, 20:55 :
Visitor Screen : x
Visitor Counter ( page / site ) : 3 / 180,305
Visitor ID : :
Visitor IP : 3.15.144.162 :
Visitor Provider : AMAZON-02 :
Provider Position ( lat x lon ) : 39.962500 x -83.006100 : x
Provider Accuracy Radius ( km ) : 1000 :
Provider City : Columbus :
Provider Province : Ohio , : ,
Provider Country : United States :
Provider Continent : North America :
Visitor Recorder : Version :
Visitor Recorder : Library :
Online Linux Manual Page : Version : Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.05
Online Linux Manual Page : Library : lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page : Library : lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base : Version : Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base : Library : lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36
Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.
ERROR : Need New Coding : (parse_manual_page_|249|Type::Tiny::Manual::UsingWithOther.3pm|36/37|el══─{─══.|.el══─{─══. ds -- \|\(em\|
) (htmlprn|149|Type::Tiny::Manual::UsingWithOther.3pm|36/37|.el══─{─══. ds -- — |.el══─{─══. ds -- \|\(em\|
) (parse_manual_page_|249|Type::Tiny::Manual::UsingWithOther.3pm|43|br══─}─══|'br══─}─══
) (htmlprn|149|Type::Tiny::Manual::UsingWithOther.3pm|43|'br══─}─══ |'br══─}─══
)