Class::Meta 0.53 review

Download
by rbytes.net on

Class::Meta is a Perl class automation, introspection, and data validation. SYNOPSIS Generate a class: package MyApp::Thingy

License: Perl Artistic License
File size: 59K
Developer: David Wheeler
0 stars award from rbytes.net

Class::Meta is a Perl class automation, introspection, and data validation.

SYNOPSIS

Generate a class:
package MyApp::Thingy;
use strict;
use Class::Meta;
use Class::Meta::Types::String;
use Class::Meta::Types::Numeric;

BEGIN {

# Create a Class::Meta object for this class.
my $cm = Class::Meta->new( key => 'thingy' );

# Add a constructor.
$cm->add_constructor(
name => 'new',
create => 1,
);

# Add a couple of attributes with generated methods.
$cm->add_attribute(
name => 'uuid',
authz => Class::Meta::READ,
type => 'string',
required => 1,
default => sub { Data::UUID->new->create_str },
);
$cm->add_attribute(
name => 'name',
is => 'string',
required => 1,
default => undef,
);
$cm->add_attribute(
name => 'age',
is => 'integer',
default => undef,
);

# Add a custom method.
$cm->add_method(
name => 'chk_pass',
view => Class::Meta::PUBLIC,
);
$cm->build;
}
Then use the class:
use MyApp::Thingy;

my $thingy = MyApp::Thingy->new;
print "ID: ", $thingy->id, $/;
$thingy->name('Larry');
print "Name: ", $thingy->name, $/;
$thingy->age(42);
print "Age: ", $thingy->age, $/;
Or make use of the introspection API:
use MyApp::Thingy;

my $class = MyApp::Thingy->my_class;
my $thingy;

print "Examining object of class ", $class->package, $/;

print "nConstructors:n";
for my $ctor ($class->constructors) {
print " o ", $ctor->name, $/;
$thingy = $ctor->call($class->package);
}

print "nAttributes:n";
for my $attr ($class->attributes) {
print " o ", $attr->name, " => ", $attr->get($thingy), $/;
if ($attr->authz >= Class::Meta::SET && $attr->type eq 'string') {
$attr->get($thingy, 'hey there!');
print " Changed to: ", $attr->get($thingy), $/;
}
}

print "nMethods:n";
for my $meth ($class->methods) {
print " o ", $meth->name, $/;
$meth->call($thingy);
}

Class::Meta provides an interface for automating the creation of Perl classes with attribute data type validation. It differs from other such modules in that it includes an introspection API that can be used as a unified interface for all Class::Meta-generated classes. In this sense, it is an implementation of the "Facade" design pattern.

Requirements:
Perl

Class::Meta 0.53 keywords