# creating classes with MOP ...
sub create {
- my $class = shift;
- my $package_name = shift;
+ my ( $class, @args ) = @_;
- (defined $package_name && $package_name)
- || confess "You must pass a package name";
+ unshift @args, 'name' if @args % 2 == 1;
- (scalar @_ % 2 == 0)
- || confess "You much pass all parameters as name => value pairs " .
- "(I found an uneven number of params in \@_)";
+ my (%options) = @args;
+ my $package_name = $options{name};
- my (%options) = @_;
+ (defined $package_name && $package_name)
+ || confess "You must pass a package name";
(ref $options{superclasses} eq 'ARRAY')
|| confess "You must pass an ARRAY ref of superclasses"
my $meta = $class->initialize($package_name);
+ # FIXME totally lame
$meta->add_method('meta' => sub {
$class->initialize(blessed($_[0]) || $_[0]);
});
# ... test our class creator
my $Bar = Class::MOP::Class->create(
- 'Bar' => (
- superclasses => [ 'Foo' ],
- methods => {
- foo => sub { 'Bar::foo' },
- bar => sub { 'Bar::bar' },
- }
- ));
+ name => 'Bar',
+ superclasses => [ 'Foo' ],
+ methods => {
+ foo => sub { 'Bar::foo' },
+ bar => sub { 'Bar::bar' },
+ }
+);
isa_ok($Bar, 'Class::MOP::Class');
ok($Bar->has_method('foo'), '... Bar->has_method(foo)');