line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Common::Routine; |
2
|
3
|
|
|
3
|
|
25671
|
use Modern::Perl; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
18
|
|
3
|
|
|
|
|
|
|
#use Carp; |
4
|
3
|
|
|
3
|
|
334
|
use Exporter; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
103
|
|
5
|
|
|
|
|
|
|
#use Data::Dumper; |
6
|
3
|
|
|
3
|
|
1269
|
use POSIX qw/ceil floor/; |
|
3
|
|
|
|
|
14163
|
|
|
3
|
|
|
|
|
16
|
|
7
|
3
|
|
|
3
|
|
4109
|
use Number::Format qw/format_number/; |
|
3
|
|
|
|
|
10482
|
|
|
3
|
|
|
|
|
2328
|
|
8
|
|
|
|
|
|
|
#use Math::Round; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = ("Exporter"); |
11
|
|
|
|
|
|
|
our @EXPORT = (); |
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw/max min sum mean median var sd |
13
|
|
|
|
|
|
|
trim ltrim rtrim ceil floor round format_number/; |
14
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
15
|
|
|
|
|
|
|
math => [qw/max min sum mean median var sd/], |
16
|
|
|
|
|
|
|
str => [qw/trim ltrim rtrim/], |
17
|
|
|
|
|
|
|
num => [qw/ceil floor round format_number/], |
18
|
|
|
|
|
|
|
all => [qw/max min sum mean median var sd trim ltrim rtrim ceil floor round format_number/] |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '0.0.5'; # VERSION |
22
|
|
|
|
|
|
|
# ABSTRACT: Provide and synthesize very commonly used routines that are not provided in perl's build-in routines. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub round { |
27
|
1
|
50
|
|
1
|
1
|
3
|
return unless @_; |
28
|
1
|
50
|
|
|
|
3
|
my $decimal = @_ == 1 ? 0 : pop; |
29
|
1
|
|
|
|
|
5
|
return Number::Format::round($_[0], $decimal); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub max { |
34
|
1
|
50
|
|
1
|
1
|
4684
|
return unless @_; |
35
|
1
|
50
|
|
|
|
5
|
my @elements = ref $_[0] ? @{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
36
|
1
|
|
|
|
|
2
|
my $max = $elements[0]; |
37
|
1
|
100
|
|
|
|
1
|
map { $max = $_ if ($max < $_) } @elements; |
|
10
|
|
|
|
|
23
|
|
38
|
1
|
|
|
|
|
4
|
return $max; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub min { |
43
|
1
|
50
|
|
1
|
1
|
5
|
return unless @_; |
44
|
1
|
50
|
|
|
|
5
|
my @elements = ref $_[0] ? @{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
45
|
1
|
|
|
|
|
1
|
my $min = $elements[0]; |
46
|
1
|
50
|
|
|
|
2
|
map { $min = $_ if ($_ < $min) } @elements; |
|
10
|
|
|
|
|
15
|
|
47
|
1
|
|
|
|
|
3
|
return $min; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub sum { |
52
|
4
|
50
|
|
4
|
1
|
7
|
return unless @_; |
53
|
4
|
50
|
|
|
|
9
|
my @elements = ref $_[0] ? @{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
54
|
4
|
|
|
|
|
4
|
my $sum; |
55
|
4
|
|
|
|
|
32
|
$sum += $_ for @elements; |
56
|
4
|
|
|
|
|
7
|
return $sum; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub mean { |
61
|
3
|
50
|
|
3
|
1
|
8
|
return unless @_; |
62
|
3
|
50
|
|
|
|
7
|
my @elements = ref $_[0] ? @{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
63
|
3
|
|
|
|
|
7
|
my $sum = &sum(@elements); |
64
|
3
|
|
|
|
|
10
|
return $sum / @elements; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub median { |
69
|
1
|
50
|
|
1
|
1
|
3
|
return unless @_; |
70
|
1
|
50
|
|
|
|
3
|
my @elements = ref $_[0] ? @{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
71
|
1
|
|
|
|
|
4
|
@elements = sort { $a <=> $b } @elements; |
|
19
|
|
|
|
|
13
|
|
72
|
1
|
|
|
|
|
1
|
my $len = @elements; |
73
|
1
|
|
|
|
|
4
|
my $mid = int($len /2 ); |
74
|
1
|
50
|
|
|
|
6
|
return $len % 2 ? $elements[$mid] : ($elements[$mid-1] + $elements[$mid]) / 2; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub var { |
79
|
2
|
50
|
|
2
|
1
|
7
|
return unless @_; |
80
|
2
|
50
|
|
|
|
7
|
my @elements = ref $_[0] ? @{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
81
|
2
|
|
|
|
|
4
|
my $mean = &mean(@elements); |
82
|
2
|
|
|
|
|
3
|
my $sum = 0; |
83
|
2
|
|
|
|
|
18
|
$sum += ($_ - $mean) ** 2 for @elements; |
84
|
2
|
|
|
|
|
31
|
return $sum / $#elements; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub sd { |
89
|
1
|
50
|
|
1
|
1
|
15
|
return unless @_; |
90
|
1
|
50
|
|
|
|
4
|
my @elements = ref $_[0] ? @{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
91
|
1
|
|
|
|
|
3
|
return (&var(@elements)) ** 0.5; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub trim { |
96
|
1
|
50
|
|
1
|
1
|
3
|
return unless @_; |
97
|
1
|
|
|
|
|
2
|
my $str = pop; |
98
|
1
|
|
|
|
|
5
|
$str =~s/^\s+|\s+$//g; |
99
|
1
|
|
|
|
|
4
|
return $str; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub ltrim { |
104
|
1
|
50
|
|
1
|
1
|
4
|
return unless @_; |
105
|
1
|
|
|
|
|
1
|
my $str = pop; |
106
|
1
|
|
|
|
|
5
|
$str =~s/^\s+//g; |
107
|
1
|
|
|
|
|
3
|
return $str; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub rtrim { |
112
|
1
|
50
|
|
1
|
1
|
3
|
return unless @_; |
113
|
1
|
|
|
|
|
2
|
my $str = pop; |
114
|
1
|
|
|
|
|
5
|
$str =~s/\s+$//g; |
115
|
1
|
|
|
|
|
3
|
return $str; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
__END__ |