File Coverage

blib/lib/self.pm
Criterion Covered Total %
statement 51 56 91.0
branch 11 16 68.7
condition 3 3 100.0
subroutine 12 12 100.0
pod 2 2 100.0
total 79 89 88.7


line stmt bran cond sub pod time code
1 17     17   444098 use strict;
  17         43  
  17         653  
2 17     17   92 use warnings;
  17         29  
  17         904  
3              
4             package self;
5 17     17   452 use 5.006;
  17         59  
  17         938  
6              
7             our $VERSION = '0.34';
8 17     17   19323 use Sub::Exporter;
  17         329651  
  17         117  
9              
10 17     17   22162 use Devel::Declare ();
  17         150216  
  17         1352  
11 17     17   26139 use B::Hooks::Parser;
  17         15653  
  17         12305  
12              
13             my $NO_SELF;
14            
15             sub import {
16 17     17   167 my ($class) = @_;
17 17         48 my $caller = caller;
18              
19 17         453 B::Hooks::Parser::setup();
20              
21 17         66 my $linestr = B::Hooks::Parser::get_linestr();
22 17         104 my $offset = B::Hooks::Parser::get_linestr_offset();
23 17         68 substr($linestr, $offset, 0) = 'use B::OPCheck const => check => \&self::_check;';
24 17         439 B::Hooks::Parser::set_linestr($linestr);
25              
26 17         200 my $exporter = Sub::Exporter::build_exporter({
27             into_level => 1,
28             exports => [qw(self args)],
29             groups => { default => [ -all ] }
30             });
31 17         3808 $exporter->(@_);
32             }
33              
34             sub unimport {
35 1     1   10 my ($class) = @_;
36 1         2 my $caller = caller;
37 1         10 $NO_SELF = 1;
38             }
39              
40             sub _check {
41 285     285   558427 my $op = shift;
42 285         718 my $caller = caller;
43 285 100       2933 return if $NO_SELF;
44 270 100       2237 return unless ref($op->gv) eq 'B::PV';
45              
46 240         962 my $linestr = B::Hooks::Parser::get_linestr;
47 240         526 my $offset = B::Hooks::Parser::get_linestr_offset;
48              
49 240         295 my $code = 'my($self,@args)=@_;';
50 240 50       6890 if (substr($linestr, $offset, 3) eq 'sub') {
    100          
51 0         0 my $line = substr($linestr, $offset);
52 0 0       0 if ($line =~ m/^sub\s.*{ /x ) {
53 0 0       0 if (index($line, "{$code") < 0) {
54 0         0 substr($linestr, $offset + index($line, '{') + 1, 0) = $code;
55 0         0 B::Hooks::Parser::set_linestr($linestr);
56             }
57             }
58             }
59              
60             # This elsif block handles:
61             # sub foo
62             # {
63             # ...
64             # }
65             elsif (index($linestr, 'sub') >= 0) {
66 58         169 $offset += Devel::Declare::toke_skipspace($offset);
67 58 100       6030 if ($linestr =~ /(sub.*?\n\s*{)/) {
68 4         13 my $pos = index($linestr, $1);
69 4 100       174 if ($pos + length($1) - 1 == $offset) {
70 2         7 substr($linestr, $offset + 1, 0) = $code;
71 2         73 B::Hooks::Parser::set_linestr($linestr);
72             }
73             }
74             }
75              
76             }
77              
78             sub _args {
79 77     77   87 my $level = 2;
80 77         103 my @c = ();
81             package DB;
82 77   100     1348 @c = caller($level++)
83             while !defined($c[3]) || $c[3] eq '(eval)';
84 77         447 return @DB::args;
85             }
86              
87             sub self {
88 66     66 1 6383 (_args)[0];
89             }
90              
91             sub args {
92 11     11 1 64 my @a = _args;
93 11         41 return @a[1..$#a];
94             }
95              
96             1;
97              
98             __END__