Class/LOP.pm

filter: Statementstmt 47.2% 128 / 271 Branchbran 23.4% 15 / 64 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 62.7% 32 / 51 Podpod 77.2% 17 / 22 totaltotal 45.3% 193 / 426 CC 41 SCAR 27.7
CC
41
Cov
41%
add_hook
99.2
__ANON__
20.0
extend_class
14.3
CRAP 16.0
« Sub/Mage.pm ↑ Summary
1 package Class::LOP;
2  
3 =head1 NAME
4  
5 Class::LOP - The Lightweight Object Protocol
6  
7 =head1 DESCRIPTION
8  
9 Just like L<Moose> is built from L<Class::MOP>. You can build your own using this module. It is a little different
10 from L<Class::MOP> though, because it doesn't use a meta class, it has less features, but it's a lot faster.
11 If you need a lightweight object protocol, this may be the tool for you.
12 Using this module you could build an extremely quick OOP framework that could be used from a CLI or as a standard
13 module.
14  
15 =head1 SYNOPSIS
16  
17 package Goosey;
18  
19 use Class::LOP;
20  
21 sub import {
22  
23 my $caller = caller();
24 # Methods can be chained for simplicity and easy tracking.
25 # Below, we'll create the 'new' constructor, enable warnings and strict, and also
26 # bestow the accessors feature, so our module can create them
27 Class::LOP->init($caller)
28 ->create_constructor
29 ->warnings_strict
30 ->have_accessors('has');
31  
32 # import multiple methods into the specified class
33 Class::LOP->init('Goosey')->import_methods($caller, qw/
34 extends
35 after
36 before
37 /);
38 }
39  
40 # Add a few hook modifiers
41 # This code sure looks a lot cleaner than writing it yourself ;-)
42 sub after {
43 my ($name, $code) = @_;
44  
45 Class::LOP->init(caller())->add_hook(
46 type => 'after',
47 name => $name,
48 method => $code,
49 );
50 }
51  
52 # Extending a class is similar to 'use base'
53 # You may have also seen this from Moose
54 # ->extend_class() makes it really easy for you
55 sub extends {
56 my (@classes) = @_;
57 Class::LOP->init(caller())
58 ->extend_class(@classes);
59 }
60  
61 # MyClass.pm
62 package MyClass;
63  
64 use Goosey; # enables warnings/strict
65 extends 'Some::Module::To::Subclass';
66  
67 has 'name' => ( is => 'rw', default => 'Foo' );
68  
69 after 'name' => sub {
70 print "This code block runs after the original!\n";
71 };
72  
73 Wow, that all looks familiar.. but we wrote it all in a fairly small amount of code. Class::LOP takes care of the
74 dirty work for you, so you can just worry about getting the features in your module that you want.
75  
76 =cut
77  
78 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 4 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 4 our $VERSION = '0.003';
83  
84 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=21.1
Pod documented
85 0 my ($self, $class) = @_;
86 0 ▶ if (!$class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 0 0
87 0 warn "No class specified";
88 0 return 0;
89 }
90  
91 {
92 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
93 0 ▶ if (! scalar %{ "${class}::" }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar %{"${class}::";}) 0 0 0
94 0 ▶ *{"${class}::new"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 return bless {}, $class;
96 0 };
97 }
98 }
99  
100 0 return bless {
101 _name => $class,
102 _attributes => [],
103 },
104 __PACKAGE__;
105 }
106  
107 7 ▶sub init {
Subroutine init: called CC=2 SCAR=8.4
Pod documented
108 7 my ($self, $class) = @_;
109 7 ▶ if (!$class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 7 7
110 0 warn "No class specified";
111 0 return 0;
112 }
113  
114 7 return bless {
115 _name => $class,
116 },
117 __PACKAGE__;
118 }
119  
120 5 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
121 5 my $self = shift;
122 5 return $self->{_name};
123 }
124  
125 4 ▶sub warnings_strict {
Subroutine warnings_strict: called CC=1 SCAR=0.0
Pod documented
126 4 my $self = shift;
127 4 warnings->import();
128 4 strict->import();
129 4 return $self;
130 }
131  
132 0 ▶sub getscope {
Subroutine getscope: not called CC=1 SCAR=6.9
Pod documented
133 0 my ($self) = @_;
134 0 return scalar caller(1);
135 }
136  
137 1 ▶sub class_exists {
Subroutine class_exists: called CC=2 SCAR=7.0
Pod documented
138 1 my ($self, $class) = @_;
139 1 ▶ $class = $self->{_name} if !$class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 1 1
140 {
141 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
142 1 return scalar %{ "${class}::" };
143 }
144 }
145  
146 0 ▶sub list_methods {
Subroutine list_methods: not called CC=3 SCAR=19.6
Pod documented
147 0 my $self = shift;
148 0 my $class = $self->{_name};
149 0 my @methods = ();
150 {
151 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
152 0 foreach my $method (keys %{"${class}::"}) {
153 0 ▶ push @methods, $method
Branch: 1 branch
BranchTrueFalseTotal
if substr($method, -2, 2) ne "::" 0 0 0
154 if substr($method, -2, 2) ne '::';
155 }
156 }
157  
158 0 return @methods;
159 }
160  
161 1 ▶sub method_exists {
Subroutine method_exists: called CC=2 SCAR=8.4
Pod documented
162 1 my ($self, $class, $method) = @_;
163 1 ▶ if (!$method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 0 1 1
164 0 $method = $class;
165 0 $class = $self->{_name};
166 }
167 1 return $class->can($method);
168 }
169  
170 0 ▶sub subclasses {
Subroutine subclasses: not called CC=2 SCAR=17.9
Pod documented
171 0 my $self = shift;
172 0 my @list = ();
173 0 my $class = $self->{_name};
174 0 @list = @{ $class->mro::get_isarev() };
175  
176 0 ▶ return scalar(@list) > 0 ? @list : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @list > 0 ? : 0 0 0
177 }
178  
179 0 ▶sub superclasses {
Subroutine superclasses: not called CC=1 SCAR=2.2
Pod documented
180 0 my $self = shift;
181 0 my $class = $self->{_name};
182 {
183 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
184 0 return @{ "${class}::ISA" };
185 }
186 }
187  
188 5 ▶sub import_methods {
Subroutine import_methods: called CC=4 SCAR=14.2
Pod documented
189 5 my ($self, $class, @methods) = @_;
190 5 my $caller = $self->name();
191 localscope: {
192 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
193 5 ▶ if (! scalar(%{ "${class}::" })) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar %{"${class}::";}) { } 0 5 5
194 0 warn "Class ${class} does not exist";
195 0 return 0;
196 }
197 else {
198 5 for my $method (@methods) {
199 67 ▶ *{"${class}::${method}"} = *{"${caller}::${method}"}
Branch: 1 branch
BranchTrueFalseTotal
if $caller->can($method) 67 0 67
200 if $caller->can($method);
201 }
202 }
203 }
204  
205 5 return $self;
206 }
207  
208 0 ▶sub extend_class {
Subroutine extend_class: not called CC=4 SCAR=26.6
Pod documented
209 0 my ($self, @mothers) = @_;
210  
211 0 my $class = $self->{_name};
212 0 foreach my $mother (@mothers) {
213 # if class is unknown to us, import it (FIXME)
214 0 ▶ unless (grep { $_ eq $mother } @{$self->{'classes'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $mother;} @{$$self{"classes"};}) 0 0 0
215 0 eval "use $mother";
216 0 ▶ warn "Could not extend $mother: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
217 if $@;
218
219 0 $mother->import;
220 }
221 0 push @{$self->{'classes'}}, $class;
222 }
223  
224 {
225 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
226 0 @{"${class}::ISA"} = @mothers;
227 }
228  
229 0 return $self;
230 }
231  
232 1 ▶sub have_accessors {
Subroutine have_accessors: called CC=2 SCAR=8.5
Pod documented
233 1 my ($self, $name) = @_;
234 1 my $class = $self->{_name};
235 1 ▶ if ($self->class_exists($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class_exists($class)) { } 1 0 1
236 {
237 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
238 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
239 1 ▶ *{"${class}::${name}"} = sub {
Subroutine __ANON__: called CC=4 SCAR=18.7
240 1 my ($acc, %args) = @_;
241 1 my $default = delete $args{default};
242 1 my $type = delete $args{is};
243 1 ▶ if ($type && $type eq 'ro') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $type eq 'ro') { } 0 1 1
Condition33%1/3
$type and $type eq "ro"
!ll&&!rl&&r
010
MC/DC0%0/2
$type and $type eq "ro"
$type $type eq "ro"
Truth table33%1/3
A and B
A= $typeB= $type eq "ro"
ABresult
0X0
100
111
244 0 ▶ *{"${class}::${acc}"} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
245 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
246 0 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 0 0 0
247 0 ▶ if (! exists $_[0]->{"$acc\_$_[0]\_default_used"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[0]{"${acc}_$_[0]_default_used"}) 0 0 0
248 0 $self->_add_attribute($_[0], $acc, $_[1]);
249 0 $_[0]->{$acc} = $_[1];
250 0 $_[0]->{"$acc\_$_[0]\_default_used"} = 1;
251 0 return $_[1];
252 }
253 }
254  
255 0 warn "Can't modify a read-only accessor (${acc})";
256 0 return 0;
257 }
258  
259 0 return $_[0]->{$acc};
260 0 };
261 }
262 else {
263 1 ▶ *{"${class}::${acc}"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
264 4 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 2 2 4
265 2 $self->_add_attribute($_[0], $acc, $_[1]);
266 2 $_[0]->{$acc} = $_[1];
267 }
268  
269 4 return $_[0]->{$acc};
270 1 };
271 }
272  
273 1 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 1 0 1
274 1 my $fullpkg = "${class}::${acc}";
275 1 $class->$acc($default);
276 }
277 1 };
278 }
279  
280 1 return $self;
281 }
282 else {
283 0 warn "Can't create accessors in class '$class', because it doesn't exist";
284 0 return 0;
285 }
286 }
287  
288 1 ▶sub create_constructor {
Subroutine create_constructor: called CC=2 SCAR=7.0
Pod documented
289 1 my ($self, @args) = @_;
290 1 my $caller = $self->{_name};
291 1 ▶ if (! $caller->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $caller->can("new")) 1 0 1
292 doconstructor: {
293 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
294 1 ▶ *{"${caller}::new"} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
295 1 my ($cself, @cargs) = @_;
296 1 bless {}, $cself;
297 1 shift;
298 1 ▶ if ($cself->can('__init')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cself->can("__init")) 0 1 1
299 0 $cself->__init(@_);
300 }
301
302 1 return $cself;
303 1 };
304 }
305
306 1 return $self;
307 }
308 }
309  
310 0 ▶sub create_class {
Subroutine create_class: not called CC=2 SCAR=14.0
Pod undocumented
311 0 my ($self, $class) = @_;
312 0 my $caller = $self->{_name};
313 0 ▶ if ($self->class_exists($caller)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class_exists($caller)) { } 0 0 0
314 0 warn "Can't create class '$class'. Already exists";
315 0 return 0;
316 }
317 else {
318 {
319 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
320 0 ▶ *{"${class}::new"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
321 0 return bless {}, $class;
322 0 };
323 }
324 }
325  
326 0 return 1;
327 }
328  
329 0 ▶sub create_method {
Subroutine create_method: not called CC=3 SCAR=21.3
Pod documented
330 0 my ($self, $name, $code) = @_;
331 0 my $class = $self->{_name};
332 0 ▶ if ($self->class_exists($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class_exists($class)) { } 0 0 0
333 {
334 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
335 0 ▶ if ($self->method_exists($class, $name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->method_exists($class, $name)) 0 0 0
336 0 warn "Method $name already exists in $class. Did you mean to use override_method()?";
337 0 return 0;
338 }
339
340 0 *{"${class}::${name}"} = $code;
341 }
342 }
343 else {
344 0 warn "Can't create ${name} in ${class}, because ${class} does not exist";
345 0 return 0;
346 }
347  
348 0 return $self;
349 }
350  
351 1 ▶sub override_method {
Subroutine override_method: called CC=2 SCAR=7.1
Pod documented
352 1 my ($self, $name, $method) = @_;
353 1 my $class = $self->{_name};
354 {
355 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
356 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
357 1 ▶ if (! $self->method_exists($class, $name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->method_exists($class, $name)) 0 1 1
358 0 warn "Cant't find '$name' in class $class - override_method()";
359 0 return 0;
360 }
361
362 1 *{"${class}::${name}"} = $method;
363 }
364 }
365  
366 0 ▶sub last_errors {
Subroutine last_errors: not called CC=1 SCAR=6.9
Pod undocumented
367 0 my $self = shift;
368 0 my $errors = $self->{errors};
369 0 $self->{errors} = [];
370 0 return $errors;
371 }
372  
373 0 ▶sub add_hook {
Subroutine add_hook: not called CC=11 SCAR=46.0
Pod documented
374 0 my ($self, %args) = @_;
375 0 my $caller = $self->{_name};
376 my ($type, $class, $method, $code) = (
377 $args{'type'},
378 $self->{_name},
379 $args{'name'},
380 0 $args{'method'}
381 );
382  
383 0 ▶ if ($self->class_exists($caller)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class_exists($caller)) { } 0 0 0
384 0 ▶ if ($type && $class && $method && $code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $class and $method and $code) { } 0 0 0
Condition0%0/9
$type and $class and $method and $code
!ll&&!rl&&r
000
$type and $class and $method
!ll&&!rl&&r
000
$type and $class
!ll&&!rl&&r
000
MC/DC0%0/4
$type and $class and $method and $code
$type $class $method $code
Truth table0%0/5
A and B and C and D
A= $typeB= $classC= $methodD= $code
ABCDresult
0XXX0
10XX0
110X0
11100
11111
385 0 ▶ if (! $self->method_exists($class, $method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->method_exists($class, $method)) 0 0 0
386 0 warn "Can't add hook because class $class does not have method $method";
387 0 return 0;
388 }
389  
390 0 my $fullpkg = "${class}::${method}";
391 0 my $old_code = \&{$fullpkg};
392 0 my $new_code;
393  
394 addhook: {
395 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
396 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
397 0 for ($type) {
398 0 ▶ if (/after/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/after/) { } 0 0 0
elsif (/before/) { } 0 0 0
elsif (/around/) { } 0 0 0
399 0 ▶ *{"${fullpkg}"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
400 0 $old_code->(@_);
401 0 $code->(@_);
402 0 };
403 }
404 elsif (/before/) {
405 0 ▶ *{"${fullpkg}"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
406 0 $code->(@_);
407 0 $old_code->(@_);
408 0 };
409 }
410 elsif (/around/) {
411 0 ▶ *{"${fullpkg}"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
412 0 $code->($old_code, @_);
413 0 };
414 }
415 else {
416 0 warn "Unknown hook type: $type";
417 0 return 0;
418 }
419 }
420 }
421 0 return $self;
422 }
423 else {
424 0 warn "Hook expecting type, class, method, and code";
425 0 return 0;
426 }
427 }
428 else {
429 0 warn "Can't add hook becase class '$class' does not exist";
430 0 return 0;
431 }
432 }
433  
434 0 ▶sub clone_object {
Subroutine clone_object: not called CC=2 SCAR=17.9
Pod documented
435 0 my $self = shift;
436 0 my $class = $self->{_name};
437 0 ▶ if (! ref($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $class) 0 0 0
438 0 warn "clone_object() expects a reference\n";
439 0 return 0;
440 }
441 0 bless { %{ $class } }, ref $class;
442 }
443  
444 0 ▶sub delete_method {
Subroutine delete_method: not called CC=1 SCAR=1.7
Pod undocumented
445 0 my ($self, $name) = @_;
446 0 my $class = $self->{_name};
447 {
448 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
449 #$class = \%{"$class\::"};
450 0 delete $class::{$name};
451 }
452 }
453  
454 0 ▶sub get_attributes {
Subroutine get_attributes: not called CC=1 SCAR=6.9
Pod undocumented
455 0 my $self = shift;
456 0 my $class = $self->{_name};
457 0 return $self->{_attributes}->{$class};
458 }
459  
460 2 ▶sub _add_attribute {
Subroutine _add_attribute: called CC=2 SCAR=6.9
461 2 my ($self, $class, $attr, $value) = @_;
462 2 ▶ if ($self->{_attributes}->{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_attributes'}{$class}) { } 1 1 2
463 1 $self->{_attributes}->{$class}->{$attr} = $value;
464 }
465 else {
466 1 $self->{_attributes}->{$class} = {
467 $attr => $value,
468 };
469 }
470 }
471 =head1 METHODS
472  
473 =head2 init
474  
475 Initialises a class. This won't create a new one, but will set the current class as the one specified, if it
476 exists.
477 You can then chain other methods onto this, or save it into a variable for repeated use.
478  
479 Class::LOP->init('SomeClass');
480  
481 =head2 new
482  
483 Initialises a class, but will also create a new one should it not exist. If you're wanting to initialise a class
484 you know exists, you're probably better off using C<init>, as it involves less work.
485  
486 Class::LOP->new('MyNewClass')
487 ->create_method('foo', sub { print "foo!\n" });
488  
489 my $class = MyNewClass->new();
490 $class->foo(); # prints foo!
491  
492 Using C<new> then chaining C<create_method> onto it, we were able to create a class and a method on-the-fly.
493  
494 =head2 warnings_strict
495  
496 Enables C<use warnings> and C<use strict> pragmas in Class::LOP modules
497  
498 $class->warnings_strict();
499  
500 =head2 getscope
501  
502 Basically just a C<caller>. Use this in your modules to return the class name
503  
504 my $caller = $class->getscope();
505  
506 =head2 class_exists
507  
508 Checks to make sure the class has been imported
509  
510 use Some::Module;
511  
512 if ($class->class_exists()) {
513 print "It's there!\n";
514 }
515  
516 =head2 method_exists
517  
518 Detects if a specific method in a class exists
519  
520 if ($class->method_exists($method_name)) { .. }
521  
522 =head2 subclasses
523  
524 Returns an list of subclassed modules
525  
526 my @subclass_mods = $class->subclasses();
527 for (@subclass_mods) {
528 print "$_\n";
529 }
530  
531 =head2 superclasses
532  
533 Returns a list of superclass (base) modules
534  
535 my @superclass_mods = $class->superclasses();
536 for (@superclass_mods) {
537 print "$_\n";
538 }
539  
540 =head2 import_methods
541  
542 Injects existing methods from the scoped module to a specified class
543  
544 $class->import_methods($destination_class, qw/this that and this/);
545  
546 Optionally, C<import_methods> can return errors if certain methods don't exist. You can read these
547 errors with C<last_errors>. This is only experimental at the moment.
548  
549 =head2 extend_class
550  
551 Pretty much the same as C<use base 'Mother::Class'>. The first parameter is the subclass, and the following array
552 will be its "mothers".
553  
554 my @mommys = qw(This::Class That::Class);
555 $class->extend_class(@mommys)
556  
557 =head2 have_accessors
558  
559 Adds Moose-style accessors to a class. First parameter is the class, second will be the name of the method to
560 create accessors.
561  
562 # Goosey.pm
563 $class->have_accessors('acc');
564  
565 # test.pl
566 use Goosey;
567  
568 acc 'x' => ( is => 'rw', default => 7 );
569  
570 Currently the only two options is C<default> and C<is>.
571  
572 =head2 create_constructor
573  
574 Simply adds the C<new> method to your class. I'm wondering whether this should be done automatically? The
575 aim of this module is to give the author as much freedom as possible, so I chose not to.
576  
577 $class->create_constructor;
578  
579 =head2 create_method
580  
581 Adds a new method to an existing class.
582  
583 $class->create_method('greet', sub {
584 my $self = shift;
585 print "Hello, World from " . ref($self) . "\n";
586 });
587  
588 MooClass->greet();
589  
590 =head2 add_hook
591  
592 Adds hook modifiers to your class. It won't import them all - only use what you need :-)
593  
594 $class->add_hook(
595 type => 'after',
596 method => $name,
597 code => $code,
598 );
599  
600 The types are C<after>, C<before>, and C<around>.
601  
602 =head2 list_methods
603  
604 Returns a list of all the methods within an initialised class. It will filter out classes
605  
606 my @methods = Class::LOP->init('SomeClass')->list_methods();
607  
608 =head2 clone_object
609  
610 Takes an object and spits out a clone of it. This means mangling the original will have no side-effects to the cloned one
611 I know L<DateTime> has its own C<clone> method, but still, it's a good example.
612  
613 my $dt = DateTime->now;
614 my $dt2 = Class::LOP->init($dt)->clone_object;
615  
616 print $dt->add(days => 5)->dmy() . "\n";
617 print $dt2->dmy() . "\n";
618  
619 Simply changing C<$dt2 = $dt> would mean both results would have the same date when we printed them, but because we cloned the object, they are separate.
620  
621 =head2 override_method
622  
623 Unlike C<create_method>, this method will let you replace the existing one, thereby overriding it.
624  
625 sub greet { print "Hello\n"; }
626
627 Class::LOP->init('ClassName')->override_method('greet', sub { print "Sup\n" });
628  
629 greet(); # prints Sup
630  
631 =head1 AUTHOR
632  
633 Brad Haywood <brad@perlpowered.com>
634  
635 =head1 LICENSE
636  
637 This library is free software. You can redistribute it and/or modify
638 it under the same terms as Perl itself.
639  
640 =cut
641  
642 4 1;
« Sub/Mage.pm ↑ Summary