Class::MOP - adding in some basic stuff
[gitmo/Class-MOP.git] / lib / Class / MOP / Class.pm
CommitLineData
8b978dd5 1
2package Class::MOP::Class;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9use Sub::Name 'subname';
10use B 'svref_2object';
11
12our $VERSION = '0.01';
13
14# Creation
15
16sub initialize {
17 my ($class, $package_name) = @_;
18 (defined $package_name)
19 || confess "You must pass a package name";
20 bless \$package_name => $class;
21}
22
23sub create {
24 my ($class, $package_name, $package_version, %options) = @_;
25 (defined $package_name)
26 || confess "You must pass a package name";
27 my $code = "package $package_name;";
28 $code .= "\$$package_name\:\:VERSION = '$package_version';"
29 if defined $package_version;
30 eval $code;
31 confess "creation of $package_name failed : $@" if $@;
32 my $meta = $package_name->meta;
33 $meta->superclasses(@{$options{superclasses}})
34 if exists $options{superclasses};
35 # ... rest to come later ...
36 return $meta;
37}
38
39# Informational
40
41sub name { ${$_[0]} }
42
43sub version {
44 my $self = shift;
45 no strict 'refs';
46 ${$self->name . '::VERSION'};
47}
48
49# Inheritance
50
51sub superclasses {
52 my $self = shift;
53 no strict 'refs';
54 if (@_) {
55 my @supers = @_;
56 @{$self->name . '::ISA'} = @supers;
57 }
58 @{$self->name . '::ISA'};
59}
60
61sub class_precedence_list {
62 my $self = shift;
63 (
64 $self->name,
65 map {
66 $_->meta->class_precedence_list()
67 } $self->superclasses()
68 );
69}
70
71## Private Utility Methods
72
73# borrowed from Class::Trait 0.20 - Thanks Ovid :)
74sub _find_subroutine_package {
75 my $sub = shift;
76 my $package = '';
77 eval {
78 my $stash = svref_2object($sub)->STASH;
79 $package = $stash->NAME
80 if $stash && $stash->can('NAME');
81 };
82 confess "Could not determine calling package: $@"
83 if $@;
84 return $package;
85}
86
871;
88
89__END__
90
91=pod
92
93=head1 NAME
94
95Class::MOP::Class - Class Meta Object
96
97=head1 SYNOPSIS
98
99=head1 DESCRIPTION
100
101=head1 AUTHOR
102
103Stevan Little E<gt>stevan@iinteractive.comE<lt>
104
105=head1 COPYRIGHT AND LICENSE
106
107Copyright 2006 by Infinity Interactive, Inc.
108
109L<http://www.iinteractive.com>
110
111This library is free software; you can redistribute it and/or modify
112it under the same terms as Perl itself.
113
114=cut