| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Config::Neat::Util - Common utility functions for other Config::Neat modules | ||
| 4 | |||
| 5 | =head1 COPYRIGHT | ||
| 6 | |||
| 7 | Copyright (C) 2012-2015 Igor Afanasyev <igor.afanasyev@gmail.com> | ||
| 8 | |||
| 9 | =head1 SEE ALSO | ||
| 10 | |||
| 11 | L<https://github.com/iafan/Config-Neat> | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | package Config::Neat::Util; | ||
| 16 | |||
| 17 | our $VERSION = '1.4'; | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | |||
| 21 | use Tie::IxHash; | ||
| 22 | |||
| 23 | our @ISA = qw(Exporter); | ||
| 24 | |||
| 25 | our @EXPORT_OK = qw( | ||
| 26 | new_ixhash | ||
| 27 | to_ixhash | ||
| 28 | to_ixhash_recursive | ||
| 29 | is_number | ||
| 30 | is_code | ||
| 31 | is_hash | ||
| 32 | is_ixhash | ||
| 33 | is_any_hash | ||
| 34 | is_array | ||
| 35 | is_neat_array | ||
| 36 | is_any_array | ||
| 37 | is_scalar | ||
| 38 | is_simple_array | ||
| 39 | is_homogenous_simple_array | ||
| 40 | hash_has_only_sequential_keys | ||
| 41 | hash_has_sequential_keys | ||
| 42 | get_next_auto_key | ||
| 43 | offset_keys | ||
| 44 | get_keys_in_order | ||
| 45 | reorder_ixhash_numerically | ||
| 46 | reorder_ixhash | ||
| 47 | rename_ixhash_key | ||
| 48 | read_file | ||
| 49 | ); | ||
| 50 | |||
| 51 | sub new_ixhash { | ||
| 52 | my $new = {}; | ||
| 53 | tie(%$new, 'Tie::IxHash'); | ||
| 54 | return $new; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub to_ixhash { | ||
| 58 | my $node = shift; | ||
| 59 | die "Not a regular hash" unless is_hash($node) && !is_ixhash($node); | ||
| 60 | my $new = new_ixhash; | ||
| 61 | map { $new->{$_} = $node->{$_} } keys %$node; | ||
| 62 | return $new; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub to_ixhash_recursive { | ||
| 66 | my ($node) = @_; | ||
| 67 | my $result = is_hash($node) && !is_ixhash($node) ? to_ixhash($node) : $node; | ||
| 68 | if (is_ixhash($result)) { | ||
| 69 | map { | ||
| 70 | $result->{$_} = to_ixhash_recursive($result->{$_}); | ||
| 71 | } keys %$result; | ||
| 72 | } | ||
| 73 | return $result; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub is_number { | ||
| 77 | my $n = shift; | ||
| 78 | return defined $n && $n =~ m/^\d+$/; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub is_code { | ||
| 82 | my $node = shift; | ||
| 83 | return ref($node) eq 'CODE'; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub is_hash { | ||
| 87 | my $node = shift; | ||
| 88 | return ref($node) eq 'HASH'; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub is_ixhash { | ||
| 92 | my $node = shift; | ||
| 93 | return undef unless is_hash($node); | ||
| 94 | return ref(tied(%$node)) eq 'Tie::IxHash'; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub is_any_hash { | ||
| 98 | my $node = shift; | ||
| 99 | return is_hash($node) || is_ixhash($node); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub is_array { | ||
| 103 | my $node = shift; | ||
| 104 | return ref($node) eq 'ARRAY'; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub is_neat_array { | ||
| 108 | my $node = shift; | ||
| 109 | return ref($node) eq 'Config::Neat::Array'; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub is_any_array { | ||
| 113 | my $node = shift; | ||
| 114 | return is_array($node) || is_neat_array($node); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub is_scalar { | ||
| 118 | my $node = shift; | ||
| 119 | return (ref(\$node) eq 'SCALAR') or (ref($node) eq 'SCALAR'); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub is_simple_array { | ||
| 123 | my $node = shift; | ||
| 124 | |||
| 125 | return 1 if is_scalar($node); | ||
| 126 | return undef unless is_array($node) || is_neat_array($node); | ||
| 127 | |||
| 128 | foreach my $value (@$node) { | ||
| 129 | return undef unless is_scalar($value); | ||
| 130 | } | ||
| 131 | return 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub is_homogenous_simple_array { | ||
| 135 | my $node = shift; | ||
| 136 | |||
| 137 | return 1 if is_scalar($node); | ||
| 138 | return undef unless is_array($node) || is_neat_array($node); | ||
| 139 | |||
| 140 | my $contains_hash = undef; | ||
| 141 | my $contains_array = undef; | ||
| 142 | my $contains_scalar = undef; | ||
| 143 | |||
| 144 | foreach my $value (@$node) { | ||
| 145 | if (is_hash($value)) { | ||
| 146 | $contains_hash |= 1; | ||
| 147 | } elsif (is_any_array($value)) { | ||
| 148 | $contains_array |= 1; | ||
| 149 | } else { | ||
| 150 | $contains_scalar |= is_scalar($value); | ||
| 151 | } | ||
| 152 | die "Mixing hashes with simple arrays/scalars within one node is not supported" if $contains_hash && $contains_scalar; | ||
| 153 | } | ||
| 154 | return $contains_scalar && !$contains_array; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub hash_has_only_sequential_keys { | ||
| 158 | my $node = shift; | ||
| 159 | return hash_has_sequential_keys($node, 1); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub hash_has_sequential_keys { | ||
| 163 | my ($node, $strict) = @_; | ||
| 164 | die "Not a hash" unless is_hash($node); | ||
| 165 | |||
| 166 | my $i = 0; | ||
| 167 | map { | ||
| 168 | if (is_number($_)) { | ||
| 169 | return undef if $_ != $i; | ||
| 170 | $i++; | ||
| 171 | } else { | ||
| 172 | return undef if $strict; | ||
| 173 | } | ||
| 174 | } keys %$node; | ||
| 175 | return 1; | ||
| 176 | } | ||
| 177 | |||
| 178 | # supposed to be used against hash that matches the | ||
| 179 | # `hash_has_sequential_keys() == true` criterion | ||
| 180 | sub get_next_auto_key { | ||
| 181 | my $node = shift; | ||
| 182 | die "Not a hash" unless is_hash($node); | ||
| 183 | |||
| 184 | # get max(key) | ||
| 185 | my $i = -1; # so that next key will start with 0 | ||
| 186 | map { | ||
| 187 | $i = $_ if is_number($_) && $_ > $i; | ||
| 188 | } keys %$node; | ||
| 189 | |||
| 190 | # return max + 1 | ||
| 191 | return $i + 1; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub offset_keys { | ||
| 195 | my ($node, $offset) = @_; | ||
| 196 | die "Not a Tie::IxHash" unless is_ixhash($node); | ||
| 197 | return $node if $offset == 0; | ||
| 198 | |||
| 199 | my $result = new_ixhash; | ||
| 200 | |||
| 201 | # remap keys | ||
| 202 | map { | ||
| 203 | if (is_number($_)) { | ||
| 204 | $result->{$_ + $offset} = $node->{$_}; | ||
| 205 | } else { | ||
| 206 | $result->{$_} = $node->{$_}; | ||
| 207 | } | ||
| 208 | } keys %$node; | ||
| 209 | |||
| 210 | return $result; | ||
| 211 | } | ||
| 212 | |||
| 213 | # accepts an array of hasrefs | ||
| 214 | sub get_keys_in_order { | ||
| 215 | my $result = new_ixhash; | ||
| 216 | |||
| 217 | map { | ||
| 218 | map { | ||
| 219 | $result->{$_} = 1; | ||
| 220 | } keys %$_; | ||
| 221 | } @_; | ||
| 222 | |||
| 223 | return keys %$result; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub reorder_ixhash_numerically { | ||
| 227 | my ($node) = @_; | ||
| 228 | die "Not a Tie::IxHash" unless is_ixhash($node); | ||
| 229 | |||
| 230 | # sort keys numerically | ||
| 231 | my @a = sort {$a <=> $b} keys %$node; | ||
| 232 | |||
| 233 | return reorder_ixhash($node, \@a); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub reorder_ixhash { | ||
| 237 | my ($node, $keysref) = @_; | ||
| 238 | die "Not a Tie::IxHash" unless is_ixhash($node); | ||
| 239 | |||
| 240 | my $result = new_ixhash; | ||
| 241 | map { $result->{$_} = $node->{$_} if exists $node->{$_} } @$keysref; | ||
| 242 | |||
| 243 | return $result; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub rename_ixhash_key { | ||
| 247 | my ($node, $from, $to) = @_; | ||
| 248 | die "Not a Tie::IxHash" unless is_ixhash($node); | ||
| 249 | die "Can\'t rename key '$from' to '$to', because the target key already exists" if exists $node->{$to}; | ||
| 250 | |||
| 251 | my $result = new_ixhash; | ||
| 252 | map { | ||
| 253 | my $key = $_ eq $from ? $to : $_; | ||
| 254 | $result->{$key} = $node->{$_}; | ||
| 255 | } keys %$node; | ||
| 256 | |||
| 257 | return $result; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub read_file { | ||
| 261 | my ($filename, $binmode) = @_; | ||
| 262 | |||
| 263 | open(CFG, $filename) or die "Can't open [$filename]: $!"; | ||
| 264 | binmode(CFG, $binmode || ':utf8'); | ||
| 265 | my $text = join('', <CFG>); | ||
| 266 | close(CFG); | ||
| 267 | |||
| 268 | return $text; | ||
| 269 | } # end sub | ||
| 270 | |||
| 271 | |||
| 272 | 1; |