| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Locale::MakePhrase::Utils; |
|
2
|
|
|
|
|
|
|
our $VERSION = 0.4; |
|
3
|
|
|
|
|
|
|
our $DEBUG = 0; |
|
4
|
|
|
|
|
|
|
our $DIE_FROM_CALLER = 0; |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Locale::MakePhrase::Utils - Collection of useful functions |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
This module implements some useful functions used within the |
|
13
|
|
|
|
|
|
|
L modules. |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
The functions we export: |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
|
20
|
|
|
|
|
|
|
|
|
21
|
10
|
|
|
10
|
|
7119
|
use strict; |
|
|
10
|
|
|
|
|
18
|
|
|
|
10
|
|
|
|
|
390
|
|
|
22
|
10
|
|
|
10
|
|
51
|
use warnings; |
|
|
10
|
|
|
|
|
17
|
|
|
|
10
|
|
|
|
|
270
|
|
|
23
|
10
|
|
|
10
|
|
47
|
use base qw(Exporter); |
|
|
10
|
|
|
|
|
16
|
|
|
|
10
|
|
|
|
|
997
|
|
|
24
|
10
|
|
|
10
|
|
55
|
use vars qw(@EXPORT_OK); |
|
|
10
|
|
|
|
|
14
|
|
|
|
10
|
|
|
|
|
6380
|
|
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
|
27
|
|
|
|
|
|
|
is_number |
|
28
|
|
|
|
|
|
|
left |
|
29
|
|
|
|
|
|
|
right |
|
30
|
|
|
|
|
|
|
alltrim |
|
31
|
|
|
|
|
|
|
die_from_caller |
|
32
|
|
|
|
|
|
|
); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
#-------------------------------------------------------------------------- |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 boolean is_number(value) |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Returns true/false indicating if the value is numeric. |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $is_number_re = qr/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee]-?\d+)?$/; |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub is_number { |
|
45
|
30
|
|
|
30
|
1
|
534
|
my $value = shift; |
|
46
|
30
|
100
|
66
|
|
|
260
|
return 0 if ( !defined $value or !length $value or $value eq "-" ); |
|
|
|
|
66
|
|
|
|
|
|
47
|
29
|
100
|
|
|
|
319
|
return 1 if ( $value =~ $is_number_re ); |
|
48
|
9
|
|
|
|
|
38
|
return 0; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
#-------------------------------------------------------------------------- |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head2 string left(string,length) |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Return the left part of a sub-string. |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub left { |
|
60
|
3
|
|
|
3
|
1
|
26
|
return substr($_[0],0,$_[1]); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
#-------------------------------------------------------------------------- |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 string right(string,length) |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Return the right part of a sub-string. |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub right { |
|
72
|
1
|
|
|
1
|
1
|
6
|
return substr($_[0],-$_[1],$_[1]); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
#-------------------------------------------------------------------------- |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 string alltrim(string) |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Trim all leading and trailing whitespace. |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub alltrim { |
|
84
|
837
|
|
|
837
|
1
|
1318
|
my $value = shift; |
|
85
|
837
|
50
|
|
|
|
1495
|
return undef unless defined $value; |
|
86
|
837
|
|
|
|
|
2595
|
$value =~ s/^\s*//; |
|
87
|
837
|
|
|
|
|
4096
|
$value =~ s/\s*$//; |
|
88
|
837
|
|
|
|
|
2354
|
$value; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
#-------------------------------------------------------------------------- |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 void die_from_caller($message) |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Throw an exception, from a caller's perspective (ie: not from within |
|
96
|
|
|
|
|
|
|
the Locale::MakePhrase modules). This allows us to generate an error |
|
97
|
|
|
|
|
|
|
message for which the user can figure out what they did wrong. |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Note: if you set C to a |
|
100
|
|
|
|
|
|
|
value other than zero, die_from_caller() will recurse that number of |
|
101
|
|
|
|
|
|
|
levels further up the stack backtrace, before die()ing. This allows |
|
102
|
|
|
|
|
|
|
you to wrap your $makePhrase-Etranslate(...) calls in a global |
|
103
|
|
|
|
|
|
|
wrapper function; eg: by setting the value to 1, the message is |
|
104
|
|
|
|
|
|
|
displayed with respect to one level up of your applications' calling |
|
105
|
|
|
|
|
|
|
code. |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=cut |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub die_from_caller { |
|
110
|
4
|
50
|
|
4
|
1
|
12
|
if ($DEBUG) { |
|
111
|
0
|
|
|
|
|
0
|
require Carp; |
|
112
|
0
|
|
|
|
|
0
|
Carp::confess "Locale::MakePhrase detected an error:"; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
4
|
|
|
|
|
7
|
my $caller_count = 0; |
|
115
|
4
|
|
|
|
|
3
|
while (1) { |
|
116
|
4
|
|
|
|
|
6
|
$caller_count++; |
|
117
|
4
|
|
|
|
|
9
|
my $caller = caller($caller_count); |
|
118
|
4
|
50
|
33
|
|
|
22
|
last if (!defined $caller || $caller !~ /^Locale::MakePhrase/); |
|
119
|
|
|
|
|
|
|
} |
|
120
|
4
|
|
|
|
|
31
|
my ($caller,$file,$line) = caller($caller_count); |
|
121
|
4
|
50
|
|
|
|
12
|
if (defined $caller) { |
|
122
|
4
|
|
|
|
|
12
|
for (1..$DIE_FROM_CALLER) { |
|
123
|
4
|
|
|
|
|
4
|
$caller_count++; |
|
124
|
4
|
|
|
|
|
17
|
($caller,$file,$line) = caller($caller_count); |
|
125
|
4
|
50
|
|
|
|
16
|
last unless defined $caller; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
} |
|
128
|
4
|
50
|
|
|
|
9
|
$caller = "main" unless defined $caller; |
|
129
|
4
|
50
|
|
|
|
14
|
$file = "(unknown)" unless defined $file; |
|
130
|
4
|
50
|
|
|
|
8
|
$line = "(unknown)" unless defined $line; |
|
131
|
4
|
|
|
|
|
13
|
my $msg = "Fatal: ". caller() ." detected an error in: $caller\n"; |
|
132
|
4
|
|
|
|
|
9
|
$msg .= "File: $file\n"; |
|
133
|
4
|
|
|
|
|
7
|
$msg .= "Line: $line\n"; |
|
134
|
4
|
50
|
|
|
|
17
|
@_ and $msg .= join (" ", @_) . "\n"; |
|
135
|
4
|
|
|
|
|
25
|
die $msg; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
|
139
|
|
|
|
|
|
|
__END__ |