line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
132653
|
use strict; |
|
2
|
|
|
|
|
20
|
|
|
2
|
|
|
|
|
59
|
|
2
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
93
|
|
3
|
|
|
|
|
|
|
package String::RewritePrefix; |
4
|
|
|
|
|
|
|
$String::RewritePrefix::VERSION = '0.008'; |
5
|
2
|
|
|
2
|
|
12
|
use Carp (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
98
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: rewrite strings based on a set of known prefixes |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# 0.972 allows \'method_name' form -- rjbs, 2010-10-25 |
9
|
2
|
|
|
|
|
12
|
use Sub::Exporter 0.972 -setup => { |
10
|
|
|
|
|
|
|
exports => [ rewrite => \'_new_rewriter' ], |
11
|
2
|
|
|
2
|
|
1055
|
}; |
|
2
|
|
|
|
|
24096
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#pod =head1 SYNOPSIS |
14
|
|
|
|
|
|
|
#pod |
15
|
|
|
|
|
|
|
#pod use String::RewritePrefix; |
16
|
|
|
|
|
|
|
#pod my @to_load = String::RewritePrefix->rewrite( |
17
|
|
|
|
|
|
|
#pod { '' => 'MyApp::', '+' => '' }, |
18
|
|
|
|
|
|
|
#pod qw(Plugin Mixin Addon +Corporate::Thinger), |
19
|
|
|
|
|
|
|
#pod ); |
20
|
|
|
|
|
|
|
#pod |
21
|
|
|
|
|
|
|
#pod # now you have: |
22
|
|
|
|
|
|
|
#pod qw(MyApp::Plugin MyApp::Mixin MyApp::Addon Corporate::Thinger) |
23
|
|
|
|
|
|
|
#pod |
24
|
|
|
|
|
|
|
#pod You can also import a rewrite routine: |
25
|
|
|
|
|
|
|
#pod |
26
|
|
|
|
|
|
|
#pod use String::RewritePrefix rewrite => { |
27
|
|
|
|
|
|
|
#pod -as => 'rewrite_dt_prefix', |
28
|
|
|
|
|
|
|
#pod prefixes => { '' => 'MyApp::', '+' => '' }, |
29
|
|
|
|
|
|
|
#pod }; |
30
|
|
|
|
|
|
|
#pod |
31
|
|
|
|
|
|
|
#pod my @to_load = rewrite_dt_prefix( qw(Plugin Mixin Addon +Corporate::Thinger)); |
32
|
|
|
|
|
|
|
#pod |
33
|
|
|
|
|
|
|
#pod # now you have: |
34
|
|
|
|
|
|
|
#pod qw(MyApp::Plugin MyApp::Mixin MyApp::Addon Corporate::Thinger) |
35
|
|
|
|
|
|
|
#pod |
36
|
|
|
|
|
|
|
#pod =method rewrite |
37
|
|
|
|
|
|
|
#pod |
38
|
|
|
|
|
|
|
#pod String::RewritePrefix->rewrite(\%prefix, @strings); |
39
|
|
|
|
|
|
|
#pod |
40
|
|
|
|
|
|
|
#pod This rewrites all the given strings using the rules in C<%prefix>. Its keys |
41
|
|
|
|
|
|
|
#pod are known prefixes for which its values will be substituted. This is performed |
42
|
|
|
|
|
|
|
#pod in longest-first order, and only one prefix will be rewritten. |
43
|
|
|
|
|
|
|
#pod |
44
|
|
|
|
|
|
|
#pod If the prefix value is a coderef, it will be executed with the remaining string |
45
|
|
|
|
|
|
|
#pod as its only argument. The return value will be used as the prefix. |
46
|
|
|
|
|
|
|
#pod |
47
|
|
|
|
|
|
|
#pod =cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub rewrite { |
50
|
18
|
|
|
18
|
1
|
875
|
my ($self, $arg, @rest) = @_; |
51
|
18
|
|
|
|
|
69
|
return $self->_new_rewriter(rewrite => { prefixes => $arg })->(@rest); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _new_rewriter { |
55
|
23
|
|
|
23
|
|
2515
|
my ($self, $name, $arg) = @_; |
56
|
23
|
|
50
|
|
|
68
|
my $rewrites = $arg->{prefixes} || {}; |
57
|
|
|
|
|
|
|
|
58
|
23
|
|
|
|
|
30
|
my @rewrites; |
59
|
23
|
|
|
|
|
102
|
for my $prefix (sort { length $b <=> length $a } keys %$rewrites) { |
|
64
|
|
|
|
|
150
|
|
60
|
71
|
|
|
|
|
168
|
push @rewrites, ($prefix, $rewrites->{$prefix}); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return sub { |
64
|
38
|
|
|
38
|
|
6352
|
my @result; |
65
|
|
|
|
|
|
|
|
66
|
38
|
50
|
|
|
|
93
|
Carp::cluck("string rewriter invoked in void context") |
67
|
|
|
|
|
|
|
unless defined wantarray; |
68
|
|
|
|
|
|
|
|
69
|
38
|
100
|
|
|
|
75
|
if ( ! wantarray) { |
70
|
29
|
50
|
|
|
|
66
|
Carp::croak("attempt to rewrite multiple strings outside of list context") |
71
|
|
|
|
|
|
|
if @_ > 1; |
72
|
|
|
|
|
|
|
|
73
|
29
|
50
|
|
|
|
60
|
Carp::cluck("rewrite invoked without args") |
74
|
|
|
|
|
|
|
if @_ == 0; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
38
|
|
|
|
|
105
|
STRING: for my $str (@_) { |
78
|
71
|
|
|
|
|
154
|
for (my $i = 0; $i < @rewrites; $i += 2) { |
79
|
166
|
100
|
|
|
|
401
|
if (index($str, $rewrites[$i]) == 0) { |
80
|
66
|
100
|
|
|
|
149
|
if (ref $rewrites[$i+1]) { |
81
|
10
|
|
|
|
|
23
|
my $rest = substr $str, length($rewrites[$i]); |
82
|
10
|
|
|
|
|
26
|
my $str = $rewrites[ $i+1 ]->($rest); |
83
|
10
|
100
|
|
|
|
53
|
push @result, (defined $str ? $str : '') . $rest; |
84
|
|
|
|
|
|
|
} else { |
85
|
56
|
|
|
|
|
161
|
push @result, $rewrites[$i+1] . substr $str, length($rewrites[$i]); |
86
|
|
|
|
|
|
|
} |
87
|
66
|
|
|
|
|
155
|
next STRING; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
5
|
|
|
|
|
13
|
push @result, $str; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
38
|
100
|
|
|
|
292
|
return wantarray ? @result : $result[0]; |
95
|
23
|
|
|
|
|
146
|
}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__END__ |