line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::XHash; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
645
|
use Data::XHash; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
372
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Data::XHash::Splice - Add splice to your XHash |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 0.09 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$xhash->splice(\%options?, $offset, $length, @elements?); |
18
|
|
|
|
|
|
|
$xhash->spliceref(\%options?, $offset, $length, \@elements?); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
This module adds methods C and C to |
23
|
|
|
|
|
|
|
L. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
These are the only methods that deal explicitly with offsets rather than |
26
|
|
|
|
|
|
|
keys. Using them *might* mean that you have chosen the wrong data |
27
|
|
|
|
|
|
|
structure, but never say never. It's here if you need it. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 METHODS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head2 $xhash->splice(\%options?, $offset?, $length?, @elements?) |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head2 $xhash->spliceref(\%options?, $offset?, $length?, \@elements?) |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Splice removes C<$length> elements (to the end of the XHash if missing |
36
|
|
|
|
|
|
|
or C) beginning at C<$offset> (from the beginning of the XHash if |
37
|
|
|
|
|
|
|
missing or C) and returns them (as an XHash by default). |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
The offset and/or length may be negative, in which case they are interpreted |
40
|
|
|
|
|
|
|
as being from the end of the XHash instead of the start. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
If you specify new elements, they are put in place of the removed ones. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Options: |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=over |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item to => $destination |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
This option is passed to C, and controls how the deleted |
51
|
|
|
|
|
|
|
elements are returned. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item nested => $boolean |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
This option is passed to C and controls whether added |
56
|
|
|
|
|
|
|
elements are recursively converted to XHashes. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=back |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub splice : method { |
63
|
11
|
|
|
11
|
0
|
18
|
my $self = shift; |
64
|
11
|
50
|
|
|
|
34
|
my %options = ref($_[0]) eq 'HASH'? %{shift()}: (); |
|
0
|
|
|
|
|
0
|
|
65
|
11
|
|
|
|
|
18
|
my ($offset, $length) = (shift, shift); |
66
|
|
|
|
|
|
|
|
67
|
11
|
|
|
|
|
182
|
return $self->spliceref(\%options, $offset, $length, \@_); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub spliceref { |
71
|
11
|
|
|
11
|
0
|
15
|
my $self = shift; |
72
|
11
|
50
|
|
|
|
31
|
my %options = ref($_[0]) eq 'HASH'? %{shift()}: (); |
|
11
|
|
|
|
|
32
|
|
73
|
11
|
|
|
|
|
18
|
my ($offset, $length, $elements) = @_; |
74
|
11
|
|
|
|
|
40
|
my @keys = $self->keys(); |
75
|
11
|
|
|
|
|
17
|
my $return; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Default destination |
78
|
11
|
50
|
|
|
|
51
|
$options{to} = $self->new() unless exists($options{to}); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Normalize undef and negative offset and length |
81
|
11
|
100
|
|
|
|
30
|
$offset = 0 unless defined($offset); |
82
|
11
|
100
|
|
|
|
27
|
$offset += @keys if $offset < 0; |
83
|
11
|
100
|
|
|
|
36
|
if (!defined($length)) { |
|
|
100
|
|
|
|
|
|
84
|
3
|
|
|
|
|
6
|
$length = @keys - $offset; |
85
|
|
|
|
|
|
|
} elsif ($length < 0) { |
86
|
1
|
|
|
|
|
3
|
$length = @keys + $length - $offset; |
87
|
|
|
|
|
|
|
} |
88
|
11
|
50
|
|
|
|
27
|
if ($offset < 0) { |
89
|
0
|
|
|
|
|
0
|
$length += $offset; |
90
|
0
|
|
|
|
|
0
|
$offset = 0; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
11
|
100
|
100
|
|
|
51
|
if ($offset < @keys && $length > 0) { |
94
|
7
|
|
|
|
|
22
|
my @delete = splice(@keys, $offset, $length); |
95
|
7
|
|
|
|
|
39
|
$return = $self->delete({ to => $options{to} }, @delete); |
96
|
|
|
|
|
|
|
} else { |
97
|
4
|
|
|
|
|
9
|
$return = $options{to}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
11
|
100
|
66
|
|
|
68
|
if ($elements && @$elements) { |
101
|
7
|
100
|
|
|
|
15
|
if ($offset > 0) { |
102
|
|
|
|
|
|
|
# not spliced - add here - spliced - not spliced |
103
|
4
|
|
|
|
|
21
|
$self->pushref($elements, at_key => $keys[$offset - 1], |
104
|
|
|
|
|
|
|
nested => $options{nested}); |
105
|
|
|
|
|
|
|
} else { |
106
|
|
|
|
|
|
|
# add here - spliced - not spliced |
107
|
3
|
|
|
|
|
21
|
$self->unshiftref($elements, nested => $options{nested}); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
11
|
|
|
|
|
59
|
return $return; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 SEE ALSO |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
perldoc -f splice |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 AUTHOR |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Brian Katzung, C<< >> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 SUPPORT AND BUG TRACKING |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
See L. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Copyright 2012 Brian Katzung. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
131
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
132
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=cut |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; # End of Data::XHash::Splice |