| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Sub::Lexical; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | $VERSION = 0.81; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | eval q(use warnings) or local $^W = 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Regexp::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Carp qw(croak cluck); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use constant DEBUG => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 17 | 3 | ▶ | croak('Sub::Lexical constructor must be called as a class method') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | if $class ne __PACKAGE__; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | cluck("arguments passed to new() aren't in pair form") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | if @_ % 2 != 0; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ## don't stuff list in if it don't fit | ||||||||||||||||||||||||||||
| 24 | 3 | ▶ | my $self = { @_ % 2 == 0 ? @_ : () }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 3 | bless($self, $class); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub subs_found { | ||||||||||||||||||||||||||
|
Subroutine
subs_found: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | return [] unless defined $self->{info}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | return $self->{info}; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | my $brackets_re = $RE{balanced}{-parens => '{}'}; | |||||||||||||||||||||||||||
| 36 | 3 | my $paren_re = $RE{balanced}{-parens => '()'}; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | ## regex for fully qualified names which I don't want/need | ||||||||||||||||||||||||||||
| 39 | # my $sub_fullname_re = qr/[_a-zA-Z](?:(?:\w*)(?:(?:'|::)(?:\w+)+)?)*/; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | my $sub_name_re = qr{[_a-zA-Z](?:[\w_]+)?}; | |||||||||||||||||||||||||||
| 42 | 3 | my $sub_proto_re = qr{\([\$%\\@&\s]*\)}; | |||||||||||||||||||||||||||
| 43 | 3 | my $sub_attrib_re = qr{(?:\s*:\s*$sub_name_re\s*(?:$paren_re)?)*}o; | |||||||||||||||||||||||||||
| 44 | ## my sub foobar (proto) : attrib { "code" } | ||||||||||||||||||||||||||||
| 45 | 3 | my $sub_match_re = qr/ | |||||||||||||||||||||||||||
| 46 | my # literal 'my' | ||||||||||||||||||||||||||||
| 47 | \s+ # 1> whitespace | ||||||||||||||||||||||||||||
| 48 | sub # literal 'my' | ||||||||||||||||||||||||||||
| 49 | \s+ # 1> whitespace | ||||||||||||||||||||||||||||
| 50 | ($sub_name_re) # group 1 | ||||||||||||||||||||||||||||
| 51 | \s* # 0> whitespace | ||||||||||||||||||||||||||||
| 52 | ( # group 2 | ||||||||||||||||||||||||||||
| 53 | $sub_proto_re ? # optional $sub_proto_re | ||||||||||||||||||||||||||||
| 54 | $sub_attrib_re ? # optional $sub_attrib_re | ||||||||||||||||||||||||||||
| 55 | ) ? # optional group 2 | ||||||||||||||||||||||||||||
| 56 | \s* # 0> whitespace | ||||||||||||||||||||||||||||
| 57 | ( # group 3 | ||||||||||||||||||||||||||||
| 58 | $brackets_re # match balanced brackets | ||||||||||||||||||||||||||||
| 59 | ) ? # optional group 3 | ||||||||||||||||||||||||||||
| 60 | (?: | ||||||||||||||||||||||||||||
| 61 | \s* # 0> whitespace | ||||||||||||||||||||||||||||
| 62 | ; # optional literal ';' | ||||||||||||||||||||||||||||
| 63 | ) ? | ||||||||||||||||||||||||||||
| 64 | /xo; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ## core functions which may expect a function e.g goto &foo | ||||||||||||||||||||||||||||
| 67 | 3 | my $core_funcs = join '|', qw(do defined eval goto grep map sort undef); | |||||||||||||||||||||||||||
| 68 | ## things that *can't* come before or go after a bareword | ||||||||||||||||||||||||||||
| 69 | 3 | my $ops_before = qr/(?<! \$ | % | @ ) | (?>! -> )/x; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | sub filter_code { | ||||||||||||||||||||||||||
|
Subroutine
filter_code: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | 3 | ▶ | croak('filter_code() must be called as an object method') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $self or $self eq __PACKAGE__
MC/DC0%0/2
not defined $self or $self eq __PACKAGE__
not defined $self
$self eq __PACKAGE__
Truth table33%1/3
not(A) or B
A= not defined $selfB= $self eq __PACKAGE__
| |||||||||||||||||||||||||||||
| 74 | if not defined $self or $self eq __PACKAGE__; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | my $code = shift; | |||||||||||||||||||||||||||
| 77 | 3 | study $code; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | while(my($subname, $subextra, $subcode) = $code =~ /$sub_match_re/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | push @{$self->{info}}, { | |||||||||||||||||||||||||||
| 81 | name => $subname, | ||||||||||||||||||||||||||||
| 82 | extra => $subextra, | ||||||||||||||||||||||||||||
| 83 | code => $subcode | ||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | my $lexname = "\$LEXSUB_${subname}"; | |||||||||||||||||||||||||||
| 87 | ## 'my sub name {}' => 'my $name; $name = sub {};' | ||||||||||||||||||||||||||||
| 88 | 2 | $code =~ s<$sub_match_re> | |||||||||||||||||||||||||||
| 89 | <my \$LEXSUB_$1; \$LEXSUB_$1 = sub $2 $3;>g; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | ## '&name()' => '$name->()' | ||||||||||||||||||||||||||||
| 92 | 2 | $code =~ s< | |||||||||||||||||||||||||||
| 93 | &? # optional & | ||||||||||||||||||||||||||||
| 94 | $subname # 'subname' | ||||||||||||||||||||||||||||
| 95 | \s* # 0+ whitespace | ||||||||||||||||||||||||||||
| 96 | ( # group $1 | ||||||||||||||||||||||||||||
| 97 | $paren_re # balanced parens | ||||||||||||||||||||||||||||
| 98 | ) # optional group $1 | ||||||||||||||||||||||||||||
| 99 | 4 | ▶ | >{"$lexname->" . ($1 || '()')}exg; | ||||||||||||||||||||||||||
|
Condition50%1/2
$1 || '()'
MC/DC0%0/1
$1 || '()'
$1
Truth table50%1/2
A
A= $1
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | ## 'goto &name' => 'goto &$name' | ||||||||||||||||||||||||||||
| 102 | 2 | $code =~ s<($core_funcs) \s* &$subname\b> | |||||||||||||||||||||||||||
| 103 | {$1 &$lexname}xg; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | ## '&name' => '$name->(@_)' | ||||||||||||||||||||||||||||
| 106 | 2 | $code =~ s{ (?<!\\) \s* &$subname\b } | |||||||||||||||||||||||||||
| 107 | {$lexname->(\@_)}xg; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | ## '\&name' => '$name' | ||||||||||||||||||||||||||||
| 110 | 2 | $code =~ s<(?: \\ \s*)+ &($sub_name_re)\b> | |||||||||||||||||||||||||||
| 111 | <\$LEXSUB_$1>xg; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | ## 'name' => '$name->()' | ||||||||||||||||||||||||||||
| 114 | 2 | $code =~ s{(?: ^ | (?<! LEXSUB_) ( (?: $ops_before | \s+) \s* ) ) | |||||||||||||||||||||||||||
| 115 | $subname \b } | ||||||||||||||||||||||||||||
| 116 | {$1$lexname->()}xmg; | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 3 | return $code; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | use Filter::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3 | ▶ | FILTER_ONLY code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 124 | 3 | $_ = Sub::Lexical->new()->filter_code($_); | |||||||||||||||||||||||||||
| 125 | 3 | }; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 3 | q(package activated); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||