File Coverage

blib/lib/Class/Object.pm
Criterion Covered Total %
statement 33 33 100.0
branch 4 4 100.0
condition 2 3 66.6
subroutine 6 6 100.0
pod 2 2 100.0
total 47 48 97.9


line stmt bran cond sub pod time code
1             package Class::Object;
2              
3 1     1   6944 use strict;
  1         3  
  1         40  
4 1     1   12 no strict 'refs'; # we use symbolic refs all over
  1         2  
  1         57  
5 1     1   6 use vars qw($VERSION);
  1         11  
  1         237  
6             $VERSION = '0.01';
7              
8             =head1 NAME
9              
10             Class::Object - each object is its own class
11              
12             =head1 SYNOPSIS
13              
14             use Class::Object;
15              
16             # Generate an object, give it a method called 'foo'
17             my $obj = Class::Object->new;
18             $obj->sub('foo', sub { return "FOO, I SAY!\n" });
19              
20             # Generate another object, give it a different method called 'foo'.
21             my $another_obj = Class::Object->new;
22             $another_obj->sub('foo', sub { return "UNFOO!\n" });
23              
24             # Get copies of those methods back out, just like any other.
25             my $obj_foo = $obj->can('foo');
26             my $another_foo = $another_obj->can('foo');
27              
28             # Same names, same classes, different methods!
29             print $obj->foo; # "FOO, I SAY!"
30             print &$obj_foo; # "FOO, I SAY!"
31             print $another_obj->foo; # "UNFOO!"
32             print &$another_foo; # "UNFOO!"
33              
34             print "Yep\n" if $obj->isa('Class::Object'); # Yep
35             print "Yep\n" if $another_obj->isa('Class::Object'); # Yep
36              
37              
38             # $obj->new clones itself, so $same_obj->foo comes out as $obj->foo
39             my $same_obj = $obj->new;
40             print $same_obj->foo; # "FOO, I SAY!"
41              
42             =head1 DESCRIPTION
43              
44             Traditionally in OO, objects belong to a class and that class as
45             methods. $poodle is an object of class Dog and Dog might have methods
46             like bark(), fetch() and nose_crotch(). What if instead of the
47             methods belonging to the Dog class, they belonged to the $poodle
48             object itself?
49              
50             That's what Class::Object does.
51              
52              
53             =head2 Methods
54              
55             For the most part, these objects work just like any other. Things
56             like can() and isa() work as expected.
57              
58             =over 4
59              
60             =item B
61              
62             my $obj = Class::Object->new;
63              
64             Generates a new object which is its own class.
65              
66             my $clone_obj = $obj->new;
67              
68             Generates a new object which is in the same class as $obj. They share
69             their methods.
70              
71             =cut
72              
73             my $counter = 0;
74              
75             sub new {
76 3     3 1 156 my($proto) = shift;
77 3   66     12 my($class) = ref $proto || $proto;
78              
79 3         4 my $obj_class;
80 3 100       6 if( ref $proto ) {
81 1         2 $obj_class = ref $proto;
82 1         2 ${$obj_class.'::_count'}++;
  1         4  
83             }
84             else {
85 2         6 $obj_class = $class.'::'.$counter++;
86 2         2 @{$obj_class.'::ISA'} = $class;
  2         35  
87 2         3 ${$obj_class.'::_count'} = 1;
  2         7  
88             }
89 3         10 bless {}, $obj_class;
90             }
91              
92             =item B
93              
94             $obj->sub($meth_name, sub { ...code... });
95              
96             This is how you declare a new method for an object, almost exactly
97             like how you do it normally.
98              
99             Normally you'd do this:
100              
101             package Foo;
102             sub wibble {
103             my($self) = shift;
104             return $self->{wibble};
105             }
106              
107             In Class::Object, you do this:
108              
109             my $foo = Class::Object->new;
110             $foo->sub('wibble', sub {
111             my($self) = shift;
112             return $self->{wibble};
113             });
114              
115             Only $foo (and its clones) have access to wibble().
116              
117             =cut
118              
119             sub sub {
120 2     2 1 125 my($self, $name, $meth) = @_;
121 2         3 *{ref($self).'::'.$name} = $meth;
  2         9  
122             }
123              
124             # When the last object in a class is destroyed, we completely
125             # annihilate that class, its methods and variables. Keeps things
126             # from leaking.
127             sub DESTROY {
128 3     3   46 my($self) = shift;
129 3         4 my $obj_class = ref $self;
130 3         4 ${$obj_class.'::_count'}--;
  3         8  
131 3 100       14 unless( ${$obj_class.'::_count'} ) {
  3         17  
132 2         3 undef %{$obj_class.'::'};
  2         126  
133             }
134             }
135              
136             =back
137              
138             =head1 BUGS and CAVEATS
139              
140             This is just a proof-of-concept module. The docs stink, there's no
141             real inheritance model... totally incomplete. Drop me a line if you'd
142             like to see it completed.
143              
144             B rebless a Class::Object object. Bad Things will happen.
145              
146             =head1 AUTHOR
147              
148             Michael G Schwern
149              
150              
151             =head1 SEE ALSO
152              
153             L is another way to do the same thing (and much more
154             complete).
155              
156             =cut
157              
158              
159             1;