line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::vaporcalc::FormatString; |
2
|
|
|
|
|
|
|
$App::vaporcalc::FormatString::VERSION = '0.005004'; |
3
|
1
|
|
|
1
|
|
58168
|
use Defaults::Modern; |
|
1
|
|
|
|
|
289602
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
38200
|
use parent 'Exporter::Tiny'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
6
|
|
|
|
|
|
|
our @EXPORT = our @EXPORT_OK = 'format_str'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub format_str { |
9
|
3
|
|
|
3
|
1
|
11
|
my $string = shift; |
10
|
3
|
50
|
33
|
|
|
17
|
return '' unless defined $string and length $string; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my %vars = @_ > 1 ? @_ |
13
|
3
|
50
|
33
|
|
|
23
|
: ref $_[0] && reftype $_[0] eq 'HASH' ? %{$_[0]} |
|
1
|
100
|
|
|
|
4
|
|
14
|
|
|
|
|
|
|
: (); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $rpl = sub { |
17
|
5
|
|
|
5
|
|
13
|
my ($orig, $match) = @_; |
18
|
5
|
50
|
|
|
|
11
|
if (defined $vars{$match}) { |
19
|
|
|
|
|
|
|
return ref $vars{$match} eq 'CODE' ? |
20
|
|
|
|
|
|
|
$vars{$match}->($match, $orig, $vars{$match}) |
21
|
5
|
100
|
|
|
|
20
|
: $vars{$match} |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
$orig |
24
|
3
|
|
|
|
|
14
|
}; |
|
0
|
|
|
|
|
0
|
|
25
|
|
|
|
|
|
|
|
26
|
3
|
|
|
|
|
8
|
state $re = qr/(%([^\s%]+)%?)/; |
27
|
3
|
|
|
|
|
33
|
$string =~ s/$re/$rpl->($1, $2)/ge; |
|
5
|
|
|
|
|
8
|
|
28
|
|
|
|
|
|
|
|
29
|
3
|
|
|
|
|
26
|
$string |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
1; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=pod |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 NAME |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
App::vaporcalc::FormatString - Templated string formatter |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 SYNOPSIS |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $things = "some very special"; |
43
|
|
|
|
|
|
|
my $formatted = format_str( "My %string% with %this% var", |
44
|
|
|
|
|
|
|
this => $things, |
45
|
|
|
|
|
|
|
string => "cool string", |
46
|
|
|
|
|
|
|
); ## -> My cool string with some very special var |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 DESCRIPTION |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
A tiny string formatter. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Exports a single function: L</format_str> |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 format_str |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Takes a string and a hash (or hash reference) mapping template variables to |
57
|
|
|
|
|
|
|
replacement strings. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
The replacement variables can be coderefs returning a string: |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
format_str( "My string with %code", |
62
|
|
|
|
|
|
|
code => sub { |
63
|
|
|
|
|
|
|
my ($match, $orig, $callback) = @_; |
64
|
|
|
|
|
|
|
. . . |
65
|
|
|
|
|
|
|
return "Some string replacing variable $match" |
66
|
|
|
|
|
|
|
}, |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
The code reference will receive the matching variable ("code"), the original matched |
70
|
|
|
|
|
|
|
string ("%code" in the above example), and itself as its respective arguments. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 AUTHOR |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Jon Portnoy <avenj@cobaltirc.org> |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |