line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hub::Perl::Compare; |
2
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
4
|
use Hub qw(:lib); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
5
|
|
|
|
|
|
|
our $VERSION = '4.00043'; |
6
|
|
|
|
|
|
|
our @EXPORT = qw(); |
7
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
8
|
|
|
|
|
|
|
compare |
9
|
|
|
|
|
|
|
sort_compare |
10
|
|
|
|
|
|
|
is_bipolar |
11
|
|
|
|
|
|
|
); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
14
|
|
|
|
|
|
|
# %COMPARISIONS - Comparison routines |
15
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our %COMPARISONS = ( |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
'eq' => sub { $_[0] eq $_[1]; }, |
20
|
|
|
|
|
|
|
'ne' => sub { $_[0] ne $_[1]; }, |
21
|
|
|
|
|
|
|
'lt' => sub { $_[0] lt $_[1]; }, |
22
|
|
|
|
|
|
|
'le' => sub { $_[0] le $_[1]; }, |
23
|
|
|
|
|
|
|
'gt' => sub { $_[0] gt $_[1]; }, |
24
|
|
|
|
|
|
|
'ge' => sub { $_[0] ge $_[1]; }, |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
'=~' => sub { $_[0] =~ $_[1]; }, |
27
|
|
|
|
|
|
|
'!~' => sub { $_[0] !~ $_[1]; }, |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
'==' => sub { $_[0] == $_[1]; }, |
30
|
|
|
|
|
|
|
'!=' => sub { $_[0] != $_[1]; }, |
31
|
|
|
|
|
|
|
'<' => sub { $_[0] < $_[1]; }, |
32
|
|
|
|
|
|
|
'>' => sub { $_[0] > $_[1]; }, |
33
|
|
|
|
|
|
|
'<=' => sub { $_[0] <= $_[1]; }, |
34
|
|
|
|
|
|
|
'>=' => sub { $_[0] >= $_[1]; }, |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
'<=>' => sub { $_[0] <=> $_[1]; }, |
37
|
|
|
|
|
|
|
'cmp' => sub { $_[0] cmp $_[1]; }, |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Extensions (above and beyond perl operators) |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
'eqic' => sub { lc($_[0]) eq lc($_[1]); }, |
42
|
|
|
|
|
|
|
'neic' => sub { lc($_[0]) ne lc($_[1]); }, |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Multiple of |
45
|
|
|
|
|
|
|
'mult-of' => sub { ($_[0] >= $_[1]) && ($_[0] % $_[1] == 0); }, |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
50
|
|
|
|
|
|
|
# compare - Wrapper for Perl's internal comparison operators. |
51
|
|
|
|
|
|
|
# compare $operator, $left_operand, $right_operand |
52
|
|
|
|
|
|
|
# |
53
|
|
|
|
|
|
|
# Support runtime comparison when the operator is held as a scalar. |
54
|
|
|
|
|
|
|
# |
55
|
|
|
|
|
|
|
# Where $operator may be one of: |
56
|
|
|
|
|
|
|
# |
57
|
|
|
|
|
|
|
# eq Equal |
58
|
|
|
|
|
|
|
# ne Not equal |
59
|
|
|
|
|
|
|
# lt Less than |
60
|
|
|
|
|
|
|
# le Less than or equal |
61
|
|
|
|
|
|
|
# gt Greater than |
62
|
|
|
|
|
|
|
# ge Greater than or equal |
63
|
|
|
|
|
|
|
# == Numeric equal |
64
|
|
|
|
|
|
|
# != Numeric not equal |
65
|
|
|
|
|
|
|
# < Numeric less than |
66
|
|
|
|
|
|
|
# > Numeric greater than |
67
|
|
|
|
|
|
|
# <= Numeric less than or equal |
68
|
|
|
|
|
|
|
# >= Numeric greater than or equal |
69
|
|
|
|
|
|
|
# eqic Equal ignoring case |
70
|
|
|
|
|
|
|
# neic Not equal ignoring case |
71
|
|
|
|
|
|
|
# mult-of Multple of |
72
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
73
|
|
|
|
|
|
|
#|test(false) compare('eq','',undef); |
74
|
|
|
|
|
|
|
#|test(true) compare('eq','abc','abc'); |
75
|
|
|
|
|
|
|
#|test(true) compare('ne','abc','Abc'); |
76
|
|
|
|
|
|
|
#|test(false) compare('eq','abc',undef); |
77
|
|
|
|
|
|
|
#|test(true) compare('!~','abc','A'); |
78
|
|
|
|
|
|
|
#|test(true) compare('=~','abc','a'); |
79
|
|
|
|
|
|
|
#|test(true) compare('==',1234,1234); |
80
|
|
|
|
|
|
|
#|test(true) compare('>=',1234,1234); |
81
|
|
|
|
|
|
|
#|test(true) compare('eqic','abc','Abc'); |
82
|
|
|
|
|
|
|
#|test(true) compare('==',undef,undef); |
83
|
|
|
|
|
|
|
#|test(false) compare('==',0,undef); |
84
|
|
|
|
|
|
|
#|test(match) my @numbers = ( 20, 1, 10, 2 ); |
85
|
|
|
|
|
|
|
#| join ';', sort { &compare('<=>',$a,$b) } @numbers; |
86
|
|
|
|
|
|
|
#~ 1;2;10;20 |
87
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub compare { |
90
|
0
|
0
|
|
0
|
1
|
0
|
my $comparator = shift or croak 'Comparator required'; |
91
|
0
|
0
|
|
|
|
0
|
croak "Unknown comparator: $comparator" unless defined $COMPARISONS{$comparator}; |
92
|
0
|
|
|
|
|
0
|
return defined $_[0] |
93
|
|
|
|
|
|
|
? defined $_[1] |
94
|
0
|
0
|
|
|
|
0
|
? &{$COMPARISONS{$comparator}} |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
95
|
|
|
|
|
|
|
: 0 |
96
|
|
|
|
|
|
|
: !defined $_[1] |
97
|
|
|
|
|
|
|
? 1 |
98
|
|
|
|
|
|
|
: 0; |
99
|
|
|
|
|
|
|
}#compare |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
102
|
|
|
|
|
|
|
# sort_compare - Compare for sorting, returning 1, 0 or -1 |
103
|
|
|
|
|
|
|
# See also L |
104
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub sort_compare { |
107
|
0
|
0
|
|
0
|
1
|
0
|
my $comparator = shift or croak 'Comparator required'; |
108
|
0
|
0
|
|
|
|
0
|
croak "Unknown comparator: $comparator" unless defined $COMPARISONS{$comparator}; |
109
|
0
|
|
|
|
|
0
|
return defined $_[0] |
110
|
|
|
|
|
|
|
? defined $_[1] |
111
|
0
|
0
|
|
|
|
0
|
? &{$COMPARISONS{$comparator}} |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
112
|
|
|
|
|
|
|
: 1 |
113
|
|
|
|
|
|
|
: defined $_[1] |
114
|
|
|
|
|
|
|
? -1 |
115
|
|
|
|
|
|
|
: 0; |
116
|
|
|
|
|
|
|
}#sort_compare |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
119
|
|
|
|
|
|
|
# is_bipolar - Test to see if this is a blessed document/view object |
120
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub is_bipolar { |
123
|
54
|
50
|
|
54
|
1
|
594
|
can($_[0], 'get_data') && can($_[0], 'get_content'); |
124
|
|
|
|
|
|
|
}#is_bipolar |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
127
|
|
|
|
|
|
|
1; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
__END__ |