lib/CGI/FormBuilder/Messages.pm

filter: Statementstmt 74.0% 60 / 81 Branchbran 50.0% 21 / 42 Conditioncond 27.2% 6 / 22 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 57.1% 96 / 168 SCAR 45.8
CC
32
Cov
60%
new
77.4
message
19.1
BEGIN
1.0
CRAP 97.5
« lib/CGI/FormBuilder/Multi.pm ↑ Summary lib/CGI/FormBuilder/Source/File.pm »
1  
2 ###########################################################################
3 # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved.
4 # Please visit http://formbuilder.org for tutorials, support, and examples.
5 ###########################################################################
6  
7 package CGI::FormBuilder::Messages;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Messages - Localized message support for FormBuilder
12  
13 =head1 SYNOPSIS
14  
15 use CGI::FormBuilder::Messages;
16  
17 my $mesg = CGI::FormBuilder::Messages->new(
18 $file || \%hash || ':locale'
19 );
20  
21 print $mesg->js_invalid_text;
22  
23 =cut
24  
25 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 10 ▶use CGI::FormBuilder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 10 our $VERSION = '3.10';
32 10 our $AUTOLOAD;
33  
34 sub new {
35 123 ▶ my $self = shift;
Subroutine new: called CC=24 SCAR=43.5
Pod undocumented
36 123 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
01230
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
37 123 my $src = shift;
38 123 ▶ debug 1, "creating Messages object from ", $src || '(default)';
Condition100%2/2
$src || '(default)'
l!l
4119
MC/DC100%1/1
$src || '(default)'
$src
Truth table100%2/2
A
A= $src
Aresult
11
00
39 123 my %hash;
40  
41 123 ▶ if (my $ref = ref $src) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $ref = ref $src) { } 2 121 123
elsif ($src =~ s/^:+//) { } 2 119 121
elsif ($src) { } 0 119 119
42 # hashref, get values directly
43 2 ▶ puke "Argument to 'messages' option must be a \$file, \\\%hash, or ':locale'"
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" or $ref eq "SCALAR" 0 2 2
Condition33%1/3
$ref eq "ARRAY" or $ref eq "SCALAR"
l!l&&r!l&&!r
002
MC/DC0%0/2
$ref eq "ARRAY" or $ref eq "SCALAR"
$ref eq "ARRAY" $ref eq "SCALAR"
Truth table33%1/3
A or B
A= $ref eq "ARRAY"B= $ref eq "SCALAR"
ABresult
1X1
011
000
44 if $ref eq 'ARRAY' || $ref eq 'SCALAR';
45  
46 # load defaults from English
47 # anonymize the %hash or we get fucked with refs later
48 2 require CGI::FormBuilder::Messages::default;
49 2 %hash = CGI::FormBuilder::Messages::default->messages;
50  
51 2 ▶ while(my($k,$v) = each %$src) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$src) 43 2 45
52 43 $hash{$k} = $v; # just override individual messages
53 }
54 } elsif ($src =~ s/^:+//) {
55 # A manual ":locale" specification ("auto" is handled by FB->new)
56 2 ▶ puke "Bad FormBuilder locale specification ':$src'" unless $src && $src ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $src and $src ne "" 0 2 2
Condition33%1/3
$src and $src ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
$src and $src ne ""
$src $src ne ""
Truth table33%1/3
A and B
A= $srcB= $src ne ""
ABresult
0X0
100
111
57  
58 # load defaults from English, in case we can't find translators
59 # as we add new features
60 2 require CGI::FormBuilder::Messages::default;
61 2 %hash = CGI::FormBuilder::Messages::default->messages;
62 2 my %h2 = ();
63  
64 # Note that the $src may be comma-separated, since this is the
65 # way that browsers present it
66 2 for (split /\s*,\s*/, $src) {
67 2 debug 2, "trying to load '$_.pm' for messages";
68 2 my $mod = __PACKAGE__.'::'.$_;
69 2 eval "require $mod";
70 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
71 # try locale's "basename"
72 0 debug 2, "not found; trying locale basename";
73 0 $mod = __PACKAGE__.'::'.substr($_,0,2);
74 0 eval "require $mod";
75 }
76 2 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
77 2 debug 2, "loading messages from $mod";
78 2 %h2 = CGI::FormBuilder::Messages::locale->messages;
79 2 last;
80 }
81 2 ▶ belch "Could not load messages module '$src.pm': $@" unless %h2;
Branch: 1 branch
BranchTrueFalseTotal
unless %h2 0 2 2
82 2 ▶ while (my($k,$v) = each %h2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %h2) 80 2 82
83 80 $hash{$k} = $v;
84 }
85 } elsif ($src) {
86 # filename, just *warn* on missing, and use defaults
87 0 debug 2, "trying to open the '$src' file for messages";
88 0 ▶ if (-f $src && -r _ && open(M, "<$src")) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $src and -r _ and open M, "<$src") 0 0 0
Condition0%0/6
-f $src and -r _ and open M, "<$src"
!ll&&!rl&&r
000
-f $src and -r _
!ll&&!rl&&r
000
MC/DC0%0/3
-f $src and -r _ and open M, "<$src"
-f $src -r _ open M, "<$src"
Truth table0%0/4
A and B and C
A= -f $srcB= -r _C= open M, "<$src"
ABCresult
0XX0
10X0
1100
1111
89 # load defaults from English
90 0 require CGI::FormBuilder::Messages::default;
91 0 %hash = CGI::FormBuilder::Messages::default->messages;
92  
93 0 ▶ while(<M>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline M)) 0 0 0
94 0 ▶ next if /^\s*#/ || /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ or /^\s*$/ 0 0 0
Condition0%0/3
/^\s*#/ or /^\s*$/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^\s*#/ or /^\s*$/
/^\s*#/ /^\s*$/
Truth table0%0/3
A or B
A= /^\s*#/B= /^\s*$/
ABresult
1X1
011
000
95 0 chomp;
96 0 my($k,$v) = split ' ', $_, 2;
97 0 $hash{$k} = $v;
98 }
99 0 close M;
100 }
101 0 ▶ belch "Could not read messages file '$src': $!" unless %hash;
Branch: 1 branch
BranchTrueFalseTotal
unless %hash 0 0 0
102 }
103 # Load default messages if no/invalid source given
104 123 ▶ unless (%hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%hash) 119 4 123
105 119 require CGI::FormBuilder::Messages::default;
106 119 %hash = CGI::FormBuilder::Messages::default->messages;
107 }
108  
109 123 return bless \%hash, $class;
110 }
111  
112 10 *messages = \&message;
113 sub message {
114 662 ▶ my $self = shift;
Subroutine message: called CC=9 SCAR=29.5
Pod undocumented
115 662 my $key = shift;
116 662 ▶ unless ($key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 662 662
117 0 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
118 0 ▶ return wantarray ? %$self : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
119 } else {
120 # requesting a byname dump
121 0 for my $k (sort keys %$self) {
122 0 printf " %-20s\t%s\n", $k, $self->{$k};
123 }
124 0 exit;
125 }
126 }
127 662 ▶ $self->{$key} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 662 662
128 662 ▶ unless (exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 0 662 662
129 0 my @keys = sort keys %$self;
130 0 puke "No message string found for '$key' (keys: @keys)";
131 }
132 662 ▶ if (ref $self->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$key} eq "ARRAY") 1 661 662
133 # hack catch for external file
134 1 $self->{$key} = "@{$self->{$key}}";
135 }
136 662 ▶ return $self->{$key} || '';
Condition50%1/2
$self->{$key} || ''
l!l
6620
MC/DC0%0/1
$self->{$key} || ''
$self->{$key}
Truth table50%1/2
A
A= $self->{$key}
Aresult
11
00
137 }
138  
139 120 ▶sub DESTROY { 1 }
Subroutine DESTROY: called CC=1 SCAR=0.0
140 sub AUTOLOAD {
141 # This allows direct addressing by name, for subclassable usage
142 662 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
143 662 my($name) = $AUTOLOAD =~ /.*::(.+)/;
144 662 return $self->message($name, @_);
145 }
146  
147 10 1;
148 __END__
« lib/CGI/FormBuilder/Multi.pm ↑ Summary lib/CGI/FormBuilder/Source/File.pm »