Class::Meta::Express 0.04 review
DownloadClass::Meta::Express is a Perl module for concise, expressive creation of Class::Meta classes. Synopsis package My::Contact;
|
|
Class::Meta::Express is a Perl module for concise, expressive creation of Class::Meta classes.
Synopsis
package My::Contact;
use Class::Meta::Express;
class {
meta contact => ( default_type => 'string' );
has 'name';
has contact => ( required => 1 );
}
This module provides an interface to concisely yet expressively create classes with Class::Meta. Although I am of course fond of Class::Meta, I've never been overly thrilled with its interface for creating classes:
package My::Thingy;
use Class::Meta;
BEGIN {
# Create a Class::Meta object for this class.
my $cm = Class::Meta->new( key => 'thingy' );
# Add a constructor.
$cm->add_constructor( name => 'new' );
# Add a couple of attributes with generated accessors.
$cm->add_attribute(
name => 'id',
is => 'integer',
required => 1,
);
$cm->add_attribute(
name => 'name',
is => 'string',
required => 1,
);
$cm->add_attribute(
name => 'age',
is => 'integer',
);
# Add a custom method.
$cm->add_method(
name => 'chk_pass',
code => sub { return 'code' },
);
$cm->build;
}
This example is relatively simple; it can get a lot more verbose. But even still, all of the method calls were annoying. I mean, whoever thought of using an object oriented interface for declaring a class? (Oh yeah: I did.) I wasn't alone in wanting a more declarative interface; Curtis Poe, with my blessing, created Class::Meta::Declare, which would use this syntax to create the same class:
package My::Thingy;
use Class::Meta::Declare ':all';
Class::Meta::Declare->new(
# Create a Class::Meta object for this class.
meta => [
key => 'thingy',
],
# Add a constructor.
constructors => [
new => { }
],
# Add a couple of attributes with generated accessors.
attributes => [
id => {
type => $TYPE_INTEGER,
required => 1,
},
name => {
required => 1,
type => $TYPE_STRING,
},
age => { type => $TYPE_INTEGER, },
],
# Add a custom method.
methods => [
chk_pass => {
code => sub { return 'code' },
}
]
);
This approach has the advantage of being a bit more concise, and it is declarative, but I find all of the indentation levels annoying; it's hard for me to figure out where I am, especially if I have to define a lot of attributes. And finally, everything is a string with this syntax, except for those ugly read-only scalars such as $TYPE_INTEGER. So I can't easily tell where one attribute ends and the next one starts. Bleh.
Requirements:
Perl
Class::Meta::Express 0.04 keywords