NAME
MOP4Import::Types - fields-aware type builder for inner-type
SYNOPSIS
Create inner-types MyApp::Artist
and MyApp::CD
using MOP4Import::Types.
# 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
}
}
DESCRIPTION
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.
"MetaObject Protocol for Import" in this module
"import()" method of this module takes name => [@pragma_list]
style paired arguments and dispatch them as $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/]];
Type names can be imported
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/;
Extending types in derived class
use MOP4Import::Types::Extend instead.
Specifying base type
use MOP4Import::Types
can recognize first HASH argument as option set and you can specify base type via basepkg
option.
use MOP4Import::Types +{basepkg => 'YourBaseObject'}
Foo => [[fields => qw/.../]], ...;
SEE ALSO
AUTHOR
Kobayashi, Hiroaki <hkoba@cpan.org>
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.