lib/oEdtk/Dict.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 77.7% 14 / 18 Conditioncond 75.0% 9 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 88.5% 85 / 96 CC 16 SCAR 18.4
CC
16
Cov
90%
new
10.1
substitue
4.2
translate
4.0
CRAP 6.3
« blib/lib/oEdtk/ECM.pm ↑ Summary blib/lib/oEdtk/XML/XSD.pm »
1 package oEdtk::Dict;
2 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 our $VERSION = 2.1131;
8 25 our @ISA = qw(Exporter);
9  
10 # Create a dictionary from an INI file.
11 47 ▶sub new {
Subroutine new: called CC=10 SCAR=23.2
Pod documented
12 47 my ( $class, $path, $options ) = @_;
13  
14 47 ▶ my $invert = $options->{'invert'} || 0;
Condition100%2/2
$options->{'invert'} || 0
l!l
146
MC/DC100%1/1
$options->{'invert'} || 0
$options->{'invert'}
Truth table100%2/2
A
A= $options->{'invert'}
Aresult
11
00
15 47 ▶ my $section = $options->{'section'} || 'DEFAULT';
Condition100%2/2
$options->{'section'} || 'DEFAULT'
l!l
434
MC/DC100%1/1
$options->{'section'} || 'DEFAULT'
$options->{'section'}
Truth table100%2/2
A
A= $options->{'section'}
Aresult
11
00
16 47 ▶ my $ignore_case = $options->{'ignore_case'} || 1;
Condition50%1/2
$options->{'ignore_case'} || 1
l!l
047
MC/DC0%0/1
$options->{'ignore_case'} || 1
$options->{'ignore_case'}
Truth table50%1/2
A
A= $options->{'ignore_case'}
Aresult
11
00
17  
18 # warn "INFO: Path EDTK_DICO >$path< \n";
19 47 tie my %ini, 'Config::IniFiles', ( -file => $path, -default => $section );
20  
21 47 my $dico = $ini{$section};
22 47 ▶ if ($invert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invert) 1 46 47
23  
24 # Invert the hash.
25 1 my %tr = ();
26 1 ▶ while ( my ( $key, $val ) = each %$dico ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$dico) 2 1 3
27 2 ▶ if ( ref($val) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 2 2
28 0 $tr{$_} = $key foreach @$val;
29 }
30 else {
31 2 $tr{$val} = $key;
32 }
33 }
34 1 $dico = \%tr;
35 }
36 47 ▶ if ($ignore_case) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore_case) 47 0 47
37 47 my $dico2 = {};
38 47 ▶ while ( my ( $key, $val ) = each %$dico ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$dico) 7 47 54
39 7 $dico2->{ lc($key) } = $val;
40 }
41 47 $dico = $dico2;
42 }
43 47 my $self = {
44 ignore_case => $ignore_case,
45 dico => $dico
46 };
47  
48 # warn "INFO: Dict object $class $self ($section $path) created...\n";
49 47 return bless $self, $class;
50 }
51  
52 7 ▶sub translate {
Subroutine translate: called CC=4 SCAR=13.9
Pod documented
53  
54 # pass the 'check' option to request a check that the value is present in the dictionary
55 # if the value is absent from the dictionary, returns 'undef'
56 7 my ( $self, $word, $check ) = @_;
57  
58 7 my $key = $word;
59 7 ▶ if ( $self->{'ignore_case'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ignore_case"}) 7 0 7
60 7 $key = lc($key);
61 }
62 7 my $val = $self->{'dico'}->{$key};
63 7 ▶ if ( !$check && !defined($val) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($check or defined $val) 1 6 7
Condition100%3/3
$check or defined $val
l!l&&r!l&&!r
151
MC/DC100%2/2
$check or defined $val
$check defined $val
Truth table100%3/3
A or B
A= $checkB= defined $val
ABresult
1X1
011
000
64 1 $val = $word;
65 }
66  
67 7 return $val;
68 }
69  
70 1 ▶sub substitue {
Subroutine substitue: called CC=4 SCAR=14.3
Pod documented
71 1 my ( $self, $var ) = @_;
72  
73 # warn "DEBUG: Dict::substitute value = '$var'\n";
74  
75 1 ▶ if ( defined($var) && length($var) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var and length $var > 0) 1 0 1
Condition33%1/3
defined $var and length $var > 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $var and length $var > 0
defined $var length $var > 0
Truth table33%1/3
A and B
A= defined $varB= length $var > 0
ABresult
0X0
100
111
76 1 ▶ while ( my ( $key, $val ) = each %{ $self->{'dico'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"dico"};}) 1 1 2
77 1 $var =~ s/$key/$val/ig;
78 }
79  
80 # warn "DEBUG: Dict::substitute value = '$var'\n";
81 }
82  
83 1 return $var;
84 }
85  
86 25 1;
87  
88 =head1 NAME
89  
90 oEdtk::Dict - INI-backed translation dictionary
91  
92 =head1 SYNOPSIS
93  
94 use oEdtk::Dict;
95  
96 my $dict = oEdtk::Dict->new('countries.ini', { section => 'COUNTRIES' });
97 my $label = $dict->translate('FR');
98 my $text = $dict->substitue('Payable a FR');
99  
100 =head1 DESCRIPTION
101  
102 Loads a dictionary from one section of an INI file and offers word lookup and
103 global substitution. It backs the corporate-entity, character translation and
104 country dictionaries used across the toolkit.
105  
106 =head1 METHODS
107  
108 =head2 new
109  
110 my $dict = oEdtk::Dict->new($path, $options);
111  
112 Read the C<$path> INI file and build the dictionary. C<$options> accepts:
113 C<invert> (swap keys and values, default off), C<section> (INI section to read,
114 default C<DEFAULT>) and C<ignore_case> (fold keys to lowercase, default on).
115  
116 =head2 translate
117  
118 my $value = $dict->translate($word, $check);
119  
120 Look up C<$word> and return its translation. When C<$check> is true an unknown
121 word yields C<undef>; otherwise the word is returned unchanged.
122  
123 =head2 substitue
124  
125 my $text = $dict->substitue($var);
126  
127 Case-insensitively replace every dictionary entry found in C<$var> and return
128 the result. (The name keeps the historical spelling.)
129  
130 =head1 AUTHOR
131  
132 The oEdtk team.
133  
134 =cut
« blib/lib/oEdtk/ECM.pm ↑ Summary blib/lib/oEdtk/XML/XSD.pm »