line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
4600
|
use 5.008; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
96
|
|
2
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
56
|
|
3
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
120
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Data::SmartMunge; |
6
|
|
|
|
|
|
|
BEGIN { |
7
|
2
|
|
|
2
|
|
38
|
$Data::SmartMunge::VERSION = '1.101612'; |
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ABSTRACT: Munge scalars, hashes and arrays in flexible ways |
11
|
2
|
|
|
2
|
|
9
|
use Exporter qw(import); |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
1451
|
|
12
|
|
|
|
|
|
|
our %EXPORT_TAGS = (util => [qw(smart_munge delete_matching)],); |
13
|
|
|
|
|
|
|
our @EXPORT_OK = @{ $EXPORT_TAGS{all} = [ map { @$_ } values %EXPORT_TAGS ] }; |
14
|
|
|
|
|
|
|
my %munger_dispatch = ( |
15
|
|
|
|
|
|
|
STRING_CODE => sub { $_[1]->($_[0]) }, |
16
|
|
|
|
|
|
|
ARRAY_CODE => sub { $_[1]->($_[0]) }, |
17
|
|
|
|
|
|
|
HASH_CODE => sub { $_[1]->($_[0]) }, |
18
|
|
|
|
|
|
|
HASH_HASH => sub { +{ %{ $_[0] }, %{ $_[1] } } }, # overlay |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub smart_munge { |
22
|
9
|
|
|
9
|
1
|
3051
|
my ($data, $munger) = @_; |
23
|
|
|
|
|
|
|
|
24
|
9
|
100
|
|
|
|
23
|
unless (defined $munger) { |
25
|
3
|
50
|
|
|
|
14
|
return $data unless wantarray; |
26
|
0
|
0
|
|
|
|
0
|
return @$data if ref $data eq 'ARRAY'; |
27
|
0
|
0
|
|
|
|
0
|
return %$data if ref $data eq 'HASH'; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
6
|
|
100
|
|
|
24
|
my $data_ref = ref $data || 'STRING'; |
31
|
6
|
|
50
|
|
|
20
|
my $munger_ref = ref $munger || 'STRING'; |
32
|
6
|
50
|
|
|
|
22
|
if (my $handler = $munger_dispatch{ $data_ref . '_' . $munger_ref }) { |
33
|
6
|
|
|
|
|
14
|
my $result = $handler->($data, $munger); |
34
|
6
|
50
|
|
|
|
58
|
return $result unless wantarray; |
35
|
0
|
0
|
|
|
|
0
|
return @$result if ref $result eq 'ARRAY'; |
36
|
0
|
0
|
|
|
|
0
|
return %$result if ref $result eq 'HASH'; |
37
|
|
|
|
|
|
|
} else { |
38
|
0
|
|
|
|
|
0
|
die "can't munge $data_ref with $munger_ref"; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub delete_matching { |
43
|
2
|
|
|
2
|
1
|
13
|
my ($re, $flags) = @_; |
44
|
2
|
100
|
|
|
|
7
|
$flags = '' unless defined $flags; |
45
|
|
|
|
|
|
|
return $flags =~ s/g// |
46
|
1
|
|
|
1
|
|
9
|
? sub { $_[0] =~ s/$re//g; $_[0] } |
|
1
|
|
|
|
|
3
|
|
47
|
2
|
100
|
|
1
|
|
21
|
: sub { $_[0] =~ s/$re//; $_[0] }; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
3
|
|
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
__END__ |