| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Config-Model | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2005-2022 by Dominique Dumont. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | package Config::Model::Role::ComputeFunction 2.169; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # ABSTRACT: compute &index or &element functions | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 59 | ▶ | use Mouse::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 59 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 59 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 59 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 59 | ▶ | use Mouse::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 59 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 59 | my $logger = get_logger("ComputeFunction"); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 164 | ▶ | sub compute_string { | ||||||||||||||||||||||||||
|
Subroutine
compute_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 164 | my ($self, $string, $check) = @_; | |||||||||||||||||||||||||||
| 26 | 164 | $string =~ s/&(index|element)(?:\(([- \d])\))?/$self->eval_function($1,$2,$check)/eg; | |||||||||||||||||||||||||||
| 27 | 164 | return $string; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 257 | ▶ | sub eval_function { | ||||||||||||||||||||||||||
|
Subroutine
eval_function: called CC=10 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 257 | my ($self, $function, $up, $check) = @_; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 257 | ▶ | if (defined $up) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | # get now the object referred | ||||||||||||||||||||||||||||
| 35 | 176 | $up =~ s/\s//g; | |||||||||||||||||||||||||||
| 36 | 176 | $up =~ s/-(\d+)/'- ' x $1/e; # change -3 -> - - - | |||||||||||||||||||||||||||
| 37 | 176 | $up =~ s/(-+)/'- ' x length($1)/e; # change --- -> - - - | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 257 | my $target = eval { | |||||||||||||||||||||||||||
| 41 | 257 | ▶ | defined $up ? $self->grab( step => $up, check => $check ) : $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 257 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | my $e = $@; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $msg = ref($e) && $e->can('full_message') ? $e->full_message : $e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $e and $e->can("full_message")
MC/DC0%0/2
ref $e and $e->can("full_message")
ref $e
$e->can("full_message")
Truth table0%0/3
A and B
A= ref $eB= $e->can("full_message")
| |||||||||||||||||||||||||||||
| 47 | 0 | Config::Model::Exception::Model->throw( | |||||||||||||||||||||||||||
| 48 | object => $self, | ||||||||||||||||||||||||||||
| 49 | error => "Compute function argument '$up':\n" . $msg | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 257 | my $result ; | |||||||||||||||||||||||||||
| 54 | 257 | ▶ | if ( $function eq 'element' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 55 | 166 | $result = $target->element_name; | |||||||||||||||||||||||||||
| 56 | 166 | ▶ | Config::Model::Exception::Model->throw( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | object => $self, | ||||||||||||||||||||||||||||
| 58 | error => "Compute function error: '". $target->name. "' has no element name" | ||||||||||||||||||||||||||||
| 59 | ) unless defined $result; | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | elsif ( $function eq 'index' ) { | ||||||||||||||||||||||||||||
| 62 | 91 | $result = $target->index_value; | |||||||||||||||||||||||||||
| 63 | 91 | ▶ | Config::Model::Exception::Model->throw( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | object => $self, | ||||||||||||||||||||||||||||
| 65 | error => "Compute function error: '". $target->name. "' has no index value" | ||||||||||||||||||||||||||||
| 66 | ) unless defined $result; | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 0 | Config::Model::Exception::Model->throw( | |||||||||||||||||||||||||||
| 70 | object => $self, | ||||||||||||||||||||||||||||
| 71 | error => "Unknown compute function &$function, " | ||||||||||||||||||||||||||||
| 72 | . "expected &element(...) or &index(...)" | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 257 | return $result; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 59 | 1; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||||||||||