| 1 | package PerlX::MethodCallWithBlock; | ||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 10 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Devel::Declare (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use B::Hooks::EndOfScope (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use B::OPCheck (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use PPI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use PPI::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 12 | ▶ | sub inject_close_paren { | ||||||||||||||||||||||||||
|
Subroutine
inject_close_paren: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 12 | my $linestr = Devel::Declare::get_linestr; | |||||||||||||||||||||||||||
| 16 | 12 | my $offset = Devel::Declare::get_linestr_offset; | |||||||||||||||||||||||||||
| 17 | 12 | substr($linestr, $offset, 0) = ')'; | |||||||||||||||||||||||||||
| 18 | 12 | Devel::Declare::set_linestr($linestr); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 591 | ▶ | sub checker { | ||||||||||||||||||||||||||
|
Subroutine
checker: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 591 | my ($op, @args) = @_; | |||||||||||||||||||||||||||
| 23 | 591 | my $offset = Devel::Declare::get_linestr_offset; | |||||||||||||||||||||||||||
| 24 | 591 | $offset += Devel::Declare::toke_skipspace($offset); | |||||||||||||||||||||||||||
| 25 | 591 | my $linestr = Devel::Declare::get_linestr; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 591 | ▶ | return if $offset > length($linestr); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 591 | my $code = substr($linestr, $offset); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 591 | my $doc = PPI::Document->new(\$code); | |||||||||||||||||||||||||||
| 32 | 591 | ▶ | return unless $doc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # find the structure of "->method(...) {" | ||||||||||||||||||||||||||||
| 35 | my $found = $doc->find( | ||||||||||||||||||||||||||||
| 36 | 6553 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.3
| |||||||||||||||||||||||||||||
| 37 | 6553 | my $el = $_[1]; | |||||||||||||||||||||||||||
| 38 | 6553 | ▶ | return 0 unless $el->class eq 'PPI::Token::Operator' && $el->content eq '->'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$el->class eq "PPI::Token::Operator" and $el->content eq "->"
MC/DC100%2/2
$el->class eq "PPI::Token::Operator" and $el->content eq "->"
$el->class eq "PPI::Token::Operator"
$el->content eq "->"
Truth table100%3/3
A and B
A= $el->class eq "PPI::Token::Operator"B= $el->content eq "->"
| |||||||||||||||||||||||||||||
| 39 | 134 | ▶ | my $word = $el->snext_sibling or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 134 | ▶ | return 0 unless $word->class eq 'PPI::Token::Word'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 105 | ▶ | my $args = $word->snext_sibling or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 105 | ▶ | if ($args->class eq 'PPI::Structure::List') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 44 | 87 | ▶ | my $block = $args->snext_sibling or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 35 | ▶ | return 0 unless $block->class eq 'PPI::Structure::Block'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | elsif ($args->class ne 'PPI::Structure::Block') { | ||||||||||||||||||||||||||||
| 48 | 0 | return 0 | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 22 | return 1; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 591 | ); | |||||||||||||||||||||||||||
| 54 | 591 | ▶ | return unless $found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 22 | my $injected_code = 'sub { BEGIN { B::Hooks::EndOfScope::on_scope_end(\&PerlX::MethodCallWithBlock::inject_close_paren); }'; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 22 | my $pnode; | |||||||||||||||||||||||||||
| 59 | 22 | $code = ""; | |||||||||||||||||||||||||||
| 60 | 22 | for my $node (@$found) { | |||||||||||||||||||||||||||
| 61 | 22 | $pnode = $node; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 22 | ▶ | while($pnode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 66 | my $prev_node = $pnode; | |||||||||||||||||||||||||||
| 65 | 66 | ▶ | while ($prev_node = $prev_node->previous_sibling) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 22 | $code = $prev_node->content . $code; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 66 | $pnode = $pnode->parent; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 22 | $code .= join "", map { $_->content } ($node, $node->snext_sibling); | |||||||||||||||||||||||||||
| 72 | 22 | my $word = $node->snext_sibling; | |||||||||||||||||||||||||||
| 73 | 22 | ▶ | if ($word->snext_sibling->class eq 'PPI::Structure::Block') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 18 | my $block = $word->snext_sibling; | |||||||||||||||||||||||||||
| 75 | 18 | my @block_elements = $block->elements; | |||||||||||||||||||||||||||
| 76 | # one-line block, we see the whole thing here. | ||||||||||||||||||||||||||||
| 77 | 18 | ▶ | if ($block_elements[0] eq '{' && $block_elements[-1] eq '}') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$block_elements[0] eq "{" and $block_elements[-1] eq "}"
MC/DC50%1/2
$block_elements[0] eq "{" and $block_elements[-1] eq "}"
$block_elements[0] eq "{"
$block_elements[-1] eq "}"
Truth table66%2/3
A and B
A= $block_elements[0] eq "{"B= $block_elements[-1] eq "}"
| |||||||||||||||||||||||||||||
| 78 | 10 | my $block_code = $node->snext_sibling->snext_sibling->content; | |||||||||||||||||||||||||||
| 79 | 10 | $code .= "(sub $block_code)"; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # There might be something more after the block... | ||||||||||||||||||||||||||||
| 82 | 10 | my $next_node = $block->next_sibling; | |||||||||||||||||||||||||||
| 83 | 10 | ▶ | while ($next_node) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 5 | $code .= $next_node->content; | |||||||||||||||||||||||||||
| 85 | 5 | $next_node = $next_node->next_sibling; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||
| 88 | 8 | $code .= "($injected_code"; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||
| 92 | 4 | my $args = $word->snext_sibling->content; | |||||||||||||||||||||||||||
| 93 | 4 | $args =~ s/\)$/,$injected_code/; | |||||||||||||||||||||||||||
| 94 | 4 | $code .= $args; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 22 | substr($linestr, $offset) = $code; | |||||||||||||||||||||||||||
| 97 | 22 | Devel::Declare::set_linestr($linestr); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 10 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | 10 | my $caller = caller; | |||||||||||||||||||||||||||
| 103 | 10 | my $offset = Devel::Declare::get_linestr_offset(); | |||||||||||||||||||||||||||
| 104 | 10 | my $linestr = Devel::Declare::get_linestr(); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 10 | substr($linestr, $offset, 0) = q[BEGIN { B::OPCheck->import($_ => check => \&PerlX::MethodCallWithBlock::checker) for qw(const pushmark lineseq refgen sassign); }]; | |||||||||||||||||||||||||||
| 107 | 10 | Devel::Declare::set_linestr($linestr); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 10 | 1; | |||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||