MOP4Import::Types - fields-aware type builder for inner-type
Create inner-types MyApp::Artist and MyApp::CD using MOP4Import::Types.
MyApp::Artist
MyApp::CD
# Define subtype Artist and CD with their fields. package MyApp; use MOP4Import::Types (Artist => [[fields => qw/artistid name/]] , CD => [[fields => qw/cdid artistid title year/]]);
Above is an equivalent of following:
package MyApp; sub Artist () {'MyApp::Artist'} package MyApp::Artist { use MOP4Import::Declare [fields => qw/artistid name/]; } sub CD () {'MyApp::CD'} package MyApp::CD { use MOP4Import::Declare [fields => qw/cdid artistid title year/]; }
You can use above types like following with compile-time field name typos detection of fields.
sub print_artist_cds { (my $self, my Artist $artist) = @_; # $artist is typed. my @cds = $self->DB->select( CD => { artistid => $artist->{artistid} # Checked statically } ); foreach my CD $cd (@cds) { # $cd is typed. print tsv($cd->{title}, $cd->{year}), "\n"; # Checked statically } }
MOP4Import::Types is yet another protocol implementation of MOP4Import family, based on MOP4Import::Pairs and MOP4Import::Declare::Type.
In contrast to MOP4Import::Declare, which is designed to modify target module itself, this module is designed to add new inner-types to target module.
With "inner-type", I mean type declared in some module and not directly exposed as "require" able module.
"import()" method of this module takes name => [@pragma_list] style paired arguments and dispatch them as $myPack->declare_type($opts, $name, @pragma_list).
name => [@pragma_list]
$myPack->declare_type($opts, $name, @pragma_list)
use MOP4Import::Types (Foo => [[fields => qw/bar baz/]] , Cat => [[fields => qw/name birth_year/]] ); # Above is equivalent of followings use MOP4Import::Declare::Type [type => Foo => [fields => qw/bar baz/]]; use MOP4Import::Declare::Type [type => Cat => [fields => qw/name birth_year/]];
package MyProject::Types; use MOP4Import::Types (User => [[fields => qw/uid name .../]] , Product => [[fields => qw/prodid name .../]] , ... ); #------------ # You can import above types in other module like following: #------------ package MyProject::Web; use MyProject::Types qw/User Product/;
use MOP4Import::Types::Extend instead.
use MOP4Import::Types can recognize first HASH argument as option set and you can specify base type via basepkg option.
use MOP4Import::Types
basepkg
use MOP4Import::Types +{basepkg => 'YourBaseObject'} Foo => [[fields => qw/.../]], ...;
MOP4Import::Declare
Kobayashi, Hiroaki <hkoba@cpan.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install MOP4Import::Declare, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MOP4Import::Declare
CPAN shell
perl -MCPAN -e shell install MOP4Import::Declare
For more information on module installation, please visit the detailed CPAN module installation guide.