line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hey::Common; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=cut |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Hey::Common - Common functions used in other Hey::* modules |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSIS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Hey::Common; |
14
|
|
|
|
|
|
|
my $common = Hey::Common->new; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $money = $common->formatMoney(524.4); # will return string "524.40" |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 DESCRIPTION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head2 new |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $common = Hey::Common->new; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
This function provides access to all of these following methods. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub new { |
29
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
30
|
0
|
|
|
|
|
|
my %param = @_; |
31
|
0
|
|
|
|
|
|
my $self = bless({}, $class); |
32
|
0
|
|
|
|
|
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=cut |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head2 forceArray |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$data->{users} = $common->forceArray($data->{users}); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
The input can either be an array ref or non-array ref. The output will either be |
42
|
|
|
|
|
|
|
that same array ref, or the non-array ref as the only item in an array as a ref. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
This is useful for items that might or might not be an array ref, but you are |
45
|
|
|
|
|
|
|
expecting an array ref. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub forceArray { |
50
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
51
|
0
|
|
|
|
|
|
my $in = shift; |
52
|
0
|
0
|
|
|
|
|
if (ref($in) eq "ARRAY") { |
53
|
0
|
|
|
|
|
|
return $in; |
54
|
|
|
|
|
|
|
} |
55
|
0
|
|
|
|
|
|
my $out; |
56
|
0
|
|
|
|
|
|
push(@{$out}, $in); |
|
0
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
return $out; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head2 randomCode |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
$someRandomCode = $common->randomCode($lengthOfCodeRequested, $keyStringOfPermittedCharacters); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$someRandomCode = $common->randomCode(); # defaults for length and key |
67
|
|
|
|
|
|
|
$someRandomCode = $common->randomCode(8); # choose a specific length, but default key |
68
|
|
|
|
|
|
|
$someRandomCode = $common->randomCode(12, 'abcdefg'); # choose a specific length and key |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
$lengthOfCodeRequested defaults to 16. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
$keyStringOfPermittedCharacters defaults to 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub randomCode { |
77
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
78
|
0
|
|
0
|
|
|
|
my $length = shift || 16; |
79
|
0
|
|
0
|
|
|
|
my $key = shift || qq(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789); |
80
|
0
|
|
|
|
|
|
my $value = ""; |
81
|
0
|
|
|
|
|
|
while (length($value) < $length) { |
82
|
0
|
|
|
|
|
|
$value .= substr($key, int(rand() * length($key)), 1); |
83
|
|
|
|
|
|
|
} |
84
|
0
|
|
|
|
|
|
return $value; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 deepCopy |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $newCopyOfSomeHashRef = $common->deepCopy($someHashRef); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
It makes a copy of a reference instead of making a reference to it. There's some usefulness there. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub deepCopy { # I don't know where this function came from, but it works nicely and has for years and years. Source unknown. |
98
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
99
|
0
|
|
|
|
|
|
my $this = shift; |
100
|
0
|
0
|
|
|
|
|
if (not ref($this)) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
$this; |
102
|
|
|
|
|
|
|
} elsif (ref($this) eq "ARRAY") { |
103
|
0
|
|
|
|
|
|
[map $self->deepCopy($_), @{$this}]; |
|
0
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
} elsif (ref($this) eq "HASH") { |
105
|
0
|
|
|
|
|
|
+{map { $_ => $self->deepCopy($this->{$_}) } keys(%{$this})}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 isAffirmative |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
if ($common->isAffirmative('y')) { |
114
|
|
|
|
|
|
|
print "'y' is affirmative, so you'll see this."; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
if ($common->isAffirmative('no')) { |
118
|
|
|
|
|
|
|
print "'no' is not affirmative, so you won't see this"; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
This checks to see if the value is affirmative. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Things that are affirmative are: 'y', 'yes', 't', 'true', or any true numerical value. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub isAffirmative { |
128
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
129
|
0
|
|
0
|
|
|
|
my $in = lc(shift) || return undef; |
130
|
0
|
|
|
|
|
|
$in =~ s|\s+||g; |
131
|
0
|
0
|
|
|
|
|
if ($in =~ m|^t(r(u(e)?)?)?$|) { |
132
|
0
|
|
|
|
|
|
return true; |
133
|
|
|
|
|
|
|
} |
134
|
0
|
0
|
|
|
|
|
if ($in =~ m|^y(e(s)?)?$|) { |
135
|
0
|
|
|
|
|
|
return true; |
136
|
|
|
|
|
|
|
} |
137
|
0
|
0
|
0
|
|
|
|
if ($in =~ m|^\d+$| && $in > 0) { |
138
|
0
|
|
|
|
|
|
return true; |
139
|
|
|
|
|
|
|
} |
140
|
0
|
|
|
|
|
|
return undef; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=cut |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 isNegative |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
if ($common->isNegative('y')) { |
148
|
|
|
|
|
|
|
print "'y' is not negative, so you won't see this."; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
if ($common->isNegative('no')) { |
152
|
|
|
|
|
|
|
print "'no' is negative, so you'll see this"; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
This checks to see if the value is negative. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Things that are negative are: 'n', 'no', 'f', 'false', any false numerical value (zero), or undef/null. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub isNegative { |
162
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
163
|
0
|
|
0
|
|
|
|
my $in = lc(shift) || return true; |
164
|
0
|
|
|
|
|
|
$in =~ s|\s+||g; |
165
|
0
|
0
|
|
|
|
|
if ($in =~ m|^f(a(l(s(e)?)?)?)?$|) { |
166
|
0
|
|
|
|
|
|
return true; |
167
|
|
|
|
|
|
|
} |
168
|
0
|
0
|
|
|
|
|
if ($in =~ m|^n(o)?$|) { |
169
|
0
|
|
|
|
|
|
return true; |
170
|
|
|
|
|
|
|
} |
171
|
0
|
0
|
0
|
|
|
|
if ($in =~ m|^\d+$| && $in <= 0) { |
172
|
0
|
|
|
|
|
|
return true; |
173
|
|
|
|
|
|
|
} |
174
|
0
|
0
|
|
|
|
|
if ($in =~ m|^$|) { |
175
|
0
|
|
|
|
|
|
return true; |
176
|
|
|
|
|
|
|
} |
177
|
0
|
|
|
|
|
|
return undef; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=cut |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 smtpClient |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
my @aListOfRecipientEmailAddresses = ('george@somewhere.com', 'ed@server.com', 'ralph@elsewhere.com'); |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
my $contentOfEmailIncludingHeader = <
|
187
|
|
|
|
|
|
|
From: fred@someplace.com |
188
|
|
|
|
|
|
|
To: fred@someplace.com |
189
|
|
|
|
|
|
|
Subject: The email subject |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
This is the email body area. Fill it full of useful email content. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Thanks, |
194
|
|
|
|
|
|
|
Fred |
195
|
|
|
|
|
|
|
Someplace Inc. |
196
|
|
|
|
|
|
|
CONTENT |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
$common->smtpClient({ Host => 'smtp.server.someplace.com', |
199
|
|
|
|
|
|
|
From => 'fred@someplace.com', |
200
|
|
|
|
|
|
|
To => \@aListOfRecipientEmailAddresses, |
201
|
|
|
|
|
|
|
Content => $contentOfEmailIncludingHeader }); |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
'Host' is optional and defaults to 'localhost'. Of course, you would need to be able to send email through whatever host you specify. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
'From' is a single email address that is used as the envelope address. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
'To' can be a single email address or a list of email addresses as a scalar or an array ref. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
'Content' is the content of the email, with header and body included. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=cut |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub smtpClient { |
214
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
215
|
0
|
|
|
|
|
|
my %param = @_; |
216
|
0
|
|
0
|
|
|
|
my $host = $param{host} || $param{Host} || "localhost"; |
217
|
0
|
|
0
|
|
|
|
my $from = $param{from} || $param{From}; |
218
|
0
|
|
0
|
|
|
|
my $content = $param{content} || $param{Content}; |
219
|
0
|
|
0
|
|
|
|
my $to = $self->forceArray($param{to} || $param{To}); |
220
|
1
|
|
|
1
|
|
31840
|
use Net::SMTP; |
|
1
|
|
|
|
|
62696
|
|
|
1
|
|
|
|
|
314
|
|
221
|
0
|
|
|
|
|
|
my $smtp = Net::SMTP->new($host); |
222
|
0
|
|
|
|
|
|
$smtp->mail($from); |
223
|
0
|
|
|
|
|
|
foreach my $ito (@{$to}) { |
|
0
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
$smtp->to($ito); |
225
|
|
|
|
|
|
|
} |
226
|
0
|
|
|
|
|
|
$smtp->data(); |
227
|
0
|
|
|
|
|
|
$smtp->datasend($content); |
228
|
0
|
|
|
|
|
|
$smtp->dataend(); |
229
|
0
|
|
|
|
|
|
$smtp->quit(); |
230
|
0
|
|
|
|
|
|
return; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=cut |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head2 formatMoney |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
my $money = 515.3; |
238
|
|
|
|
|
|
|
$money = $common->formatMoney($money); |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
$money is the non-formatted money amount. It will be returned as a formatted string, but with no currency symbol. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=cut |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
sub formatMoney { # probably could use some work, but it works. |
245
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
246
|
0
|
|
0
|
|
|
|
my $in = shift || 0; |
247
|
0
|
|
|
|
|
|
$in = $in * 100; |
248
|
0
|
|
|
|
|
|
$in = int($in.".00"); |
249
|
0
|
|
|
|
|
|
$in = $in / 100; |
250
|
0
|
|
|
|
|
|
$in = "$in"; |
251
|
0
|
0
|
|
|
|
|
unless ($in =~ m|\.|) { |
252
|
0
|
|
|
|
|
|
$in .= "."; |
253
|
|
|
|
|
|
|
} |
254
|
0
|
|
|
|
|
|
until ($in =~ m|\.\d{2}|) { |
255
|
0
|
|
|
|
|
|
$in .= "0"; |
256
|
|
|
|
|
|
|
} |
257
|
0
|
|
|
|
|
|
return $in; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=cut |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head2 sha1 |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
my $something = 'This is something that will be hashed.'; |
265
|
|
|
|
|
|
|
my $sha1Hash = $common->sha1($something); |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
$something is any value that you want hashed. It can be a binary value or a simple scalar. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
$sha1Hash is a simple sha1 hex of whatever you passed in. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=cut |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sub sha1 { |
274
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
275
|
0
|
|
|
|
|
|
my $input = shift; |
276
|
1
|
|
|
1
|
|
1353
|
use Digest::SHA1 'sha1_hex'; |
|
1
|
|
|
|
|
1098
|
|
|
1
|
|
|
|
|
123
|
|
277
|
0
|
|
|
|
|
|
return sha1_hex($input); |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=cut |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head1 AUTHOR |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
Dusty Wilson Emodule-Hey-Common@dusty.hey.nuE |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
Copyright (C) 2006 by Dusty Wilson, hey.nu Network Community Services |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
291
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
292
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=cut |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
1; |