| 1 | package oEdtk::Dict; | ||||||||||||||||||||||||||||
| 2 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 24 | ▶ | use Config::IniFiles; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 24 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 24 | our $VERSION = 2.1126; | |||||||||||||||||||||||||||
| 8 | 24 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # Create a dictionary from an INI file. | ||||||||||||||||||||||||||||
| 11 | 25 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 25 | my ( $class, $path, $options ) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 25 | ▶ | my $invert = $options->{'invert'} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'invert'} || 0
MC/DC100%1/1
$options->{'invert'} || 0
$options->{'invert'}
Truth table100%2/2
A
A= $options->{'invert'}
| |||||||||||||||||||||||||||||
| 15 | 25 | ▶ | my $section = $options->{'section'} || 'DEFAULT'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'section'} || 'DEFAULT'
MC/DC100%1/1
$options->{'section'} || 'DEFAULT'
$options->{'section'}
Truth table100%2/2
A
A= $options->{'section'}
| |||||||||||||||||||||||||||||
| 16 | 25 | ▶ | my $ignore_case = $options->{'ignore_case'} || 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'ignore_case'} || 1
MC/DC0%0/1
$options->{'ignore_case'} || 1
$options->{'ignore_case'}
Truth table50%1/2
A
A= $options->{'ignore_case'}
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # warn "INFO: Path EDTK_DICO >$path< \n"; | ||||||||||||||||||||||||||||
| 19 | 25 | tie my %ini, 'Config::IniFiles', ( -file => $path, -default => $section ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 25 | my $dico = $ini{$section}; | |||||||||||||||||||||||||||
| 22 | 25 | ▶ | if ($invert) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Invert the hash. | ||||||||||||||||||||||||||||
| 25 | 1 | my %tr = (); | |||||||||||||||||||||||||||
| 26 | 1 | ▶ | while ( my ( $key, $val ) = each %$dico ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | if ( ref($val) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | $tr{$_} = $key foreach @$val; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | else { | ||||||||||||||||||||||||||||
| 31 | 2 | $tr{$val} = $key; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 1 | $dico = \%tr; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 25 | ▶ | if ($ignore_case) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 25 | my $dico2 = {}; | |||||||||||||||||||||||||||
| 38 | 25 | ▶ | while ( my ( $key, $val ) = each %$dico ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 7 | $dico2->{ lc($key) } = $val; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 25 | $dico = $dico2; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 25 | my $self = { | |||||||||||||||||||||||||||
| 44 | ignore_case => $ignore_case, | ||||||||||||||||||||||||||||
| 45 | dico => $dico | ||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # warn "INFO: Dict object $class $self ($section $path) created...\n"; | ||||||||||||||||||||||||||||
| 49 | 25 | 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
| |||||||||||||||||||||||||||||
| 60 | 7 | $key = lc($key); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 7 | my $val = $self->{'dico'}->{$key}; | |||||||||||||||||||||||||||
| 63 | 7 | ▶ | if ( !$check && !defined($val) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$check or defined $val
MC/DC100%2/2
$check or defined $val
$check
defined $val
Truth table100%3/3
A or B
A= $checkB= defined $val
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $var and length $var > 0
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
| |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | while ( my ( $key, $val ) = each %{ $self->{'dico'} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 24 | 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 | ||||||||||||||||||||||||||||