| 1 | package Rose::DB::Object::Metadata::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 61 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 61 | ▶ | use Carp(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 61 | require Exporter; | |||||||||||||||||||||||||||
| 8 | 61 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 61 | our @EXPORT_OK = | |||||||||||||||||||||||||||
| 11 | qw(perl_hashref perl_arrayref perl_quote_key perl_quote_value | ||||||||||||||||||||||||||||
| 12 | hash_key_padding); | ||||||||||||||||||||||||||||
| 13 | 61 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 61 | our $DEFAULT_PERL_INDENT = 4; | |||||||||||||||||||||||||||
| 16 | 61 | our $DEFAULT_PERL_BRACES = 'k&r'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 61 | our $VERSION = '0.817'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub perl_hashref | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
perl_hashref: not called CC=20 SCAR=60.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my(%args) = (@_ == 1 ? (hash => $_[0]) : @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | my $inline = defined $args{'inline'} ? $args{'inline'} : ($args{'inline'} = 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my $indent = defined $args{'indent'} ? $args{'indent'} : ($args{'indent'} = $DEFAULT_PERL_INDENT); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my $braces = defined $args{'braces'} ? $args{'braces'} : ($args{'braces'} = $DEFAULT_PERL_BRACES); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $level = defined $args{'level'} ? $args{'level'} : ($args{'level'} = 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | my $no_curlies = delete $args{'no_curlies'}; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | my $key_padding = $args{'key_padding'} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'key_padding'} || 0
MC/DC0%0/1
$args{'key_padding'} || 0
$args{'key_padding'}
Truth table0%0/2
A
A= $args{'key_padding'}
| |||||||||||||||||||||||||||||
| 30 | 0 | my $inline_limit = $args{'inline_limit'}; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $sort_keys = $args{'sort_keys'} || sub { lc $_[0] cmp lc $_[1] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$args{'sort_keys'} || sub {
lc $_[0] cmp lc $_[1];
}
MC/DC0%0/2
$args{'sort_keys'} || sub {
lc $_[0] cmp lc $_[1];
}
$args{'sort_keys'}
sub {
lc $_[0] cmp lc $_[1];
}
Truth table0%0/3
A || B
A= $args{'sort_keys'}B= sub {
lc $_[0] cmp lc $_[1];
}
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | my $hash = delete $args{'hash'}; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | my $indent_txt = ' ' x ($indent * ($level + 1)); | |||||||||||||||||||||||||||
| 37 | 0 | my $sub_indent = ' ' x ($indent * $level); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my @pairs; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | foreach my $key (sort { $sort_keys->($a, $b) } keys %$hash) | |||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||
| 43 | push(@pairs, sprintf('%-*s => ', $key_padding, perl_quote_key($key)) . | ||||||||||||||||||||||||||||
| 44 | 0 | perl_value(value => $hash->{$key}, %args)); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | my($inline_perl, $perl); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $inline_perl = ($no_curlies ? '' : '{ ') . join(', ', @pairs) . ($no_curlies ? '' : ' }'); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if($braces eq 'bsd') | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $perl = "\n${sub_indent}" . ($no_curlies ? '' : "{\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | elsif($braces eq 'k&r') | ||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $perl = "{\n" unless($no_curlies); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else | ||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | Carp::croak 'Invalid ', (defined $args{'braces'} ? '' : 'default '), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | "brace style: '$braces'"; | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $perl .= join(",\n", map { "$indent_txt$_" } @pairs) . ',' . | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | ($no_curlies ? '' : "\n$sub_indent}"); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if(defined $inline_limit && length($inline_perl) > $inline_limit) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $inline_limit and length $inline_perl > $inline_limit
MC/DC0%0/2
defined $inline_limit and length $inline_perl > $inline_limit
defined $inline_limit
length $inline_perl > $inline_limit
Truth table0%0/3
A and B
A= defined $inline_limitB= length $inline_perl > $inline_limit
| |||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | 0 | return $perl; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | return $inline ? $inline_perl : $perl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub perl_arrayref | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
perl_arrayref: not called CC=15 SCAR=54.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my(%args) = (@_ == 1 ? (array => $_[0]) : @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $inline = defined $args{'inline'} ? $args{'inline'} : ($args{'inline'} = 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $indent = defined $args{'indent'} ? $args{'indent'} : ($args{'indent'} = $DEFAULT_PERL_INDENT); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $braces = defined $args{'braces'} ? $args{'braces'} : ($args{'braces'} = $DEFAULT_PERL_BRACES); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $level = defined $args{'level'} ? $args{'level'} : ($args{'level'} = 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $key_padding = $args{'key_padding'} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'key_padding'} || 0
MC/DC0%0/1
$args{'key_padding'} || 0
$args{'key_padding'}
Truth table0%0/2
A
A= $args{'key_padding'}
| |||||||||||||||||||||||||||||
| 85 | 0 | my $inline_limit = $args{'inline_limit'}; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $sort_keys = $args{'sort_keys'} || sub { lc $_[0] cmp lc $_[1] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$args{'sort_keys'} || sub {
lc $_[0] cmp lc $_[1];
}
MC/DC0%0/2
$args{'sort_keys'} || sub {
lc $_[0] cmp lc $_[1];
}
$args{'sort_keys'}
sub {
lc $_[0] cmp lc $_[1];
}
Truth table0%0/3
A || B
A= $args{'sort_keys'}B= sub {
lc $_[0] cmp lc $_[1];
}
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $array = delete $args{'array'}; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $indent_txt = ' ' x ($indent * ($level + 1)); | |||||||||||||||||||||||||||
| 92 | 0 | my $sub_indent = ' ' x ($indent * $level); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my @items; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | foreach my $item (@$array) | |||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 0 | push(@items, perl_value(value => $item, %args)); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my($inline_perl, $perl); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | $inline_perl = '[ ' . join(', ', @items) . ' ]'; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if($braces eq 'bsd') | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 0 | $perl = "\n${sub_indent}\[\n"; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | elsif($braces eq 'k&r') | ||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 0 | $perl = "[\n"; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | else | ||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | Carp::croak 'Invalid ', (defined $args{'braces'} ? '' : 'default '), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | "brace style: '$braces'"; | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | $perl .= join(",\n", map { "$indent_txt$_" } @items) . ",\n$sub_indent]"; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if(defined $inline_limit && length($inline_perl) > $inline_limit) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $inline_limit and length $inline_perl > $inline_limit
MC/DC0%0/2
defined $inline_limit and length $inline_perl > $inline_limit
defined $inline_limit
length $inline_perl > $inline_limit
Truth table0%0/3
A and B
A= defined $inline_limitB= length $inline_perl > $inline_limit
| |||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||
| 123 | 0 | return $perl; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return $inline ? $inline_perl : $perl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub perl_value | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
perl_value: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | my(%args) = (@_ == 1 ? (value => $_[0]) : @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my $value = delete $args{'value'}; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | $args{'level'}++; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if(my $ref = ref $value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if($ref eq 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||
| 141 | 0 | return perl_arrayref(array => $value, %args); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | elsif($ref eq 'HASH') | ||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 0 | $args{'key_padding'} = hash_key_padding($value); | |||||||||||||||||||||||||||
| 146 | 0 | delete $args{'inline'}; | |||||||||||||||||||||||||||
| 147 | 0 | return perl_hashref(hash => $value, %args); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | else | ||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 0 | return $value; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | return perl_quote_value($value) | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | sub hash_key_padding | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
hash_key_padding: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 160 | 0 | my($hash) = shift; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | my $max_len = 0; | |||||||||||||||||||||||||||
| 163 | 0 | my $min_len = -1; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | foreach my $name (keys %$hash) | |||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | $max_len = length($name) if(length $name > $max_len); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $min_len = length($name) if(length $name < $min_len || $min_len < 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $name < $min_len or $min_len < 0
MC/DC0%0/2
length $name < $min_len or $min_len < 0
length $name < $min_len
$min_len < 0
Truth table0%0/3
A or B
A= length $name < $min_lenB= $min_len < 0
| |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | return $max_len; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub perl_quote_key | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
perl_quote_key: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | 0 | my($key) = shift; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | return $key if($key =~ /^\d+$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | for($key) | |||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||
| 182 | 0 | ▶ | s/'/\\'/g if(/'/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | $_ = "'$_'" if(/\W/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | return $key; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | sub perl_quote_value | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
perl_quote_value: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 191 | 0 | my($val) = shift; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | for($val) | |||||||||||||||||||||||||||
| 194 | { | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | s/'/\\'/g if(/'/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | $_ = "'$_'" unless(/^(?:[1-9]\d*\.?\d*|\.\d+)$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | return $val; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 61 | 1; | |||||||||||||||||||||||||||