Type::Tiny::Manual::UsingWithClassTiny - Online Linux Manual PageSection : 3pm
Updated : 2021-02-24
Source : perl v5.32.1
Note : User Contributed Perl Documentation

NAMEType::Tiny::Manual::UsingWithClassTiny − use of Type::Tiny with Class::Tiny

MANUALClass::Tiny is an even-smaller-than-Moo class builder. Let's translate the classic Horse class from Moo to Class::Tiny. Moo: ​ package Horse { ​ use Moo; ​ use Types::Standard qw( Str Num ArrayRef ); ​ use namespace::autoclean; ​ ​ has name => ( is => 'ro', isa => Str, required => 1 ); ​ has gender => ( is => 'ro', isa => Str ); ​ has age => ( is => 'rw', isa => Num ); ​ has children => ( ​ is => 'ro', ​ isa => ArrayRef, ​ default => sub { return [] }, ​ ); ​ } Class::Tiny: ​ package Horse { ​ use Class::Tiny qw( gender age ), { ​ name => sub { die "name is required"; }, ​ children => sub { return [] }, ​ }; ​ use Types::Standard qw( Str Num ArrayRef Dict Optional slurpy Any); ​ use Type::Params qw( wrap_methods compile ); ​ use namespace::autoclean; ​ ​ # type checks ​ wrap_methods( ​ BUILD => [Dict[ ​ name => Str, ​ gender => Optional[Str], ​ age => Optional[Num], ​ children => Optional[ArrayRef], ​ slurpy Any, ​ ]], ​ name => [], ​ gender => [], ​ age => Optional[Num], ​ children => [], ​ ); ​ } What's going on here? Well, Class::Tiny, after it has built a new object, will do this: ​ $self−>BUILD($args); (Technically, it calls BUILD not just for the current class, but for all parent classes too.) We can hook onto this in order to check type constraints for the constructor. We use wrap_methods from Type::Params to wrap the original BUILD method (which doesn't exist, so wrap_methods will just assume an empty sub) with a type check for $args. The type check is just a Dict that checks the class's required and optional attributes and includes ​slurpy Any at the end to be flexible for subclasses adding new attributes. Then we wrap the name, gender, and children methods with checks to make sure they're only being called as getters, and we wrap age, allowing it to be called as a setter with a Num. There are also a couple of CPAN modules that can help you out.

Class::Tiny::ConstrainedAccessorClass::Tiny::ConstrainedAccessor creates a BUILD and accessors that enforce Type::Tiny constraints. Attribute types are passed to Class::Tiny::ConstrainedAccessor; attribute defaults are passed to Class::Tiny. ​ package Horse { ​ use Types::Standard qw( Str Num ArrayRef ); ​ use Class::Tiny::ConstrainedAccessor { ​ name => Str, ​ gender => Str, ​ age => Num, ​ children => ArrayRef, ​ }; ​ use Class::Tiny qw( gender age ), { ​ name => sub { die "name is required"; }, ​ children => sub { return [] }, ​ }; ​ }

Class::Tiny::AntlersClass::Tiny::Antlers provides Moose-like syntax for Class::Tiny, including support for isa. You do not also need to use Class::Tiny itself. ​ package Horse { ​ use Class::Tiny::Antlers qw(has); ​ use Types::Standard qw( Str Num ArrayRef ); ​ use namespace::autoclean; ​ ​ has name => ( ​ is => 'ro', ​ isa => Str, ​ default => sub { die "name is required" }, ​ ); ​ has gender => ( is => 'ro', isa => Str ); ​ has age => ( is => 'rw', isa => Num ); ​ has children => ( ​ is => 'ro', ​ isa => ArrayRef, ​ default => sub { return [] }, ​ ); ​ }

NEXT STEPSHere's your next step: • Type::Tiny::Manual::UsingWithOther Using Type::Tiny with Class::InsideOut, Params::Check, and Object::Accessor.

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   :   15,102 byte
man-Type::Tiny::Manual::UsingWithClassTiny.3pmBuild   :   2024-12-05, 20:55   :  
Visitor Screen   :   x
Visitor Counter ( page / site )   :   2 / 180,971
Visitor ID   :     :  
Visitor IP   :   3.129.194.30   :  
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::UsingWithClassTiny.3pm|36/37|el══─{─══.|.el══─{─══. ds -- \|\(em\| )         (htmlprn|149|Type::Tiny::Manual::UsingWithClassTiny.3pm|36/37|.el══─{─══. ds --  —  |.el══─{─══. ds -- \|\(em\| )         (parse_manual_page_|249|Type::Tiny::Manual::UsingWithClassTiny.3pm|43|br══─}─══|'br══─}─══ )         (htmlprn|149|Type::Tiny::Manual::UsingWithClassTiny.3pm|43|'br══─}─══ |'br══─}─══ )