| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Tie::Hash::Stack; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
#============================================================================= |
|
4
|
|
|
|
|
|
|
# |
|
5
|
|
|
|
|
|
|
# $Id: Stack.pm,v 0.9 2001/06/30 12:13:46 mneylon Exp $ |
|
6
|
|
|
|
|
|
|
# $Revision: 0.9 $ |
|
7
|
|
|
|
|
|
|
# $Author: mneylon $ |
|
8
|
|
|
|
|
|
|
# $Date: 2001/06/30 12:13:46 $ |
|
9
|
|
|
|
|
|
|
# $Log: Stack.pm,v $ |
|
10
|
|
|
|
|
|
|
# Revision 0.9 2001/06/30 12:13:46 mneylon |
|
11
|
|
|
|
|
|
|
# |
|
12
|
|
|
|
|
|
|
# Initial Release (based on www.perlmonks.org code with some additional |
|
13
|
|
|
|
|
|
|
# changes) |
|
14
|
|
|
|
|
|
|
# |
|
15
|
|
|
|
|
|
|
# |
|
16
|
|
|
|
|
|
|
#============================================================================= |
|
17
|
|
|
|
|
|
|
|
|
18
|
11
|
|
|
11
|
|
33198
|
use strict; |
|
|
11
|
|
|
|
|
21
|
|
|
|
11
|
|
|
|
|
800
|
|
|
19
|
11
|
|
|
11
|
|
58
|
use Carp; |
|
|
11
|
|
|
|
|
18
|
|
|
|
11
|
|
|
|
|
992
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
11
|
|
|
11
|
|
55
|
use Exporter (); |
|
|
11
|
|
|
|
|
23
|
|
|
|
11
|
|
|
|
|
263
|
|
|
22
|
11
|
|
|
11
|
|
55
|
use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS); |
|
|
11
|
|
|
|
|
13
|
|
|
|
11
|
|
|
|
|
14523
|
|
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$VERSION = "0.100"; |
|
25
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
|
26
|
|
|
|
|
|
|
@EXPORT = qw(push_hash pop_hash shift_hash unshift_hash |
|
27
|
|
|
|
|
|
|
reverse_hash merge_hash flatten_hash get_depth); |
|
28
|
|
|
|
|
|
|
%EXPORT_TAGS = ( ); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub TIEHASH { |
|
31
|
12
|
|
|
12
|
|
376
|
my $self = shift; |
|
32
|
12
|
|
100
|
|
|
92
|
my $newhash = shift || {}; |
|
33
|
12
|
100
|
|
|
|
278
|
croak "Argument must be a hashref in Tie::Hash::Stack constructor" |
|
34
|
|
|
|
|
|
|
unless UNIVERSAL::isa( $newhash, 'HASH' ); |
|
35
|
11
|
|
|
|
|
27
|
my $hash_array = [ $newhash ]; # Create array, with one blank hash |
|
36
|
11
|
|
|
|
|
61
|
return bless $hash_array, $self; |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub FETCH { |
|
40
|
109
|
|
|
109
|
|
784
|
my $self = shift; |
|
41
|
109
|
|
|
|
|
134
|
my $key = shift; |
|
42
|
|
|
|
|
|
|
|
|
43
|
109
|
|
|
|
|
173
|
foreach ( @$self ) { |
|
44
|
|
|
|
|
|
|
# $_ is a hashref! |
|
45
|
150
|
100
|
|
|
|
487
|
return $_->{ $key } if ( exists $_->{ $key } ); |
|
46
|
|
|
|
|
|
|
} |
|
47
|
32
|
|
|
|
|
155
|
return undef; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub STORE { |
|
51
|
59
|
|
|
59
|
|
366
|
my $self = shift; |
|
52
|
59
|
|
|
|
|
84
|
my $key = shift; |
|
53
|
59
|
|
|
|
|
78
|
my $value = shift; |
|
54
|
|
|
|
|
|
|
|
|
55
|
59
|
|
|
|
|
927
|
$self->[0]->{ $key } = $value; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub DELETE { |
|
59
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
60
|
0
|
|
|
|
|
0
|
my $key = shift; |
|
61
|
0
|
|
|
|
|
0
|
my $return = $self->FETCH( $key ); |
|
62
|
0
|
|
|
|
|
0
|
foreach ( @$self ) { |
|
63
|
0
|
0
|
|
|
|
0
|
delete $_->{ $key } if ( exists $_->{ $key } ); |
|
64
|
|
|
|
|
|
|
} |
|
65
|
0
|
|
|
|
|
0
|
return $return; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub CLEAR { |
|
70
|
2
|
|
|
2
|
|
59
|
my $self = shift; |
|
71
|
2
|
|
|
|
|
11
|
@$self = ( { } ); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub EXISTS { |
|
75
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
76
|
0
|
|
|
|
|
0
|
my $key = shift; |
|
77
|
0
|
|
|
|
|
0
|
foreach ( @$self ) { |
|
78
|
0
|
0
|
|
|
|
0
|
return 1 if exists $_->{ $key }; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
0
|
|
|
|
|
0
|
return undef; |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub FIRSTKEY { |
|
84
|
3
|
|
|
3
|
|
60
|
my $self = shift; |
|
85
|
3
|
|
|
|
|
5
|
my %hash; |
|
86
|
3
|
|
|
|
|
4
|
foreach ( @$self ) { |
|
87
|
9
|
|
|
|
|
14
|
foreach my $key ( keys %$_ ) { |
|
88
|
21
|
|
|
|
|
34
|
$hash{ $key } = 1; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
} |
|
91
|
3
|
|
|
|
|
14
|
my @keys = sort keys %hash; |
|
92
|
3
|
|
|
|
|
13
|
return $keys[ 0 ]; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub NEXTKEY { |
|
96
|
15
|
|
|
15
|
|
39
|
my $self = shift; |
|
97
|
15
|
|
|
|
|
14
|
my $lastkey = shift; |
|
98
|
15
|
|
|
|
|
15
|
my %hash; |
|
99
|
15
|
|
|
|
|
18
|
foreach ( @$self ) { |
|
100
|
45
|
|
|
|
|
67
|
foreach my $key ( keys %$_ ) { |
|
101
|
105
|
|
|
|
|
156
|
$hash{ $key } = 1; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
} |
|
104
|
15
|
|
|
|
|
56
|
my @keys = sort keys %hash; |
|
105
|
15
|
|
|
|
|
19
|
my $i = 0; |
|
106
|
15
|
|
66
|
|
|
111
|
$i++ while ( ( $lastkey ne $keys[ $i ] ) |
|
107
|
|
|
|
|
|
|
&& ( $i < @keys - 1 ) ) ; |
|
108
|
15
|
100
|
|
|
|
25
|
if ( $i == @keys - 1 ) { |
|
109
|
3
|
|
|
|
|
12
|
return (); |
|
110
|
|
|
|
|
|
|
} else { |
|
111
|
12
|
|
|
|
|
48
|
return $keys[ $i+1 ]; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
0
|
|
|
0
|
|
0
|
sub DESTROY { |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub merge_hash (\%) { |
|
119
|
2
|
|
|
2
|
1
|
15
|
my $href = shift; |
|
120
|
2
|
|
|
|
|
7
|
my $obj = tied %$href; |
|
121
|
2
|
50
|
33
|
|
|
41
|
croak "must be a Tie::Hash::Stack tied hash" |
|
122
|
|
|
|
|
|
|
unless $obj and $obj->isa('Tie::Hash::Stack'); |
|
123
|
2
|
|
|
|
|
8
|
my %hash; |
|
124
|
2
|
|
|
|
|
9
|
foreach ( reverse @$obj ) { |
|
125
|
6
|
|
|
|
|
25
|
foreach my $key ( keys %$_ ) { |
|
126
|
14
|
|
|
|
|
45
|
$hash{ $key } = $_->{ $key }; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
} |
|
129
|
2
|
|
|
|
|
20
|
return %hash; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub flatten_hash(\%) { |
|
133
|
1
|
|
|
1
|
1
|
6
|
my $href = shift; |
|
134
|
1
|
|
|
|
|
2
|
my $obj = tied %$href; |
|
135
|
1
|
50
|
33
|
|
|
10
|
croak "must be a Tie::Hash::Stack tied hash" |
|
136
|
|
|
|
|
|
|
unless $obj and $obj->isa('Tie::Hash::Stack'); |
|
137
|
1
|
|
|
|
|
858
|
my %hash = merge_hash %$href; |
|
138
|
1
|
|
|
|
|
14
|
@$obj = ( \%hash ); |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub push_hash (\%;\%) { |
|
143
|
16
|
|
|
16
|
1
|
92
|
my $href = shift; |
|
144
|
16
|
|
|
|
|
31
|
my $obj = tied %$href; |
|
145
|
16
|
|
100
|
|
|
1356
|
my $addhash = shift || {}; |
|
146
|
16
|
50
|
33
|
|
|
149
|
croak "First argument must be a Tie::Hash::Stack tied hash in push_hash" |
|
147
|
|
|
|
|
|
|
unless $obj and $obj->isa('Tie::Hash::Stack'); |
|
148
|
16
|
50
|
|
|
|
56
|
croak "Second argument must be a hashref in push_hash" |
|
149
|
|
|
|
|
|
|
unless UNIVERSAL::isa( $addhash, 'HASH' ); |
|
150
|
16
|
|
|
|
|
52
|
unshift @$obj, $addhash; |
|
151
|
|
|
|
|
|
|
} |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub pop_hash (\%) { |
|
154
|
9
|
|
|
9
|
1
|
63
|
my $href = shift; |
|
155
|
9
|
|
|
|
|
13
|
my $obj = tied %$href; |
|
156
|
9
|
50
|
33
|
|
|
60
|
croak "First argument must be a Tie::Hash::Stack tied hash in pop_hash" |
|
157
|
|
|
|
|
|
|
unless $obj and $obj->isa('Tie::Hash::Stack'); |
|
158
|
9
|
|
|
|
|
16
|
shift @$obj; |
|
159
|
9
|
100
|
|
|
|
85
|
@$obj = ( { } ) if !@$obj; |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub unshift_hash (\%;\%) { |
|
163
|
2
|
|
|
2
|
1
|
19
|
my $href = shift; |
|
164
|
2
|
|
|
|
|
4
|
my $obj = tied %$href; |
|
165
|
2
|
|
100
|
|
|
9
|
my $addhash = shift || {}; |
|
166
|
2
|
50
|
33
|
|
|
21
|
croak "First argument must be a Tie::Hash::Stack tied hash in unshift_hash" |
|
167
|
|
|
|
|
|
|
unless $obj and $obj->isa('Tie::Hash::Stack'); |
|
168
|
2
|
50
|
|
|
|
8
|
croak "Second argument must be a hashref in unshift_hash" |
|
169
|
|
|
|
|
|
|
unless UNIVERSAL::isa( $addhash, 'HASH' ); |
|
170
|
2
|
|
|
|
|
6
|
push @$obj, $addhash; |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub shift_hash (\%) { |
|
174
|
4
|
|
|
4
|
1
|
26
|
my $href = shift; |
|
175
|
4
|
|
|
|
|
6
|
my $obj = tied %$href; |
|
176
|
4
|
50
|
33
|
|
|
25
|
croak "First argument must be a Tie::Hash::Stack tied hash in shift_hash" |
|
177
|
|
|
|
|
|
|
unless $obj and $obj->isa('Tie::Hash::Stack'); |
|
178
|
4
|
|
|
|
|
5
|
pop @$obj; |
|
179
|
4
|
100
|
|
|
|
18
|
@$obj = ( { } ) if !@$obj; |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub reverse_hash (\%) { |
|
183
|
1
|
|
|
1
|
1
|
9
|
my $href = shift; |
|
184
|
1
|
|
|
|
|
2
|
my $obj = tied %$href; |
|
185
|
1
|
50
|
33
|
|
|
9
|
croak "First argument must be a Tie::Hash::Stack tied hash in shift_hash" |
|
186
|
|
|
|
|
|
|
unless $obj and $obj->isa('Tie::Hash::Stack'); |
|
187
|
1
|
|
|
|
|
5
|
@$obj = reverse @$obj; |
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub get_depth (\%) { |
|
191
|
7
|
|
|
7
|
1
|
25
|
my $href = shift; |
|
192
|
7
|
|
|
|
|
10
|
my $obj = tied %$href; |
|
193
|
7
|
50
|
33
|
|
|
42
|
croak "First argument must be a Tie::Hash::Stack tied hash in shift_hash" |
|
194
|
|
|
|
|
|
|
unless $obj and $obj->isa('Tie::Hash::Stack'); |
|
195
|
7
|
|
|
|
|
17
|
return @$obj - 1; |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
42; |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
__END__ |