| 1 | package Data::SimplePath; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use warnings::register; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | Data::SimplePath - Path-like access to complex data structures | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | Version 0.005 | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 16 | our $VERSION = '0.005'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | { | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # global options, will be used as defaults for newly created objects, can be changed on | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # import (see import): | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 16 | my %config = ( | |||||||||||||||||||||||||||||||||||||||||
| 24 | 'AUTO_ARRAY' => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 'REPLACE_LEAF' => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 'SEPARATOR' => '/', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # _global ($key, $value) - retrieves or changes global configuration options, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # $key the option to get (see %config above for valid keys) | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # $value if defined, the option will be set to this value and a true value | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # will be returned, if undefined, current value of the option will be | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # returned | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # if $key is invalid, an undefined value will be returned | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 968 | ▶ | sub _global { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_global: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 968 | my ($key, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 968 | ▶ | return unless exists $config {$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 964 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | $config {$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 958 | return $config {$key}; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 16 | my $valid_number = qr/^\d+$/; # only positive integers (no sign) are valid as array index | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # _number ($var) - returns the - always true - array index if $var is a valid number (ie. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # the number $var if it is greater than 0 or '0 but true' for the value 0 itself). if the | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # argument is not a valid number, returns undef. | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 338 | ▶ | sub _number { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_number: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 338 | my ($num) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 338 | ▶ | return unless $num =~ /$valid_number/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 272 | ▶ | return $num == 0 ? '0 but true' : $num; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | # import (%args) - allow global options to be set when the module is used. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # eg.: use Data::SimplePath 'AUTO_ARRAY' => 1, 'SEPARATOR' => '/'; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # see global config hash above for valid keys. will warn on invalid keys if enabled. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 16 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 17 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 17 | ▶ | while (my ($key, $value) = each %args) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | ▶ | _warn ("Unknown option: $key") unless _global ($key, $value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 17 | return; # birthday present for perl::critic... | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # use default options | ||||||||||||||||||||||||||||||||||||||||||
| 75 | use Data::SimplePath; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # or change the default options for new objects: | ||||||||||||||||||||||||||||||||||||||||||
| 78 | use Data::SimplePath 'AUTO_ARRAY' => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 'REPLACE_LEAF' => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 'SEPARATOR' => '#'; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # create new empty object with default options: | ||||||||||||||||||||||||||||||||||||||||||
| 83 | my $a = Data::SimplePath -> new (); | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | # create new object, set some initial content: | ||||||||||||||||||||||||||||||||||||||||||
| 86 | my $b = Data::SimplePath -> new ( | ||||||||||||||||||||||||||||||||||||||||||
| 87 | { 'k1' => 'v1', 'k2' => ['a', { 'b' => 'c' }, 'd'] } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # same as above, but override some default options: | ||||||||||||||||||||||||||||||||||||||||||
| 91 | my $c = Data::SimplePath -> new ( | ||||||||||||||||||||||||||||||||||||||||||
| 92 | { 'k1' => 'v1', 'k2' => ['a', { 'b' => 'c' }, 'd'] }, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | { 'AUTO_ARRAY' => 0, 'SEPARATOR' => ':' } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # get the value 'c', ':' is the separator: | ||||||||||||||||||||||||||||||||||||||||||
| 97 | my $x = $c -> get ('k2:1:b'); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # change the separator to '/': | ||||||||||||||||||||||||||||||||||||||||||
| 100 | $c -> separator ('/'); | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | # enable automatic creation of arrays for numeric keys: | ||||||||||||||||||||||||||||||||||||||||||
| 103 | $c -> auto_array (1); | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # create a new element: | ||||||||||||||||||||||||||||||||||||||||||
| 106 | $c -> set ('k2/4/some key/0', 'new value'); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | # the object will now contain the following data: | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # 'k1' => 'v1', # k1 | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # 'k2' => [ # k2 | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # 'a', # k2/0 | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # { # k2/1 | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # 'b' => 'c' # k2/1/b | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # }, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # 'd', # k2/2 | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # undef, # k2/3 | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # { # k2/4 | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # 'some key' => [ # k2/4/some key | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # 'new value' # k2/4/some/key/0 | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # ] | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # ] | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | This module enables path-like (as in file system path) access to complex data structures of hashes | ||||||||||||||||||||||||||||||||||||||||||
| 130 | and/or arrays. Not much more to say, see the L<SYNOPSIS> example above... | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | Ok, a few more notes: The data structure may consist of hashes or arrays, to an arbitrary depth, | ||||||||||||||||||||||||||||||||||||||||||
| 133 | and scalar values. You probably should not try to put blessed arrays or hashes in it, it may lead | ||||||||||||||||||||||||||||||||||||||||||
| 134 | to unexpected behaviour in some situations. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | The object containing the data structure exists only to help accessing the contents, you are free | ||||||||||||||||||||||||||||||||||||||||||
| 137 | to modify the data in any way you want without the provided methods if you like, this will not | ||||||||||||||||||||||||||||||||||||||||||
| 138 | break the object's behaviour. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | The methods to access a certain element in the data structure need to know which element to act on | ||||||||||||||||||||||||||||||||||||||||||
| 141 | (of course), there are two ways of specifying the element: | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =item by key | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | The key is a single string, with parts of the path separated by the (object specific) separator. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | This is the recommended way to access an element. Note that the methods will normalize the provided | ||||||||||||||||||||||||||||||||||||||||||
| 149 | key before it is used, see the C<normalize ()> method below. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =item by path | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | The path is an array containing the parts of the full path, it is basically the key split on the | ||||||||||||||||||||||||||||||||||||||||||
| 154 | separator string. Empty (or undef) elements are usually ignored when a path is processed. | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | In the following documentation these two terms will be used as described above. Note that the root | ||||||||||||||||||||||||||||||||||||||||||
| 159 | of the data structure is specified as an empty key (ie. he empty string C<''>) or an empty array as | ||||||||||||||||||||||||||||||||||||||||||
| 160 | path. | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | =head2 Similar Modules | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | There are a few modules with similar functionality are available: L<Data::Path> and L<Data::DPath> | ||||||||||||||||||||||||||||||||||||||||||
| 165 | provide access to data structures using a more flexible and powerful (some may call it complicated) | ||||||||||||||||||||||||||||||||||||||||||
| 166 | XPath like matching. | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | L<Data::SPath> provides access to data structures using paths like C<Data::SimplePath> does | ||||||||||||||||||||||||||||||||||||||||||
| 169 | (including accessing arrayrefs with numeric keys, L<Data::Path> and L<Data::DPath> require special | ||||||||||||||||||||||||||||||||||||||||||
| 170 | syntax for arrayrefs). Also, this module does support calling object methods with method names | ||||||||||||||||||||||||||||||||||||||||||
| 171 | specified in the path, C<Data::SimplePath> does not offer special treatment for objects. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | However, unlike the aforementioned modules, C<Data::SimplePath> not only provides read access to an | ||||||||||||||||||||||||||||||||||||||||||
| 174 | existing data structure, it also provides methods to create, change or delete values in the data | ||||||||||||||||||||||||||||||||||||||||||
| 175 | structure, using paths to specify the location, and automatically create nested structures if | ||||||||||||||||||||||||||||||||||||||||||
| 176 | required. | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | So if you only need read access, see the documentation of the modules mentioned above, maybe one is | ||||||||||||||||||||||||||||||||||||||||||
| 179 | better suited for your needs than C<Data::SimplePath>. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | Each of the following configuration options can be set for every object either when creating the | ||||||||||||||||||||||||||||||||||||||||||
| 184 | object (see C<new ()> and the example in the L<SYNOPSIS> above) or later on with the methods | ||||||||||||||||||||||||||||||||||||||||||
| 185 | C<auto_array ()>, C<replace_leaf ()> and C<separator ()> (see below). The default values for the | ||||||||||||||||||||||||||||||||||||||||||
| 186 | options are mentioned below, and these defaults can be modified on C<import ()> time, as shown in | ||||||||||||||||||||||||||||||||||||||||||
| 187 | the C<SYNOPSIS> example above. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =head2 AUTO_ARRAY | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | If this option is set to a true value, arrays will be created for numeric keys: | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | # suppose the data structure is an empty hashref: | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | # with AUTO_ARRAY set to true: | ||||||||||||||||||||||||||||||||||||||||||
| 196 | $h -> set ('a/0/b', 'value'); | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | # the data structure will now contain: | ||||||||||||||||||||||||||||||||||||||||||
| 199 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 200 | # 'a' => [ # array created due to numeric key 0 | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 202 | # 'b' => 'value' | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # ] | ||||||||||||||||||||||||||||||||||||||||||
| 205 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | # same with AUTO_ARRAY set to false: | ||||||||||||||||||||||||||||||||||||||||||
| 208 | $h -> set ('a/0/b', 'value'); | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | # the data structure will now contain: | ||||||||||||||||||||||||||||||||||||||||||
| 211 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # 'a' => { # everything's a hash | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # '0' => { | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # 'b' => 'value' | ||||||||||||||||||||||||||||||||||||||||||
| 215 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | This only works for newly created sub-lists (and thus this setting only changes how the C<set ()> | ||||||||||||||||||||||||||||||||||||||||||
| 220 | method works), already existing hashes will not be changed, and elements in these hashes can be | ||||||||||||||||||||||||||||||||||||||||||
| 221 | created, deleted and accessed with numeric keys as usual. | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | The default value of this option is C<1> (enabled). | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | =head2 REPLACE_LEAF | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | If this option is true (default), an already existing scalar value in the data structure will be | ||||||||||||||||||||||||||||||||||||||||||
| 228 | replaced by a hashref or arrayref automatically if you try to C<set ()> a value beneath its path: | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # suppose the data structure contains the following data: | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # 'key' => 'value' | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | # with REPLACE_LEAF disabled: | ||||||||||||||||||||||||||||||||||||||||||
| 236 | $h -> set ('key/subkey', 'value'); # this won't work | ||||||||||||||||||||||||||||||||||||||||||
| 237 | # data is not changed | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | # with REPLACE_LEAF enabled: | ||||||||||||||||||||||||||||||||||||||||||
| 240 | $h -> set ('key/subkey', 'value'); # works | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | # the data structure now contains: | ||||||||||||||||||||||||||||||||||||||||||
| 243 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 244 | # 'key' => { | ||||||||||||||||||||||||||||||||||||||||||
| 245 | # 'subkey' => 'value' | ||||||||||||||||||||||||||||||||||||||||||
| 246 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | Note that if this option is set to false, you can still assign a hashref (or arrayref) directly to | ||||||||||||||||||||||||||||||||||||||||||
| 250 | the element itself: | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | # same result as above: | ||||||||||||||||||||||||||||||||||||||||||
| 253 | $h -> set ('key', {'subkey' => 'value'}); | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | The default value of this option is C<1> (enabled). | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | =head2 SEPARATOR | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | The string used to separate path elements in the key. This may be any string you like, just make | ||||||||||||||||||||||||||||||||||||||||||
| 260 | sure the string itself is not contained in any actual keys of the hashes in the data structure, you | ||||||||||||||||||||||||||||||||||||||||||
| 261 | will not be able to access such elements by key (access by path will still work, though). | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | The default value is C<'/'>. | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | my $h = Data::SimplePath -> new ($initial, $config); | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | Creates a new C<Data::SimplePath> object. If C<$initial> is specified, which must be either a | ||||||||||||||||||||||||||||||||||||||||||
| 272 | hashref or an arrayref, the contents of the object will be set to this data structure. C<$config> | ||||||||||||||||||||||||||||||||||||||||||
| 273 | may be used to set the configuration options for the object. It must be a hashref, valid keys are | ||||||||||||||||||||||||||||||||||||||||||
| 274 | C<'AUTO_ARRAY'>, C<'REPLACE_LEAF'> and C<'SEPARATOR'> (see the L<CONFIGURATION> section for | ||||||||||||||||||||||||||||||||||||||||||
| 275 | details). Note that you have to specify C<$initial> if you want to set configuration options, even | ||||||||||||||||||||||||||||||||||||||||||
| 276 | if you don't want to add any initial content, use C<undef> in that case: | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | my $h = Data::SimplePath -> new (undef, $config); | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | The initial hashref (or arrayref) will be used as is, every modification of the object will alter | ||||||||||||||||||||||||||||||||||||||||||
| 281 | the original data: | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | my $i = { 'a' => 'b' }; | ||||||||||||||||||||||||||||||||||||||||||
| 284 | my $h = Data::SimplePath -> new ($i); | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | $h -> set ('a', 'c'); | ||||||||||||||||||||||||||||||||||||||||||
| 287 | print $i -> {'a'}; # will print the new value 'c' | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | Note that if C<$initial> is defined a warning will be printed if it is not a hashref or an | ||||||||||||||||||||||||||||||||||||||||||
| 290 | arraryref, see the L<WARNINGS> section below. An invalid value for C<$config> will cause no | ||||||||||||||||||||||||||||||||||||||||||
| 291 | warning, the default settings will be used in this case. | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 313 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 313 | my ($class, $init, $config) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 313 | ▶ | my $self = { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 'DATA' => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 'AUTO_ARRAY' => _global ('AUTO_ARRAY'), | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 'REPLACE_LEAF' => _global ('REPLACE_LEAF'), | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 'SEPARATOR' => _global ('SEPARATOR'), | ||||||||||||||||||||||||||||||||||||||||||
| 302 | _hashref ($config) ? %$config : () | ||||||||||||||||||||||||||||||||||||||||||
| 303 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 313 | ▶ | $self -> {'DATA'} = $init if _valid_root ($init); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 313 | ▶ | _warn ("Discarding invalid data: $init") if defined $init and not $self -> {'DATA'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $init and not $self->{"DATA"}
MC/DC100%2/2
defined $init and not $self->{"DATA"}
defined $init
not $self->{"DATA"}
Truth table100%3/3
A and not(B)
A= defined $initB= not $self->{"DATA"}
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 313 | return bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 1988 | ▶ | sub _arrayref { return ref shift eq 'ARRAY' ? 1 : 0; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_arrayref: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 1726 | ▶ | sub _hashref { return ref shift eq 'HASH' ? 1 : 0; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hashref: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | 234 | ▶ | sub _warn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_warn: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 234 | ▶ | warnings::warn (shift) if warnings::enabled (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 234 | return; | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | # _is_number ($var) - basically the same as _number (), but prints a warning if $var is no number | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 296 | ▶ | sub _is_number { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_number: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 296 | my ($number) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 296 | ▶ | unless ($number = _number ($number)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 46 | _warn ("Trying to access array element with non-numeric key $_[0]"); | |||||||||||||||||||||||||||||||||||||||||
| 323 | 46 | return; | |||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 250 | return $number; | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | # _valid_root can be used as a class method (with one parameter $root) and as an object method | ||||||||||||||||||||||||||||||||||||||||||
| 329 | # (without any parameter), it will return true if $root or $self -> {'DATA'} is a hashref or an | ||||||||||||||||||||||||||||||||||||||||||
| 330 | # arrayref. | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 1033 | ▶ | sub _valid_root { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_valid_root: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 1033 | my ($root) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 334 | 1033 | ▶ | $root = $root -> {'DATA'} if (ref $root eq __PACKAGE__); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 1033 | ▶ | return (_arrayref ($root) or _hashref ($root)) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_arrayref($root) or _hashref($root)
MC/DC100%2/2
_arrayref($root) or _hashref($root)
_arrayref($root)
_hashref($root)
Truth table100%3/3
A or B
A= _arrayref($root)B= _hashref($root)
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | =head1 OBJECT METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | =head2 auto_array, replace_leaf, separator | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | # get current value | ||||||||||||||||||||||||||||||||||||||||||
| 343 | my $aa = $h -> auto_array (); | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | # set AUTO_ARRAY to 1, $aa will contain the old value: | ||||||||||||||||||||||||||||||||||||||||||
| 346 | my $aa = $h -> auto_array (1); | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | # the same syntax works for $h -> replace_leaf () and | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # $h -> separator () | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | Get and/or set the object's C<AUTO_ARRAY>, C<REPLACE_LEAF> and C<SEPARATOR> options. If no | ||||||||||||||||||||||||||||||||||||||||||
| 352 | parameter is specified (or the paramter is C<undef>) these methods will return the current value of | ||||||||||||||||||||||||||||||||||||||||||
| 353 | the option, else the option will be set to the given (scalar) value and the old setting will be | ||||||||||||||||||||||||||||||||||||||||||
| 354 | returned. | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | 238 | ▶ | sub auto_array { return shift -> _config ('AUTO_ARRAY', shift); } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
auto_array: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 250 | ▶ | sub replace_leaf { return shift -> _config ('REPLACE_LEAF', shift); } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace_leaf: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | 255 | ▶ | sub separator { return shift -> _config ('SEPARATOR', shift); } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
separator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | # _config ($key, $value) - get or set a configuration option | ||||||||||||||||||||||||||||||||||||||||||
| 363 | # $key the name of the option to get/set (AUTO_ARRAY, REPLACE_LEAF or SEPARATOR) | ||||||||||||||||||||||||||||||||||||||||||
| 364 | # $value if defined, the option is set to this new value and the old value is returned, if | ||||||||||||||||||||||||||||||||||||||||||
| 365 | # undefined, only the current value of the option will be returned | ||||||||||||||||||||||||||||||||||||||||||
| 366 | # for invalid keys (or the DATA key) undef must be returned! | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | 807 | ▶ | sub _config { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_config: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 807 | my ($self, $key, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 370 | 807 | ▶ | return if not exists $self -> {$key} or $key eq 'DATA'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not exists $self->{$key} or $key eq "DATA"
MC/DC100%2/2
not exists $self->{$key} or $key eq "DATA"
not exists $self->{$key}
$key eq "DATA"
Truth table100%3/3
not(A) or B
A= not exists $self->{$key}B= $key eq "DATA"
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 758 | ▶ | return $self -> {$key} unless defined $new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 372 | 27 | (my $old, $self -> {$key}) = ($self -> {$key}, $new); | |||||||||||||||||||||||||||||||||||||||||
| 373 | 27 | return $old; | |||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =head2 clone | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | my $copy = $h -> clone (); | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | Creates a new C<Data::SimplePath> object with the same contents and settings as the original one. | ||||||||||||||||||||||||||||||||||||||||||
| 381 | Both objects are independent, ie. changing the contents (or settings) of one object does not effect | ||||||||||||||||||||||||||||||||||||||||||
| 382 | the other one. (L<Storable>'s C<dclone ()> funtion is used to create the copy, see its | ||||||||||||||||||||||||||||||||||||||||||
| 383 | documentation for details.) | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | If you actually need more than one object to modify one data structure, either create the root | ||||||||||||||||||||||||||||||||||||||||||
| 388 | reference first and pass it to the constructors of the different objects, or retrieve the root | ||||||||||||||||||||||||||||||||||||||||||
| 389 | reference from an existing object with the C<data ()> method and pass it to the constructor. This | ||||||||||||||||||||||||||||||||||||||||||
| 390 | may be useful for example if you need certain operations with C<AUTO_ARRAY> enabled and others | ||||||||||||||||||||||||||||||||||||||||||
| 391 | without the C<AUTO_ARRAY> feature. | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 2 | ▶ | sub clone { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 399 | 2 | ▶ | require Storable if $self -> {'DATA'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | return __PACKAGE__ -> new ( | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 2 | ▶ | $self -> {'DATA'} ? Storable::dclone ($self -> {'DATA'}) : undef, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | { | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 'AUTO_ARRAY' => $self -> auto_array (), | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 'REPLACE_LEAF' => $self -> replace_leaf (), | ||||||||||||||||||||||||||||||||||||||||||
| 405 | 'SEPARATOR' => $self -> separator (), | ||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | =head2 data | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | my $data = $h -> data (); # get a reference to the object contents | ||||||||||||||||||||||||||||||||||||||||||
| 413 | my %data = $h -> data (); # or - if it's a hash - put a copy in a hash | ||||||||||||||||||||||||||||||||||||||||||
| 414 | my @data = $h -> data (); # or put a copy in an array | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | Returns the object contents. In scalar context, the reference (either a hashref or an arrayref, | ||||||||||||||||||||||||||||||||||||||||||
| 417 | depending on the data structure's root) will be returned - note that this is the actual data as | ||||||||||||||||||||||||||||||||||||||||||
| 418 | used in the object, modifications will effect the object's data. In list context L<Storable>'s | ||||||||||||||||||||||||||||||||||||||||||
| 419 | C<dclone ()> function will be used to create a copy of the data, the copy's root will be | ||||||||||||||||||||||||||||||||||||||||||
| 420 | dereferenced and the resulting list will be returned. Please see L<Storable>'s documentation for | ||||||||||||||||||||||||||||||||||||||||||
| 421 | limitations. | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | If there is no data, C<undef> (or an empty list) will be returned. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | 247 | ▶ | sub data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
data: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 247 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 429 | 247 | ▶ | return unless $self -> {'DATA'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | 127 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 2 | require Storable; | |||||||||||||||||||||||||||||||||||||||||
| 432 | 2 | my $new = Storable::dclone ($self -> {'DATA'}); | |||||||||||||||||||||||||||||||||||||||||
| 433 | 2 | ▶ | return _hashref ($new) ? %$new : @$new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 125 | return $self -> {'DATA'}; | |||||||||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | =head2 does_exist | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | if ($h -> does_exist ($key)) { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | Returns a true value if the element specified by the key exists in the data structure. If it does | ||||||||||||||||||||||||||||||||||||||||||
| 443 | not exist, an undefined value will be returned. Instead of a key you may also specify an arrayref | ||||||||||||||||||||||||||||||||||||||||||
| 444 | containing the path to the element to check. Using a key is recommended, though. The key will be | ||||||||||||||||||||||||||||||||||||||||||
| 445 | normalized before it is used, see the C<normalize_key ()> method below. | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | Actually, the value returned is a reference: if the element is itself a hashref or an arrayref, | ||||||||||||||||||||||||||||||||||||||||||
| 450 | that reference is returned, in all other cases, a reference to the element is returned (unless the | ||||||||||||||||||||||||||||||||||||||||||
| 451 | element does not exist, of course): | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | # for a Data::SimplePath object with the following data: | ||||||||||||||||||||||||||||||||||||||||||
| 454 | my $data = { | ||||||||||||||||||||||||||||||||||||||||||
| 455 | 'a' => { | ||||||||||||||||||||||||||||||||||||||||||
| 456 | 'a1' => 'scalar value for a1' | ||||||||||||||||||||||||||||||||||||||||||
| 457 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 458 | 'b' => 'scalar value for b', | ||||||||||||||||||||||||||||||||||||||||||
| 459 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | my $ref1 = $h -> does_exist ('a'); | ||||||||||||||||||||||||||||||||||||||||||
| 462 | my $ref2 = $h -> does_exist ('b'); | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | In this example C<$ref2> will be set to a reference to C<'scalar value for b'>, changing this | ||||||||||||||||||||||||||||||||||||||||||
| 465 | value is possible: | ||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | $$ref2 = 'another value for b'; | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | C<$ref1> will contain the same reference as C<< $data -> {'a'} >>, so you can change the contents | ||||||||||||||||||||||||||||||||||||||||||
| 470 | of this (sub-) hashref, but not C<< $data -> {'a'} >> itself. | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | However, it is recommended to use the C<set ()> method to change the data structure, the behaviour | ||||||||||||||||||||||||||||||||||||||||||
| 473 | of C<does_exist ()> may change in future versions. | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 329 | ▶ | sub does_exist { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
does_exist: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 329 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 481 | 329 | my @path = $self -> _path ($key); | |||||||||||||||||||||||||||||||||||||||||
| 482 | 329 | my $root = $self -> {'DATA'}; | |||||||||||||||||||||||||||||||||||||||||
| 483 | 329 | ▶ | while (defined (my $top = shift @path)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 484 | 630 | ▶ | return unless $root = $self -> _find_element ($root, $top); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||
| 486 | 155 | return $root; | |||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | # _find_element ($root, $key) - find an element directly under $root | ||||||||||||||||||||||||||||||||||||||||||
| 490 | # $root the current hashref or arrayref | ||||||||||||||||||||||||||||||||||||||||||
| 491 | # $key the key (or array index) to look for | ||||||||||||||||||||||||||||||||||||||||||
| 492 | # if either $root -> {$key} or $root -> [$key] exists, a reference will be returned, as described | ||||||||||||||||||||||||||||||||||||||||||
| 493 | # in the pod for the does_exist () method above (ie. hashref and arrayref will be returned | ||||||||||||||||||||||||||||||||||||||||||
| 494 | # directly, else a ref to the scalar will be returned). if the key does not exist, undef will be | ||||||||||||||||||||||||||||||||||||||||||
| 495 | # returned. additionally, a warning will be printed (if enabled) if the $root is an arrayref and | ||||||||||||||||||||||||||||||||||||||||||
| 496 | # the $key is not a number. if $root is invalid, undef will be returned, too. | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | 701 | ▶ | sub _find_element { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_element: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | 701 | my ($self, $root, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 500 | 701 | ▶ | if (_hashref ($root)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 335 | ▶ | return unless exists $root -> {$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | 297 | ▶ | return $root -> {$key} if _valid_root ($root -> {$key}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 53 | return \($root -> {$key}); | |||||||||||||||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||||||||||||||||
| 505 | elsif (_arrayref ($root)) { | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 263 | ▶ | return unless $key = _is_number ($key); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 507 | 231 | ▶ | return unless @{$root} > $key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 508 | 210 | ▶ | return $root -> [$key] if _valid_root ($root -> [$key]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | 62 | return \($root -> [$key]); | |||||||||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||||||||
| 511 | 103 | return; | |||||||||||||||||||||||||||||||||||||||||
| 512 | } # complaining about the returns: that's a paddling... | ||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | =head2 get | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | my $value = get ($key); | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | Returns the value of the element specified by the key C<$key>. If the element does not exist an | ||||||||||||||||||||||||||||||||||||||||||
| 519 | undefined value will be returned (which may be the actual value of the element, so better use the | ||||||||||||||||||||||||||||||||||||||||||
| 520 | C<does_exist ()> method to check for existence if this is required). Instead of a key you may also | ||||||||||||||||||||||||||||||||||||||||||
| 521 | specify an arrayref containing the path to the element to check. Using a key is recommended, | ||||||||||||||||||||||||||||||||||||||||||
| 522 | though. The key will be normalized before it is used, see the C<normalize_key ()> method below. | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | If the element specified by the key (or path) is itself a hashref or an arrayref, this reference | ||||||||||||||||||||||||||||||||||||||||||
| 525 | will be returned if the method is called in scalar context. In list context, it will be copied | ||||||||||||||||||||||||||||||||||||||||||
| 526 | (using L<Storable>'s C<dclone ()> function) and the resulting (dereferenced) list will be returned. | ||||||||||||||||||||||||||||||||||||||||||
| 527 | (See L<Storable>'s documentation for limitations.) | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | Note that if called with an empty key (or an empty path) C<get ()> works like the C<data ()> | ||||||||||||||||||||||||||||||||||||||||||
| 530 | method, see above for details. | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | 229 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | 229 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 536 | 229 | my $ref = $self -> does_exist ($key); | |||||||||||||||||||||||||||||||||||||||||
| 537 | 229 | ▶ | return unless $ref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | 92 | ▶ | if (_valid_root ($ref)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 539 | 45 | ▶ | return $ref unless wantarray; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 6 | require Storable; | |||||||||||||||||||||||||||||||||||||||||
| 541 | 6 | my $new = Storable::dclone ($ref); | |||||||||||||||||||||||||||||||||||||||||
| 542 | 6 | ▶ | return _hashref ($new) ? %$new : @$new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||||||||||||||||
| 544 | 47 | return $$ref; | |||||||||||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||
| 547 | =head2 set | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | my $success = $h -> set ($key, $value); | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | Sets the element specified by C<$key> (may be an arrayref to the element's path, as usual) to the | ||||||||||||||||||||||||||||||||||||||||||
| 552 | value C<$value>. All required intermediate arrayrefs and/or hashrefs will be created: | ||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | # starting with an empty arrayref as the data structure... | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | $h -> set ('0/hash/0', 'value'); | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | # the data structure now contains: | ||||||||||||||||||||||||||||||||||||||||||
| 559 | # [ | ||||||||||||||||||||||||||||||||||||||||||
| 560 | # { # 0 | ||||||||||||||||||||||||||||||||||||||||||
| 561 | # 'hash' => [ # 0/hash | ||||||||||||||||||||||||||||||||||||||||||
| 562 | # 'value' # 0/hash/0 | ||||||||||||||||||||||||||||||||||||||||||
| 563 | # ] | ||||||||||||||||||||||||||||||||||||||||||
| 564 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 565 | # ] | ||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | Note that in the example above the AUTO_ARRAY option is turned on. Another option that modifies the | ||||||||||||||||||||||||||||||||||||||||||
| 568 | behaviour of C<set ()> is REPLACE_LEAF. See the L<CONFIGURATION> section for a description of both | ||||||||||||||||||||||||||||||||||||||||||
| 569 | options and some example code. | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | The method will return true if the operation was successful, and false if an error occured. If | ||||||||||||||||||||||||||||||||||||||||||
| 572 | warnings are enabled (see the L<WARNINGS> section below), a warning will be printed in case of an | ||||||||||||||||||||||||||||||||||||||||||
| 573 | error. | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | If you specify an empty key or path, the value must be a hashref or arrayref and the object's data | ||||||||||||||||||||||||||||||||||||||||||
| 576 | will be set to this new data structure. | ||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | 35 | ▶ | sub set { ## no critic (Subroutines::RequireFinalReturn) - we always return, believe it or not... | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | 35 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 583 | 35 | my @path = $self -> _path ($key); | |||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | # path is empty, the root element must be changed. but only if the new value is either a | ||||||||||||||||||||||||||||||||||||||||||
| 586 | # hashref or an arrayref: | ||||||||||||||||||||||||||||||||||||||||||
| 587 | 35 | ▶ | unless (@path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 588 | 6 | ▶ | return unless _valid_root ($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 589 | 1 | $self -> {'DATA'} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 590 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | # if root is not yet set, we need to create it before we start: | ||||||||||||||||||||||||||||||||||||||||||
| 594 | 29 | ▶ | unless ($self -> {'DATA'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 595 | 4 | ▶ | $self -> {'DATA'} = ($self -> auto_array () and _number ($path [0])) ? [] : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->auto_array and _number($path[0])
MC/DC100%2/2
$self->auto_array and _number($path[0])
$self->auto_array
_number($path[0])
Truth table100%3/3
A and B
A= $self->auto_arrayB= _number($path[0])
| |||||||||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | # path is not empty, start iterating along the path, start at the root: | ||||||||||||||||||||||||||||||||||||||||||
| 599 | 29 | my $root = $self -> {'DATA'}; | |||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | # don't forget the defined, the key may be 0. test cases would catch it, though... | ||||||||||||||||||||||||||||||||||||||||||
| 602 | 29 | ▶ | while (defined (my $top = shift @path)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | # if REPLACE_LEAF is disabled, root may be something else than a hashref or an | ||||||||||||||||||||||||||||||||||||||||||
| 605 | # arrayref, print a warning and return in this case: | ||||||||||||||||||||||||||||||||||||||||||
| 606 | 84 | ▶ | unless (_valid_root ($root)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 607 | 4 | _warn 'Trying to add an element beneath a scalar value'; | |||||||||||||||||||||||||||||||||||||||||
| 608 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 609 | } | ||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||
| 611 | # path is empty, $top was the last element -> set the value: | ||||||||||||||||||||||||||||||||||||||||||
| 612 | 80 | ▶ | unless (@path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 613 | 23 | ▶ | if (_arrayref ($root)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 614 | 5 | ▶ | return unless $top = _is_number ($top); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 615 | 3 | $root -> [$top] = $value; | |||||||||||||||||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||||||||||||||||
| 617 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 618 | 18 | $root -> {$top} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||||||||||||||||
| 620 | 21 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | # path is not yet empty, search a child with the key $top in current $root: | ||||||||||||||||||||||||||||||||||||||||||
| 624 | 57 | my $child = $self -> _find_element ($root, $top); | |||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||
| 626 | # child may now be an arrayref or a hashref, in that case we can just use it as the | ||||||||||||||||||||||||||||||||||||||||||
| 627 | # new root element (skip the if block below). if child is undef it does not yet | ||||||||||||||||||||||||||||||||||||||||||
| 628 | # exist and we need to create it. in all other cases, ie. it exists but is not an | ||||||||||||||||||||||||||||||||||||||||||
| 629 | # array- or a hashref, we override it with an array- or hashref if the REPLACE_LEAF | ||||||||||||||||||||||||||||||||||||||||||
| 630 | # option is set. | ||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||
| 632 | 57 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $child or $self->replace_leaf and not _valid_root($child)
$self->replace_leaf and not _valid_root($child)
MC/DC100%3/3
not $child or $self->replace_leaf and not _valid_root($child)
not $child
$self->replace_leaf
not _valid_root($child)
Truth table100%4/4
not(A) or B and not(C)
A= not $childB= $self->replace_leafC= not _valid_root($child)
| |||||||||||||||||||||||||||||||||||||||||||
| 633 | not $child or # child doesn't exist | ||||||||||||||||||||||||||||||||||||||||||
| 634 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 635 | $self -> replace_leaf () and # REPLACE_LEAF is set | ||||||||||||||||||||||||||||||||||||||||||
| 636 | not _valid_root ($child) # and it's no hash- or arrayref | ||||||||||||||||||||||||||||||||||||||||||
| 637 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 638 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | 22 | ▶ | if (_arrayref ($root)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||
| 643 | # important: _find_element will also return false if the root is an | ||||||||||||||||||||||||||||||||||||||||||
| 644 | # array and the key is no valid number, we need to check this: | ||||||||||||||||||||||||||||||||||||||||||
| 645 | 8 | ▶ | return unless $top = _number ($top); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||
| 647 | # note that the type of the child (ie. arrayref or hashref) depends | ||||||||||||||||||||||||||||||||||||||||||
| 648 | # on the AUTO_ARRAY setting and the value of the next key: | ||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||
| 650 | 6 | ▶ | if ($self -> auto_array () and _number ($path [0])) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->auto_array and _number($path[0])
MC/DC100%2/2
$self->auto_array and _number($path[0])
$self->auto_array
_number($path[0])
Truth table100%3/3
A and B
A= $self->auto_arrayB= _number($path[0])
| |||||||||||||||||||||||||||||||||||||||||||
| 651 | 1 | $child = $root -> [$top] = []; | |||||||||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||||||||||
| 653 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 654 | 5 | $child = $root -> [$top] = {}; | |||||||||||||||||||||||||||||||||||||||||
| 655 | } | ||||||||||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||||||||||
| 658 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||
| 660 | 14 | ▶ | if ($self -> auto_array () and _number ($path [0])) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->auto_array and _number($path[0])
MC/DC100%2/2
$self->auto_array and _number($path[0])
$self->auto_array
_number($path[0])
Truth table100%3/3
A and B
A= $self->auto_arrayB= _number($path[0])
| |||||||||||||||||||||||||||||||||||||||||||
| 661 | 3 | $child = $root -> {$top} = []; | |||||||||||||||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||||||||||||||||
| 663 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 664 | 11 | $child = $root -> {$top} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | } | ||||||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||||||
| 669 | } | ||||||||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||||||
| 671 | # at this point $child can be used as the next root element, if it is no arrayref | ||||||||||||||||||||||||||||||||||||||||||
| 672 | # or hashref we exit at the start of the next loop. | ||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||
| 674 | 55 | $root = $child; | |||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||
| 676 | } | ||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||
| 680 | =head2 remove | ||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||
| 682 | my $removed = $h -> remove ($key); | ||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||
| 684 | Deletes the element specified by the key C<$key> (you may also specify an arrayref containing the | ||||||||||||||||||||||||||||||||||||||||||
| 685 | element's path in the data structure, usage of the key is recommended, though). The value of the | ||||||||||||||||||||||||||||||||||||||||||
| 686 | removed element will be returned. If the element does not exist, C<undef> will be returned. If the | ||||||||||||||||||||||||||||||||||||||||||
| 687 | key (or path) is empty, the root reference will be returned and the data structure will be removed | ||||||||||||||||||||||||||||||||||||||||||
| 688 | from the object. | ||||||||||||||||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||||||||||||||||
| 690 | This function basically works like Perl's C<delete ()> function for hashes and like the | ||||||||||||||||||||||||||||||||||||||||||
| 691 | C<splice ()> function for arrays (removing one element and not adding anything to the array, of | ||||||||||||||||||||||||||||||||||||||||||
| 692 | course). | ||||||||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||||||||
| 694 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||
| 696 | 16 | ▶ | sub remove { ## no critic (Subroutines::RequireFinalReturn) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 697 | 16 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 698 | 16 | my @path = $self -> _path ($key); | |||||||||||||||||||||||||||||||||||||||||
| 699 | 16 | my $root = $self -> {'DATA'}; | |||||||||||||||||||||||||||||||||||||||||
| 700 | 16 | ▶ | unless (@path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 701 | 2 | $self -> {'DATA'} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 702 | 2 | return $root; | |||||||||||||||||||||||||||||||||||||||||
| 703 | } | ||||||||||||||||||||||||||||||||||||||||||
| 704 | 14 | ▶ | while (defined (my $top = shift @path)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 705 | 27 | ▶ | unless (@path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 706 | 13 | ▶ | if (_hashref ($root)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 707 | 5 | return delete $root -> {$top}; | |||||||||||||||||||||||||||||||||||||||||
| 708 | } | ||||||||||||||||||||||||||||||||||||||||||
| 709 | elsif (_arrayref ($root)) { | ||||||||||||||||||||||||||||||||||||||||||
| 710 | 7 | ▶ | return unless $top = _is_number ($top); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 711 | 5 | ▶ | return splice (@$root, $top, 1) if @$root > $top; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||||||||||||
| 713 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 714 | } | ||||||||||||||||||||||||||||||||||||||||||
| 715 | 14 | ▶ | return unless $root = $self -> _find_element ($root, $top); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 716 | } | ||||||||||||||||||||||||||||||||||||||||||
| 717 | } | ||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||
| 719 | =head2 path | ||||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||
| 721 | my @path = $h -> path ($key); | ||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||
| 723 | Returns an array containing the path elements for the specified key C<$key>, ie. the normalized key | ||||||||||||||||||||||||||||||||||||||||||
| 724 | (see C<normalize_key ()> below) split at the separator. Note that the resulting array may be empty. | ||||||||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||||||||
| 726 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||
| 728 | 303 | ▶ | sub path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 729 | 303 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 730 | 303 | $key = $self -> normalize_key ($key); | |||||||||||||||||||||||||||||||||||||||||
| 731 | 303 | my $s = $self -> {'SEPARATOR'}; | |||||||||||||||||||||||||||||||||||||||||
| 732 | 303 | return split /\Q$s\E/, $key; | |||||||||||||||||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||||||
| 735 | =head2 key | ||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||
| 737 | my $key = $h -> key (@path); | ||||||||||||||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||||||||||||
| 739 | Joins the array with the current separator string and returns the resulting string. The example | ||||||||||||||||||||||||||||||||||||||||||
| 740 | above can be written as: | ||||||||||||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||||||||||||
| 742 | my $key = join $h -> separator (), @path; | ||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||
| 744 | Additionally, you may use this function with an arrayref, the following will return the same | ||||||||||||||||||||||||||||||||||||||||||
| 745 | string as the first example: | ||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||
| 747 | my $key = $h -> key (\@path); | ||||||||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||||||||
| 749 | Note that - unlike the C<path ()> function - no further processing is done. For example, if the | ||||||||||||||||||||||||||||||||||||||||||
| 750 | array contains empty strings, the resulting string will contain multiple consecutive separators. | ||||||||||||||||||||||||||||||||||||||||||
| 751 | Use C<normalize_key ()> to remove these if required. | ||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||
| 753 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||||||
| 755 | 112 | ▶ | sub key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
key: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 756 | 112 | my ($self, @path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 757 | 112 | ▶ | @path = @{$path [0]} if _arrayref ($path [0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 758 | 112 | return join $self -> {'SEPARATOR'}, @path; | |||||||||||||||||||||||||||||||||||||||||
| 759 | } | ||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||
| 761 | =head2 normalize_key | ||||||||||||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||||||||||||
| 763 | $key = $h -> normalize_key ($key); | ||||||||||||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||||||||||||
| 765 | Removes separator string(s) at the beginning and end of the specified key and replaces all | ||||||||||||||||||||||||||||||||||||||||||
| 766 | occurrences of multiple consecutive separator strings in the key with a single one. For example, | ||||||||||||||||||||||||||||||||||||||||||
| 767 | the normalized version of C</some//path//> (with the separator C</>) would be C<some/path>. | ||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||
| 769 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||
| 771 | 424 | ▶ | sub normalize_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
normalize_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 772 | 424 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 773 | 424 | my $s = $self -> {'SEPARATOR'}; | |||||||||||||||||||||||||||||||||||||||||
| 774 | 424 | $key =~ s{^(?:\Q$s\E)*(.*?)(?:\Q$s\E)*$}{$1}; | |||||||||||||||||||||||||||||||||||||||||
| 775 | 424 | $key =~ s{(?:\Q$s\E)+}{$s}g; | |||||||||||||||||||||||||||||||||||||||||
| 776 | 424 | return $key; | |||||||||||||||||||||||||||||||||||||||||
| 777 | } | ||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||
| 779 | # _path ($key_or_path) - get path array | ||||||||||||||||||||||||||||||||||||||||||
| 780 | # $key_or_path if this is an array reference, the array will be returned, if not, it is | ||||||||||||||||||||||||||||||||||||||||||
| 781 | # assumed to be a scalar, split at the separator and the array is returned | ||||||||||||||||||||||||||||||||||||||||||
| 782 | # some minor improvement: if an arrayref is used, the array will be cleaned of all invalid | ||||||||||||||||||||||||||||||||||||||||||
| 783 | # elements, ie. only non-empty scalar values will be returned. | ||||||||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||||||||
| 785 | 429 | ▶ | sub _path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_path: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 786 | 429 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 787 | 429 | ▶ | if ($key and _arrayref ($key)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key and _arrayref($key)
MC/DC100%2/2
$key and _arrayref($key)
$key
_arrayref($key)
Truth table100%3/3
A and B
A= $keyB= _arrayref($key)
| |||||||||||||||||||||||||||||||||||||||||||
| 788 | 178 | my @new; | |||||||||||||||||||||||||||||||||||||||||
| 789 | 178 | foreach (@$key) { | |||||||||||||||||||||||||||||||||||||||||
| 790 | 433 | ▶ | push @new, $_ if defined $_ and not ref $_ and $_ ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $_ and not ref $_ and $_ ne ""
defined $_ and not ref $_
MC/DC100%3/3
defined $_ and not ref $_ and $_ ne ""
defined $_
not ref $_
$_ ne ""
Truth table100%4/4
A and not(B) and C
A= defined $_B= not ref $_C= $_ ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 791 | } | ||||||||||||||||||||||||||||||||||||||||||
| 792 | 178 | return @new; | |||||||||||||||||||||||||||||||||||||||||
| 793 | } | ||||||||||||||||||||||||||||||||||||||||||
| 794 | 251 | return $self -> path ($key); | |||||||||||||||||||||||||||||||||||||||||
| 795 | } | ||||||||||||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||||||||||||
| 797 | =head1 WARNINGS | ||||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||
| 799 | C<Data::SimplePath> can print warnings if something is wrong, eg. if you try to access an array | ||||||||||||||||||||||||||||||||||||||||||
| 800 | element with a non-numeric key or if you call the C<new ()> function with C<$initial> being not a | ||||||||||||||||||||||||||||||||||||||||||
| 801 | hashref or arrayref. If you enable warnings (ie. C<use warnings;>) these warnings will be enabled, | ||||||||||||||||||||||||||||||||||||||||||
| 802 | too. You may use the C<use warnings 'Data::SimplePath';> command to enable only the warnings of | ||||||||||||||||||||||||||||||||||||||||||
| 803 | this module, and if you want to enable warnings in general but disable C<Data::SimplePath>'s ones, | ||||||||||||||||||||||||||||||||||||||||||
| 804 | use C<no warnings 'Data::SimplePath';>. | ||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||
| 806 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||
| 808 | Stefan Goebel | ||||||||||||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||||||||||||
| 810 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||||||||||||
| 812 | Copyright (C) 2009 - 2013 Stefan Goebel, all rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||
| 814 | This program is free software; you can redistribute it and/or modify it under the same terms as | ||||||||||||||||||||||||||||||||||||||||||
| 815 | Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||
| 817 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||||||||||
| 819 | 16 | 1; | |||||||||||||||||||||||||||||||||||||||||