Labyrinth/Phrasebook.pm

filter: Statementstmt 47.7% 21 / 44 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 43.4% 30 / 69 CC 7 SCAR 21.4
CC
7
Cov
38%
new
20.0
get
12.0
load
6.0
CRAP 8.5
« Labyrinth/DTUtils.pm ↑ Summary Labyrinth/Audit.pm »
1 package Labyrinth::Phrasebook;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 $VERSION = '5.32';
8  
9 =head1 NAME
10  
11 Labyrinth::Phrasebook - Phrasebook Manager for Labyrinth
12  
13 =head1 SYNOPSIS
14  
15 use Labyrinth::Phrasebook;
16  
17 my $pb = Labyrinth::Phrasebook->new($phrasebook);
18 $pb->load($dictionary);
19 my $result = $pb->get($entry);
20  
21 =head1 DESCRIPTION
22  
23 Using L<Data::Phrasebook>, this package acts as a simple wrapper for the
24 Labyrinth framework.
25  
26 =cut
27  
28 # -------------------------------------
29 # Library Modules
30  
31 12 ▶use Data::Phrasebook;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 12 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 12 ▶use Labyrinth::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # -------------------------------------
36 # Variables
37  
38 12 my %pbcache;
39  
40 # -------------------------------------
41 # The Public Interface Subs
42  
43 =head1 FUNCTIONS
44  
45 =head2 Constructor
46  
47 =over 4
48  
49 =item new()
50  
51 Create a new Phrasebook object.
52  
53 =back
54  
55 =cut
56  
57 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
58 0 my ($self, $phrasebook, $dict) = @_;
59  
60 0 ▶ Croak("Cannot read configuration file [$phrasebook]\n") unless(-r $phrasebook);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $phrasebook 0 0 0
61  
62 0 my $pb = Data::Phrasebook->new(
63 class => 'Plain',
64 loader => 'Ini',
65 file => $phrasebook
66 );
67  
68 0 ▶ Croak("Cannot access configuration file [$phrasebook]\n") unless($pb);
Branch: 1 branch
BranchTrueFalseTotal
unless $pb 0 0 0
69  
70 # set dictionary if not using default
71 0 ▶ $pb->dict($dict) if($dict);
Branch: 1 branch
BranchTrueFalseTotal
if $dict 0 0 0
72  
73 # set parameter pattern
74 0 $pb->delimiters( qr{ \$(\w+) }x, 1 );
75  
76 # create an attributes hash
77 0 my $atts = { 'pb' => $pb };
78  
79 # create the object
80 0 bless $atts, $self;
81 0 return $atts;
82 }
83  
84 =head2 Methods
85  
86 =over 4
87  
88 =item load
89  
90 Reset primary dictionary.
91  
92 =cut
93  
94 0 ▶sub load {
Subroutine load: not called CC=2 SCAR=17.9
Pod documented
95 0 my ($self, $section) = @_;
96 0 ▶ $self->{pb}->dict( $section ) if($section);
Branch: 1 branch
BranchTrueFalseTotal
if $section 0 0 0
97 0 return $self->{pb}->dict;
98 }
99  
100 =item get
101  
102 Gets an entry from the current section or from the default section if
103 entry doesn't exist in the current section.
104  
105 =cut
106  
107 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
108 0 my ($self, $key, $hash) = @_;
109 0 my $crypt = join('', map { "$_=$hash->{$_}" } grep {$hash->{$_}} keys %$hash);
110  
111 0 ▶ return $pbcache{$key}{$crypt} if($pbcache{$key}{$crypt});
Branch: 1 branch
BranchTrueFalseTotal
if $pbcache{$key}{$crypt} 0 0 0
112  
113 0 my $val = $self->{pb}->fetch( $key, $hash );
114 0 ▶ Croak("Unknown key phrase [$key]\n") unless($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
115 0 $val =~ s/\n|\t/ /sg;
116  
117 # # parse any given parameters
118 # if($hash) {
119 # for my $name (keys %$hash) {
120 # LogDebug("get: name=[$name], value=[$hash->{$name}]");
121 # $hash->{$name} ||= '';
122 # $val =~ s/\$$name/$hash->{$name}/g;
123 # }
124 # }
125  
126 # $val =~ s/\$\w+//g; # remove unparsed parameters
127 0 $pbcache{$key}{$crypt} = $val;
128 0 return $val;
129 }
130  
131 0 ▶sub DESTROY {1}
Subroutine DESTROY: not called CC=1 SCAR=6.9
132  
133 12 1;
134  
135 __END__
« Labyrinth/DTUtils.pm ↑ Summary Labyrinth/Audit.pm »