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;
(
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 {
}
sub
CD () {
'MyApp::CD'
}
package
MyApp::CD {
}
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.
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)
.
(
Foo
=> [[
fields
=>
qw/bar baz/
]]
,
Cat
=> [[
fields
=>
qw/name birth_year/
]]
);
# Above is equivalent of followings
Type names can be imported
package
MyProject::Types;
(
User
=> [[
fields
=>
qw/uid name .../
]]
,
Product
=> [[
fields
=>
qw/prodid name .../
]]
, ...
);
#------------
# You can import above types in other module like following:
#------------
package
MyProject::Web;
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.
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.