| 1 | package Treex::Block::Util::Find; | ||||||||||||||||||||||||||||
| 2 | 1 | $Treex::Block::Util::Find::VERSION = '2.20210102'; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Treex::Core::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | extends 'Treex::Block::Util::Eval'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | has on_error => ( | |||||||||||||||||||||||||||
| 8 | is => 'ro', | ||||||||||||||||||||||||||||
| 9 | isa => enum( [qw(fatal warn ignore)] ), | ||||||||||||||||||||||||||||
| 10 | default => 'fatal', | ||||||||||||||||||||||||||||
| 11 | documentation => 'what to do on errors in the code for eval', | ||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | has expand_code => ( | |||||||||||||||||||||||||||
| 15 | is=> 'ro', | ||||||||||||||||||||||||||||
| 16 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 17 | default => 1, | ||||||||||||||||||||||||||||
| 18 | documentation => 'Should "$." be expanded to "$this->" in all eval codes?', | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | has max_nodes_per_tree => ( | |||||||||||||||||||||||||||
| 22 | is=> 'ro', | ||||||||||||||||||||||||||||
| 23 | isa => 'Int', | ||||||||||||||||||||||||||||
| 24 | default => 0, | ||||||||||||||||||||||||||||
| 25 | documentation => 'Print at most N trees for each tree. Default 0 means unlimited.', | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ## no critic (ProhibitStringyEval) This block needs string evals | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub process_zone { | ||||||||||||||||||||||||||
|
Subroutine
process_zone: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 0 | my ( $self, $zone ) = @_; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Extract variables $bundle, $document ($doc), so they can be used in eval code | ||||||||||||||||||||||||||||
| 34 | 0 | my $bundle = $zone->get_bundle(); | |||||||||||||||||||||||||||
| 35 | 0 | my $document = $bundle->get_document(); | |||||||||||||||||||||||||||
| 36 | 0 | my $doc = $document; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | foreach my $layer (qw(a t n p)) { | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | next if !$zone->has_tree($layer); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | my $tree = $zone->get_tree($layer); | |||||||||||||||||||||||||||
| 41 | 0 | my $this = $tree; | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ( my $code = $self->_args->{"${layer}tree"} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $code =~ s/\$\./\$this->/g if $self->expand_code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ( eval "my \$${layer}tree = \$tree; $code" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | say $tree->get_address(); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 0 | $self->_check_errors($code); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ( my $code = $self->_args->{"${layer}node"} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | my $nodes_found = 0; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | $code =~ s/\$\./\$this->/g if $self->expand_code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | NODE: | ||||||||||||||||||||||||||||
| 53 | 0 | foreach my $node ( $tree->get_descendants() ) { | |||||||||||||||||||||||||||
| 54 | 0 | $this = $node; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( eval "my \$${layer}node = \$node; $code" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | say $node->get_address(); | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ($self->max_nodes_per_tree){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $nodes_found++; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | last NODE if $nodes_found >= $self->max_nodes_per_tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | $self->_check_errors($code); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | return; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub _check_errors { | ||||||||||||||||||||||||||
|
Subroutine
_check_errors: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 70 | 0 | my ($self, $code) = @_; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | return if !$@ || $self->on_error eq 'ignore'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $@ or $self->on_error eq "ignore"
MC/DC0%0/2
not $@ or $self->on_error eq "ignore"
not $@
$self->on_error eq "ignore"
Truth table0%0/3
not(A) or B
A= not $@B= $self->on_error eq "ignore"
| |||||||||||||||||||||||||||||
| 72 | 0 | my $msg = "While evaluating '$code' got error: $@"; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | log_fatal $msg if $self->on_error eq 'fatal'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | log_warn $msg; | |||||||||||||||||||||||||||
| 75 | 0 | return; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | 1; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||||||||