File: metaclass.pm

package info (click to toggle)
libclass-mop-perl 0.36-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 552 kB
  • ctags: 209
  • sloc: perl: 6,157; makefile: 46
file content (98 lines) | stat: -rw-r--r-- 2,306 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

package metaclass;

use strict;
use warnings;

use Carp         'confess';
use Scalar::Util 'blessed';

our $VERSION   = '0.03';
our $AUTHORITY = 'cpan:STEVAN';

use Class::MOP;

sub import {
    shift;
    my $metaclass;
    if (!defined($_[0]) || $_[0] =~ /^\:(attribute|method|instance)_metaclass/) {
        $metaclass = 'Class::MOP::Class';
    }
    else {
        $metaclass = shift;
        ($metaclass->isa('Class::MOP::Class'))
            || confess "The metaclass ($metaclass) must be derived from Class::MOP::Class";
    }
    my %options = @_;
    my $package = caller();
    
    # create a meta object so we can install &meta
    my $meta = $metaclass->initialize($package => %options);
    $meta->add_method('meta' => sub {
        # we must re-initialize so that it 
        # works as expected in subclasses, 
        # since metaclass instances are 
        # singletons, this is not really a 
        # big deal anyway.
        $metaclass->initialize((blessed($_[0]) || $_[0]) => %options)
    });
}

1;

__END__

=pod

=head1 NAME

metaclass - a pragma for installing and using Class::MOP metaclasses

=head1 SYNOPSIS

  package MyClass;

  # use Class::MOP::Class
  use metaclass; 

  # ... or use a custom metaclass
  use metaclass 'MyMetaClass';
  
  # ... or use a custom metaclass  
  # and custom attribute and method
  # metaclasses
  use metaclass 'MyMetaClass' => (
      ':attribute_metaclass' => 'MyAttributeMetaClass',
      ':method_metaclass'    => 'MyMethodMetaClass',    
  );

  # ... or just specify custom attribute
  # and method classes, and Class::MOP::Class
  # is the assumed metaclass
  use metaclass (
      ':attribute_metaclass' => 'MyAttributeMetaClass',
      ':method_metaclass'    => 'MyMethodMetaClass',    
  );

=head1 DESCRIPTION

This is a pragma to make it easier to use a specific metaclass 
and a set of custom attribute and method metaclasses. It also 
installs a C<meta> method to your class as well. 

=head1 AUTHORS

Stevan Little E<lt>stevan@iinteractive.comE<gt>

Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2006 by Infinity Interactive, Inc.

L<http://www.iinteractive.com>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut