File Coverage

blib/lib/Class/Anonymous/Utils.pm
Criterion Covered Total %
statement 53 53 100.0
branch 15 22 68.1
condition n/a
subroutine 11 11 100.0
pod 0 4 0.0
total 79 90 87.7


line stmt bran cond sub pod time code
1             package Class::Anonymous::Utils;
2              
3 3     3   1760 use strict;
  3         6  
  3         65  
4 3     3   12 use warnings;
  3         4  
  3         64  
5              
6 3     3   12 use Carp;
  3         5  
  3         188  
7 3     3   13 use Exporter 'import';
  3         5  
  3         1653  
8              
9             our @EXPORT_OK = qw/method before after around/;
10              
11             our %EXPORT_TAGS = (
12             all => \@EXPORT_OK,
13             );
14              
15             sub method {
16 6 50   6 0 47 croak "'method' requires a name and a callback" unless @_ == 2;
17 6         12 my ($name, $cb) = @_;
18 6         19 $Class::Anonymous::CURRENT->($name => $cb);
19             }
20              
21             sub before {
22 1 50   1 0 7 croak "'before' requires a name and a callback" unless @_ == 2;
23 1         2 my ($name, $cb) = @_;
24 1         3 my $old = $Class::Anonymous::CURRENT->($name);
25 1 50       3 croak "$name is not a method of the object" unless $old;
26             my $new = sub {
27 3     3   3 my $self = shift;
28 3         5 my $want = wantarray;
29 3 100       8 if ($want) {
    100          
30 1         4 () = $self->$cb(@_);
31 1         6 return $self->$old(@_);
32             } elsif (defined $want) {
33 1         3 scalar $self->$cb(@_);
34 1         7 return $self->$old(@_);
35             } else {
36 1         4 $self->$cb(@_);
37 1         6 $self->$old(@_);
38             }
39 1         5 };
40 1         4 $Class::Anonymous::CURRENT->($name => $new);
41             }
42              
43             sub after {
44 1 50   1 0 7 croak "'after' requires a name and a callback" unless @_ == 2;
45 1         2 my ($name, $cb) = @_;
46 1         4 my $old = $Class::Anonymous::CURRENT->($name);
47 1 50       3 croak "$name is not a method of the object" unless $old;
48             my $new = sub {
49 3     3   4 my $self = shift;
50 3         4 my $want = wantarray;
51 3 100       9 if ($want) {
    100          
52 1         4 my @ret = $self->$old(@_);
53 1         7 () = $self->$cb(@_);
54 1         7 return @ret;
55             } elsif (defined $want) {
56 1         3 my $ret = $self->$old(@_);
57 1         5 scalar $self->$cb(@_);
58 1         5 return $ret;
59             } else {
60 1         3 $self->$old(@_);
61 1         5 $self->$cb(@_);
62             }
63 1         4 };
64 1         3 $Class::Anonymous::CURRENT->($name => $new);
65             }
66              
67             sub around {
68 2 50   2 0 21 croak "'around' requires a name and a callback" unless @_ == 2;
69 2         5 my ($name, $cb) = @_;
70 2         7 my $old = $Class::Anonymous::CURRENT->($name);
71 2 50       8 croak "$name is not a method of the object" unless $old;
72 2     2   20 $Class::Anonymous::CURRENT->($name => sub { $cb->($old, @_) });
  2         7  
73             }
74              
75             1;
76