File Coverage

blib/lib/self.pm
Criterion Covered Total %
statement 50 56 89.2
branch 16 22 72.7
condition 3 3 100.0
subroutine 11 11 100.0
pod 2 2 100.0
total 82 94 87.2


line stmt bran cond sub pod time code
1 17     17   2864031 use strict;
  17         123  
  17         504  
2 17     17   88 use warnings;
  17         31  
  17         543  
3              
4             package self;
5 17     17   538 use 5.006;
  17         63  
6              
7             our $VERSION = '0.36';
8 17     17   10195 use Sub::Exporter;
  17         223007  
  17         98  
9              
10 17     17   12156 use B::Hooks::Parser;
  17         28391  
  17         11800  
11              
12             my $NO_SELF;
13              
14             sub import {
15 18     18   184 my ($class) = @_;
16 18         44 my $caller = caller;
17              
18 18         427 B::Hooks::Parser::setup();
19              
20 18         88 my $linestr = B::Hooks::Parser::get_linestr();
21 18         65 my $offset = B::Hooks::Parser::get_linestr_offset();
22 18         2075 substr($linestr, $offset, 0) = 'use B::OPCheck const => check => \&self::_check;';
23 18         1899 B::Hooks::Parser::set_linestr($linestr);
24              
25 18         147 my $exporter = Sub::Exporter::build_exporter({
26             into_level => 1,
27             exports => [qw(self args)],
28             groups => { default => [ -all ] }
29             });
30 18         8341 $exporter->(@_);
31             }
32              
33             sub unimport {
34 1     1   9 my ($class) = @_;
35 1         3 my $caller = caller;
36 1         68 $NO_SELF = 1;
37             }
38              
39             sub _check {
40 351     351   161558 my $op = shift;
41 351         567 my $caller = caller;
42 351 100       2756 return if $NO_SELF;
43 337 100       1981 return unless ref($op->gv) eq 'B::PV';
44              
45 303         1139 my $linestr = B::Hooks::Parser::get_linestr;
46 303         560 my $offset = B::Hooks::Parser::get_linestr_offset;
47 303         511 my $line = substr($linestr, $offset);
48              
49 303         359 my $code = 'my($self,@args)=@_;';
50              
51             # This cover cases like:
52             # sub foo { ... }
53             # Offset is at the first '{' because subroutine name is also a "const"
54 303 100       2365 if (substr($linestr, $offset, 1) eq '{') {
    50          
    100          
55 88 100       1214 if (substr($linestr, 0, $offset) =~ m/sub\s\S+\s*\z/x ) {
56 57 100       367 if (index($line, "{$code") < 0) {
57 49         123 substr($linestr, $offset + 1, 0) = $code;
58 49         1214 B::Hooks::Parser::set_linestr($linestr);
59             }
60             }
61             }
62             elsif (substr($linestr, $offset, 3) eq 'sub') {
63 0 0       0 if ($line =~ m/^sub\s.*{ /x ) {
64 0 0       0 if (index($line, "{$code") < 0) {
65 0         0 substr($linestr, $offset + index($line, '{') + 1, 0) = $code;
66 0         0 B::Hooks::Parser::set_linestr($linestr);
67             }
68             }
69             }
70              
71             # This elsif block handles:
72             # sub foo
73             # {
74             # ...
75             # }
76             elsif (index($linestr, 'sub') >= 0) {
77 57         210 $offset += B::Hooks::Toke::skipspace($offset);
78 57 100       1355 if ($linestr =~ /(sub.*?\n\s*{)/) {
79 2         11 my $pos = index($linestr, $1);
80 2 50       81 if ($pos + length($1) - 1 == $offset) {
81 0         0 substr($linestr, $offset + 1, 0) = $code;
82 0         0 B::Hooks::Parser::set_linestr($linestr);
83             }
84             }
85             }
86             }
87              
88             sub _args {
89 78     78   103 my $level = 2;
90 78         108 my @c = ();
91             package DB;
92 78   100     898 @c = caller($level++)
93             while !defined($c[3]) || $c[3] eq '(eval)';
94 78         560 return @DB::args;
95             }
96              
97             sub self {
98 67     67 1 38769 (_args)[0];
99             }
100              
101             sub args {
102 11     11 1 93 my @a = _args;
103 11         42 return @a[1..$#a];
104             }
105              
106             1;
107              
108             __END__