File Coverage

example/lib/Method/Modifiers.pm
Criterion Covered Total %
statement 33 35 94.2
branch 4 6 66.6
condition 2 3 66.6
subroutine 9 10 90.0
pod 4 4 100.0
total 52 58 89.6


line stmt bran cond sub pod time code
1             package # this is an example for modify_subroutine()/subroutne_modifier().
2             Method::Modifiers;
3              
4 2     2   33576 use strict;
  2         3  
  2         55  
5 2     2   9 use warnings;
  2         3  
  2         107  
6              
7             our $VERSION = '0.66';
8              
9 2     2   10 use Exporter qw(import);
  2         3  
  2         137  
10              
11             our @EXPORT = qw(before around after);
12             our @EXPORT_OK = (@EXPORT, qw(add_method_modifier));
13             our %EXPORT_TAGS = (
14             all => \@EXPORT_OK,
15             moose => \@EXPORT,
16             );
17              
18 2     2   652 use Data::Util ();
  2         2  
  2         229  
19              
20             sub _croak{
21 0     0   0 require Data::Util::Error;
22 0         0 goto &Data::Util::Error::croak;
23             }
24              
25             sub add_method_modifier{
26 12     12 1 11 my $into = shift;
27 12         11 my $type = shift;
28 12         10 my $modifier = pop;
29              
30 12         16 foreach my $name(@_){
31 12         24 my $method = Data::Util::get_code_ref($into, $name);
32              
33 12 100 66     40 if(!$method || !Data::Util::subroutine_modifier($method)){
34              
35 4 50       12 unless($method){
36 4 50       49 $method = $into->can($name)
37             or _croak(qq{The method '$name' is not found in the inheritance hierarchy for class $into});
38             }
39              
40 4         23 $method = Data::Util::modify_subroutine($method, $type => [$modifier]);
41              
42 2     2   8 no warnings 'redefine';
  2         2  
  2         268  
43 4         14 Data::Util::install_subroutine($into, $name => $method);
44             }
45             else{ # $method exists and is modified
46 8         24 Data::Util::subroutine_modifier($method, $type => $modifier);
47             }
48             }
49 12         1106 return;
50             }
51              
52             sub before{
53 4     4 1 17 my $into = caller;
54 4         9 add_method_modifier($into, before => @_);
55             }
56             sub around{
57 4     4 1 17 my $into = caller;
58 4         5 add_method_modifier($into, around => @_);
59             }
60             sub after{
61 4     4 1 541 my $into = caller;
62 4         9 add_method_modifier($into, after => @_);
63             }
64              
65              
66             1;
67             __END__