line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Params::Style; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: Style.pm,v 1.3 2005/11/29 14:36:10 mrodrigu Exp $ |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
69042
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
79
|
|
6
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
57
|
|
7
|
2
|
|
|
2
|
|
10
|
use Carp; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
205
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require Exporter; |
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
10
|
use vars qw( @ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION); |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
484
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# This allows declaration use Params::Style ':all'; |
16
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => [ qw( &perl_style_params &javaStyleParams &JavaStyleParams &nostyleparams &replace_keys) ] ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} }, &replace_keys, |
19
|
|
|
|
|
|
|
&perl_style_params, &javaStyleParams, &JavaStyleParams, &nostyleparams |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$VERSION = '0.06'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my( $uc, $UC); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
BEGIN |
28
|
2
|
50
|
|
2
|
|
3029
|
{ if( $] >= 5.007) { $uc = qr/\p{UppercaseLetter}/; |
|
2
|
|
|
2
|
|
21
|
|
|
2
|
|
|
|
|
207
|
|
|
2
|
|
|
|
|
63059
|
|
|
2
|
|
|
|
|
11
|
|
29
|
2
|
|
|
|
|
1933
|
$UC = qr/\P{UppercaseLetter}/; |
30
|
|
|
|
|
|
|
} |
31
|
0
|
|
|
|
|
0
|
else { $uc= qr/[A-Z]/; |
32
|
0
|
|
|
|
|
0
|
$UC= qr/[^A-Z]/; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
################################################################ |
37
|
|
|
|
|
|
|
# # |
38
|
|
|
|
|
|
|
# functional interface # |
39
|
|
|
|
|
|
|
# # |
40
|
|
|
|
|
|
|
################################################################ |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub replace_keys |
43
|
50
|
|
|
50
|
1
|
5666
|
{ my $replace_keys = shift; # sub call to use on each key |
44
|
|
|
|
|
|
|
|
45
|
50
|
100
|
|
|
|
111
|
if( @_ == 1) |
46
|
|
|
|
|
|
|
{ # should be a reference |
47
|
9
|
|
|
|
|
13
|
my $options= shift; |
48
|
9
|
100
|
|
|
|
47
|
if( UNIVERSAL::isa( $options, 'ARRAY')) |
|
|
100
|
|
|
|
|
|
49
|
4
|
100
|
|
|
|
16
|
{ if( scalar @$options % 2) { _croak_odd_arg_nb(); } |
|
1
|
|
|
|
|
2
|
|
50
|
3
|
|
|
|
|
6
|
my @options; my $flip= 0; |
|
3
|
|
|
|
|
4
|
|
51
|
3
|
|
|
|
|
8
|
foreach (@$options) |
52
|
12
|
100
|
|
|
|
22
|
{ if( $flip=1-$flip) { push @options, $replace_keys->( $_); } |
|
6
|
|
|
|
|
17
|
|
53
|
6
|
|
|
|
|
16
|
else { push @options, $_; } |
54
|
|
|
|
|
|
|
} |
55
|
3
|
|
|
|
|
12
|
return \@options; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif( UNIVERSAL::isa( $options, 'HASH')) |
58
|
3
|
|
|
|
|
9
|
{ my %options= map { $replace_keys->($_) => $options->{$_} } keys %$options; |
|
6
|
|
|
|
|
33
|
|
59
|
3
|
|
|
|
|
11
|
return \%options; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else |
62
|
2
|
|
|
|
|
6
|
{ _croak_wrong_arg_type( ref $options); } |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else |
66
|
41
|
100
|
|
|
|
101
|
{ if( scalar @_ % 2) { _croak_odd_arg_nb(); } |
|
1
|
|
|
|
|
4
|
|
67
|
40
|
|
|
|
|
37
|
my @options; |
68
|
40
|
|
|
|
|
99
|
while( my $key= shift @_) { push @options, $replace_keys->( $key), shift( @_); } |
|
34
|
|
|
|
|
71
|
|
69
|
40
|
|
|
|
|
271
|
return @options; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
18
|
|
|
18
|
1
|
18685
|
sub perl_style_params { return replace_keys( \&perl_style, @_); } |
74
|
14
|
|
|
14
|
1
|
80
|
sub javaStyleParams { return replace_keys( \&javaStyle, @_); } |
75
|
2
|
|
|
2
|
1
|
7
|
sub JavaStyleParams { return replace_keys( \&JavaStyle, @_); } |
76
|
2
|
|
|
2
|
1
|
38
|
sub nostyleparams { return replace_keys( \&nostyle, @_); } |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
################################################################ |
80
|
|
|
|
|
|
|
# # |
81
|
|
|
|
|
|
|
# basic replace style # |
82
|
|
|
|
|
|
|
# # |
83
|
|
|
|
|
|
|
################################################################ |
84
|
|
|
|
|
|
|
|
85
|
17
|
|
|
17
|
1
|
20
|
sub javaStyle { my $name= shift; $name=~ s{_(\w)}{\U$1}g; return $name; } |
|
17
|
|
|
|
|
366
|
|
|
17
|
|
|
|
|
71
|
|
86
|
4
|
|
|
4
|
1
|
4
|
sub JavaStyle { my $name= shift; $name=~ s{(?:_|^)(\w)}{\U$1}g; return $name; } |
|
4
|
|
|
|
|
26
|
|
|
4
|
|
|
|
|
16
|
|
87
|
8
|
|
|
8
|
1
|
13
|
sub nostyle { my $name= lc shift; $name=~ s{_}{}g; return $name; } |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
34
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub perl_style |
90
|
95
|
|
|
95
|
1
|
15864
|
{ my $name= shift; |
91
|
95
|
100
|
|
|
|
327
|
return $name if( $name=~ m{_}); |
92
|
62
|
|
|
|
|
716
|
$name=~ s{(?
|
93
|
62
|
|
|
|
|
2496
|
$name=~ s{(?
|
94
|
62
|
|
|
|
|
890
|
$name=~ s{_($uc)$}{_\L$1}; |
95
|
62
|
|
|
|
|
257
|
$name=~ s{^_}{}; |
96
|
62
|
|
|
|
|
297
|
return $name; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _croak_odd_arg_nb |
100
|
2
|
|
|
2
|
|
9
|
{ my $pn_sub= (caller(2))[3]; |
101
|
2
|
|
|
|
|
5
|
my ($package, $filename, $line)= (caller(3))[0..2]; |
102
|
2
|
|
50
|
|
|
9
|
$filename||= ''; $line ||= ''; |
|
2
|
|
50
|
|
|
7
|
|
103
|
2
|
|
|
|
|
11
|
die "odd number of arguments passed to $pn_sub at $filename line $line\n"; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub _croak_wrong_arg_type |
107
|
2
|
|
|
2
|
|
5
|
{ my $type= shift; |
108
|
2
|
|
|
|
|
8
|
my $pn_sub= (caller(2))[3]; |
109
|
2
|
|
|
|
|
7
|
my ($package, $filename, $line)= (caller(3))[0..2]; |
110
|
2
|
|
50
|
|
|
10
|
$filename||= ''; $line ||= ''; |
|
2
|
|
50
|
|
|
6
|
|
111
|
2
|
|
|
|
|
15
|
die "wrong arguments type $type passed to $pn_sub at $filename line $line ", |
112
|
|
|
|
|
|
|
"should be hash, hashref, array or array ref\n"; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
################################################################ |
116
|
|
|
|
|
|
|
# # |
117
|
|
|
|
|
|
|
# tied hash interface # |
118
|
|
|
|
|
|
|
# # |
119
|
|
|
|
|
|
|
################################################################ |
120
|
|
|
|
|
|
|
|
121
|
2
|
|
|
2
|
|
2394
|
use Attribute::Handlers autotie => { '__CALLER__::ParamsStyle' => __PACKAGE__ }; |
|
2
|
|
|
|
|
18011
|
|
|
2
|
|
|
|
|
17
|
|
122
|
|
|
|
|
|
|
|
123
|
2
|
|
|
2
|
|
2617
|
use vars qw(@ISA); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
89
|
|
124
|
2
|
|
|
2
|
|
2183
|
use Tie::Hash; |
|
2
|
|
|
|
|
1919
|
|
|
2
|
|
|
|
|
606
|
|
125
|
|
|
|
|
|
|
unshift @ISA, 'Tie::ExtraHash'; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my %replace_func= ( perl_style => \&Params::Style::perl_style, |
128
|
|
|
|
|
|
|
javaStyle => \&Params::Style::javaStyle, |
129
|
|
|
|
|
|
|
JavaStyle => \&Params::Style::JavaStyle, |
130
|
|
|
|
|
|
|
nostyle => \&Params::Style::nostyle, |
131
|
|
|
|
|
|
|
); |
132
|
|
|
|
|
|
|
my $DEFAULT_STYLE= 'perl_style'; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub TIEHASH |
136
|
12
|
|
|
12
|
|
17532
|
{ my( $class, $style)= @_; |
137
|
12
|
|
|
|
|
16
|
my $replace_keys; |
138
|
12
|
100
|
|
|
|
71
|
if( UNIVERSAL::isa( $style, 'CODE')) |
|
|
100
|
|
|
|
|
|
139
|
1
|
|
|
|
|
3
|
{ $replace_keys= $style; } |
140
|
|
|
|
|
|
|
elsif( $style) |
141
|
10
|
100
|
|
|
|
322
|
{ $replace_keys= $replace_func{$style} or croak "wrong Params::Style style '$style'\n"; } |
142
|
|
|
|
|
|
|
else |
143
|
1
|
|
|
|
|
3
|
{ $replace_keys= $replace_func{$DEFAULT_STYLE}; } |
144
|
|
|
|
|
|
|
|
145
|
11
|
|
|
|
|
57
|
return bless [ {}, $replace_keys ], $class; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub STORE |
150
|
24
|
|
|
24
|
|
1035
|
{ my( $hash, $key, $value)= @_; |
151
|
24
|
|
|
|
|
34
|
my( $storage, $replace_keys)= @$hash; |
152
|
24
|
|
|
|
|
50
|
$storage->{$replace_keys->($key)}= $value; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub EXISTS |
156
|
7
|
|
|
7
|
|
561
|
{ my( $hash, $key)= @_; |
157
|
7
|
|
|
|
|
9
|
my( $storage, $replace_keys)= @$hash; |
158
|
7
|
|
|
|
|
11
|
return exists $storage->{$replace_keys->($key)}; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub FETCH |
162
|
24
|
|
|
24
|
|
299
|
{ my( $hash, $key)= @_; |
163
|
24
|
|
|
|
|
33
|
my( $storage, $replace_keys)= @$hash; |
164
|
24
|
|
|
|
|
41
|
return $storage->{$replace_keys->($key)}; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
1; |
170
|
|
|
|
|
|
|
__END__ |