line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::SearchReplace; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
11545
|
use 5.006; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
42
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
619
|
|
5
|
|
|
|
|
|
|
#use warnings; |
6
|
|
|
|
|
|
|
#use Data::Dumper; |
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = ( 'sr' ); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '1.02'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# CVS stuff |
16
|
|
|
|
|
|
|
our $date = '$Date: 2007/04/26 09:37:11 $'; |
17
|
|
|
|
|
|
|
our $author = '$Author: steve $'; |
18
|
|
|
|
|
|
|
our $version = '$Revision: 1.02 $'; |
19
|
|
|
|
|
|
|
|
20
|
7
|
|
100
|
7
|
0
|
325
|
sub new { bless $_[1] || {}, $_[0] } |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
{ |
23
|
|
|
|
|
|
|
my $_counter = 0; |
24
|
|
|
|
|
|
|
sub sr { |
25
|
335
|
100
|
|
335
|
0
|
2474
|
my $class = _is_package($_[0]) ? shift : Data::SearchReplace->new(); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# we reset our counter if the caller doesn't come from us... |
28
|
|
|
|
|
|
|
# if you decide to override the private functions _array and _hash |
29
|
|
|
|
|
|
|
# in your own modules you might want to rethink this... |
30
|
335
|
100
|
|
|
|
1195
|
$_counter = 0 if ((caller())[0] ne 'Data::SearchReplace'); |
31
|
|
|
|
|
|
|
|
32
|
335
|
100
|
|
|
|
638
|
my $attrib = defined($_[1]) ? shift : {}; |
33
|
335
|
|
|
|
|
362
|
my $var = shift; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# what action are we taking? |
36
|
|
|
|
|
|
|
# (input to this function overrides class values) |
37
|
335
|
|
|
|
|
405
|
my $action = 'SEARCH'; |
38
|
335
|
100
|
100
|
|
|
1787
|
$action = 'REGEX' if (defined $attrib->{REGEX} && |
39
|
|
|
|
|
|
|
length($attrib->{REGEX}) ); |
40
|
335
|
100
|
100
|
|
|
1434
|
$action = 'FUNCT' if (defined $attrib->{CODE} && |
41
|
|
|
|
|
|
|
length($attrib->{CODE}) ); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# did they setup their vars in class? |
44
|
|
|
|
|
|
|
$attrib->{$_} ||= $class->{$_} || '' |
45
|
335
|
|
100
|
|
|
5589
|
for (qw(SEARCH REPLACE REGEX CODE)); |
|
|
|
100
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
335
|
100
|
|
|
|
1141
|
if (ref($var) eq 'HASH') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
48
|
71
|
|
|
|
|
125
|
_hash($class, $attrib, $var); |
49
|
|
|
|
|
|
|
}elsif (ref($var) eq 'ARRAY') { |
50
|
58
|
|
|
|
|
102
|
_array($class, $attrib, $var); |
51
|
|
|
|
|
|
|
}elsif (ref($var) eq 'SCALAR') { |
52
|
205
|
100
|
|
|
|
402
|
if ($action eq 'SEARCH') { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
53
|
114
|
100
|
|
|
|
457
|
++$_counter if ($$var =~ s/$attrib->{SEARCH}/$attrib->{REPLACE}/g); |
54
|
|
|
|
|
|
|
}elsif ($action eq 'REGEX') { |
55
|
80
|
|
|
|
|
6846
|
eval '++$_counter if ($$var =~ '.$attrib->{REGEX}.')'; |
56
|
80
|
50
|
|
|
|
319
|
warn $@ if $@; |
57
|
|
|
|
|
|
|
}elsif ($action eq 'FUNCT') { |
58
|
11
|
|
|
|
|
34
|
my $oldvar = $var; |
59
|
11
|
|
|
|
|
27
|
$$var = $attrib->{CODE}->($$oldvar); |
60
|
11
|
50
|
|
|
|
55
|
++$_counter unless $$oldvar eq $$var; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
}elsif (ref($var)){ |
63
|
1
|
|
|
|
|
4
|
return; # something we can't handle |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
334
|
|
|
|
|
1358
|
return $_counter; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
71
|
100
|
|
71
|
|
73
|
sub _hash { sr($_[0], $_[1], ref($_[2]->{$_}) ? $_[2]->{$_} : \$_[2]->{$_}) for (keys %{$_[2]}) } |
|
71
|
|
|
|
|
390
|
|
71
|
|
|
|
|
|
|
|
72
|
58
|
100
|
|
58
|
|
67
|
sub _array { sr($_[0], $_[1], ref($_) ? $_ : \$_) for (@{$_[2]}) } |
|
58
|
|
|
|
|
216
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _is_package { |
75
|
335
|
50
|
33
|
335
|
|
1598
|
return unless (defined($_[0]) && ref($_[0])); |
76
|
|
|
|
|
|
|
|
77
|
335
|
|
|
|
|
486
|
for (qw(SCALAR HASH ARRAY REF GLOB CODE)) { |
78
|
1990
|
100
|
|
|
|
4613
|
next unless (ref($_[0]) eq $_); |
79
|
5
|
|
|
|
|
28
|
return; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
330
|
|
|
|
|
807
|
return 1; # made it through the tests so we assume it's a package |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
__END__ |