line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Ruby::Collections; |
2
|
3
|
|
|
3
|
|
235814
|
use Exporter 'import'; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
198
|
|
3
|
|
|
|
|
|
|
@EXPORT = qw(ra rh p p_obj p_array p_hash); |
4
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
5
|
3
|
|
|
3
|
|
18
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
127
|
|
6
|
3
|
|
|
3
|
|
44
|
use v5.10; |
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
165
|
|
7
|
3
|
|
|
3
|
|
17
|
use Scalar::Util qw(reftype); |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
214
|
|
8
|
3
|
|
|
3
|
|
17
|
use FindBin; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
149
|
|
9
|
3
|
|
|
3
|
|
16
|
use lib "$FindBin::Bin/../../lib"; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
25
|
|
10
|
|
|
|
|
|
|
require Ruby::Collections::Hash; |
11
|
|
|
|
|
|
|
require Ruby::Collections::Array; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=item ra() |
14
|
|
|
|
|
|
|
Create a Ruby::Collections::Array with optional arguments or any array ref. |
15
|
|
|
|
|
|
|
If array ref is also a Ruby::Collections::Array, it will be nested in instead of wrapped up. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Examples: |
18
|
|
|
|
|
|
|
ra -> [] |
19
|
|
|
|
|
|
|
ra( 1, 2, 3 ) -> [ 1, 2, 3 ] |
20
|
|
|
|
|
|
|
ra( [ 1, 2, 3 ] ) -> [ 1, 2, 3 ] |
21
|
|
|
|
|
|
|
ra( ra( 1, 2, 3 ) ) -> [ [ 1, 2, 3 ] ] |
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub ra { |
25
|
3739
|
|
|
3739
|
1
|
17957
|
my $new_ary = tie my @new_ary, 'Ruby::Collections::Array'; |
26
|
3739
|
100
|
100
|
|
|
33803
|
if ( @_ == 1 |
|
|
|
100
|
|
|
|
|
27
|
|
|
|
|
|
|
&& reftype( $_[0] ) eq 'ARRAY' |
28
|
|
|
|
|
|
|
&& ref( $_[0] ) ne 'Ruby::Collections::Array' ) |
29
|
|
|
|
|
|
|
{ |
30
|
12
|
|
|
|
|
15
|
@new_ary = @{ $_[0] }; |
|
12
|
|
|
|
|
44
|
|
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
else { |
33
|
3727
|
|
|
|
|
11595
|
@new_ary = @_; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
3739
|
|
|
|
|
42398
|
return $new_ary; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=item rh() |
40
|
|
|
|
|
|
|
Create a Ruby::Collections::Hash with optional arguments or any hash ref. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Examples: |
43
|
|
|
|
|
|
|
rh -> {} |
44
|
|
|
|
|
|
|
rh( { 'a' => 1 } ) -> { 'a' => 1 } |
45
|
|
|
|
|
|
|
rh( 'a' => 1 ) -> { 'a' => 1 } |
46
|
|
|
|
|
|
|
rh( 'a', 1 ) -> { 'a' => 1 } |
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub rh { |
50
|
152
|
|
|
152
|
1
|
10379
|
my $new_hash = tie my %new_hash, 'Ruby::Collections::Hash'; |
51
|
152
|
|
|
|
|
750
|
%new_hash = (); |
52
|
|
|
|
|
|
|
|
53
|
152
|
100
|
|
|
|
456
|
if ( @_ == 0 ) { |
|
|
100
|
|
|
|
|
|
54
|
16
|
|
|
|
|
84
|
return $new_hash; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
elsif ( @_ == 1 ) { |
57
|
3
|
50
|
|
|
|
32
|
if ( reftype( $_[0] ) eq 'HASH' ) { |
58
|
3
|
|
|
|
|
6
|
%new_hash = %{ $_[0] }; |
|
3
|
|
|
|
|
16
|
|
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
0
|
|
|
|
|
0
|
die 'Input is not a HASH.'; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
else { |
65
|
133
|
100
|
|
|
|
308
|
if ( @_ % 2 == 0 ) { |
66
|
132
|
|
|
|
|
352
|
for ( my $i = 0 ; $i < @_ ; $i += 2 ) { |
67
|
309
|
|
|
|
|
1703
|
$new_hash->{ $_[$i] } = $_[ $i + 1 ]; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
else { |
71
|
1
|
|
|
|
|
17
|
die 'Number of keys and values is not even.'; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
135
|
|
|
|
|
1042
|
return $new_hash; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item p() |
79
|
|
|
|
|
|
|
Print the data structure of any object. |
80
|
|
|
|
|
|
|
If the object is simply a scalar, it will be printed out directly. |
81
|
|
|
|
|
|
|
Undefined object will be printed as 'undef' instead of ''. |
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub p { |
85
|
5
|
|
|
5
|
1
|
1818
|
for my $item (@_) { |
86
|
5
|
100
|
|
|
|
31
|
if ( reftype($item) eq 'ARRAY' ) { |
|
|
50
|
|
|
|
|
|
87
|
4
|
|
|
|
|
12
|
say p_array($item); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
elsif ( reftype($item) eq 'HASH' ) { |
90
|
1
|
|
|
|
|
4
|
say p_hash($item); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
0
|
0
|
|
|
|
0
|
say defined $item ? "$item" : 'undef'; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item p_obj() |
99
|
|
|
|
|
|
|
Same as p(). Instead of printing the result, it simply returns a string. |
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub p_obj { |
103
|
3152
|
|
|
3152
|
1
|
5313
|
my $str_ary = ra; |
104
|
3152
|
|
|
|
|
5689
|
for my $item (@_) { |
105
|
3152
|
100
|
|
|
|
11184
|
if ( reftype($item) eq 'ARRAY' ) { |
|
|
100
|
|
|
|
|
|
106
|
179
|
|
|
|
|
302
|
$str_ary->push( p_array($item) ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
elsif ( reftype($item) eq 'HASH' ) { |
109
|
2
|
|
|
|
|
8
|
$str_ary->push( p_hash($item) ); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else { |
112
|
2971
|
100
|
|
|
|
11905
|
$str_ary->push( ( defined $item ) ? "$item" : 'undef' ); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
3152
|
|
|
|
|
9434
|
return $str_ary->join("\n"); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item p_array() |
119
|
|
|
|
|
|
|
Retuen the stringfied data structure of any ARRAY. |
120
|
|
|
|
|
|
|
Undefined object will be printed as 'undef' instead of ''. |
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub p_array { |
124
|
732
|
|
|
732
|
1
|
964
|
my $ary = shift @_; |
125
|
732
|
|
|
|
|
1013
|
my @str_ary = (); |
126
|
|
|
|
|
|
|
|
127
|
732
|
|
|
|
|
754
|
for my $item ( @{$ary} ) { |
|
732
|
|
|
|
|
24403
|
|
128
|
1723
|
100
|
|
|
|
5708
|
if ( reftype($item) eq 'ARRAY' ) { |
|
|
100
|
|
|
|
|
|
129
|
272
|
|
|
|
|
559
|
push( @str_ary, p_array($item) ); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
elsif ( reftype($item) eq 'HASH' ) { |
132
|
23
|
|
|
|
|
52
|
push( @str_ary, p_hash($item) ); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
else { |
135
|
1428
|
100
|
|
|
|
4330
|
push( @str_ary, defined $item ? "$item" : 'undef' ); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
732
|
|
|
|
|
4657
|
return '[' . join( ', ', @str_ary ) . ']'; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item p_hash() |
143
|
|
|
|
|
|
|
Print the stringfied data structure of any HASH. |
144
|
|
|
|
|
|
|
Undefined object will be printed as 'undef' instead of ''. |
145
|
|
|
|
|
|
|
=cut |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub p_hash { |
148
|
28
|
|
|
28
|
1
|
46
|
my $hash = shift @_; |
149
|
28
|
|
|
|
|
39
|
my @str_ary = (); |
150
|
28
|
|
|
|
|
34
|
my @key_str_ary = (); |
151
|
28
|
|
|
|
|
33
|
my @val_str_ary = (); |
152
|
|
|
|
|
|
|
|
153
|
28
|
|
|
|
|
99
|
while ( my ( $key, $val ) = each %$hash ) { |
154
|
29
|
50
|
|
|
|
161
|
if ( reftype($key) eq 'ARRAY' ) { |
|
|
50
|
|
|
|
|
|
155
|
0
|
|
|
|
|
0
|
push( @key_str_ary, p_array($key) ); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
elsif ( reftype($key) eq 'HASH' ) { |
158
|
0
|
|
|
|
|
0
|
push( @key_str_ary, p_hash($key) ); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
else { |
161
|
29
|
50
|
|
|
|
79
|
push( @key_str_ary, defined $key ? "$key" : 'undef' ); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
29
|
100
|
|
|
|
107
|
if ( reftype($val) eq 'ARRAY' ) { |
|
|
50
|
|
|
|
|
|
165
|
4
|
|
|
|
|
10
|
push( @val_str_ary, p_array($val) ); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
elsif ( reftype($val) eq 'HASH' ) { |
168
|
0
|
|
|
|
|
0
|
push( @val_str_ary, p_hash($val) ); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
else { |
171
|
25
|
50
|
|
|
|
130
|
push( @val_str_ary, defined $val ? "$val" : 'undef' ); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
28
|
|
|
|
|
77
|
for ( my $i = 0 ; $i < scalar(@key_str_ary) ; $i++ ) { |
176
|
29
|
|
|
|
|
127
|
@str_ary[$i] = @key_str_ary[$i] . '=>' . @val_str_ary[$i]; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
28
|
|
|
|
|
204
|
return '{' . join( ', ', @str_ary ) . '}'; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
1; |
183
|
|
|
|
|
|
|
__END__; |