line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Message::Transform; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Message::Transform::VERSION = '1.132260'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
879
|
use strict;use warnings; |
|
1
|
|
|
1
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
1
|
|
|
1
|
|
5
|
use vars qw(@ISA @EXPORT_OK); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
377
|
|
9
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
@EXPORT_OK = qw(mtransform); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub mtransform { |
13
|
10
|
|
|
10
|
1
|
3672
|
my ($message, $transform) = @_; |
14
|
10
|
100
|
100
|
|
|
123
|
die 'Message::Transform::mtransform: two HASH references required' |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
15
|
|
|
|
|
|
|
if scalar @_ < 2 or |
16
|
|
|
|
|
|
|
scalar @_ > 2 or |
17
|
|
|
|
|
|
|
not ref $message or |
18
|
|
|
|
|
|
|
not ref $transform or |
19
|
|
|
|
|
|
|
ref $message ne 'HASH' or |
20
|
|
|
|
|
|
|
ref $transform ne 'HASH'; |
21
|
|
|
|
|
|
|
|
22
|
4
|
|
|
|
|
8
|
return _mtransform($message, $transform); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _special { |
26
|
2
|
|
|
2
|
|
3
|
my ($message, $transform) = @_; |
27
|
2
|
|
|
|
|
2
|
my $orig_transform = $transform; #in case we need to bail out |
28
|
2
|
|
|
|
|
3
|
substr($transform, 0, 8, ''); |
29
|
2
|
100
|
|
|
|
7
|
if($transform =~ /^s\//) { |
30
|
1
|
|
|
|
|
2
|
substr($transform, 0, 2, ''); |
31
|
1
|
|
|
|
|
1
|
my $ret; |
32
|
1
|
|
|
|
|
58
|
eval "\$ret = $transform;"; |
33
|
1
|
|
|
|
|
6
|
return $ret; |
34
|
|
|
|
|
|
|
} |
35
|
1
|
|
|
|
|
5
|
return $orig_transform; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _mtransform { |
39
|
4
|
|
|
4
|
|
4
|
my ($message, $transform) = @_; |
40
|
4
|
|
|
|
|
9
|
foreach my $t (keys %$transform) { |
41
|
5
|
100
|
|
|
|
9
|
if(ref $transform->{$t}) { #shallow copy if transform is reference |
42
|
1
|
|
|
|
|
3
|
$message->{$t} = $transform->{$t}; |
43
|
|
|
|
|
|
|
} else { #scalar; perhaps something fancy |
44
|
4
|
100
|
|
|
|
9
|
if(substr($transform->{$t}, 0, 8) eq ' special') { #special handling |
45
|
2
|
|
|
|
|
5
|
$message->{$t} = _special($message, $transform->{$t}); |
46
|
|
|
|
|
|
|
} else { |
47
|
2
|
|
|
|
|
6
|
$message->{$t} = $transform->{$t}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
1; |
53
|
|
|
|
|
|
|
__END__ |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 NAME |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Message::Transform - Fast, simple message transformations |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 SYNOPSIS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
use Message::Transform qw(mtransform); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $message = {a => 'b'}; |
64
|
|
|
|
|
|
|
mtransform($message, {x => 'y'}); |
65
|
|
|
|
|
|
|
#$message contains {a => 'b', x => 'y'} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $message = {a => 'b'}; |
68
|
|
|
|
|
|
|
mtransform($message, {c => {d => 'e'}}); |
69
|
|
|
|
|
|
|
#$message contains {a => 'b', c => {d => 'e'}} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $message = {a => 'b'}; |
72
|
|
|
|
|
|
|
mtransform($message, {x => ' specials/$message->{a}'}); |
73
|
|
|
|
|
|
|
#$message contains {a => 'b', x => 'b'} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 DESCRIPTION |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
This is a very very light-weight and fast library that does some basic but |
78
|
|
|
|
|
|
|
reasonably powerful message substitutions. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 FUNCTION |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 mtransform($message, $transform); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Takes two and only two arguments, both HASH references. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 SEE ALSO |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 TODO |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
More special handling. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Recursive transforms. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 BUGS |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
None known. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 COPYRIGHT |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Copyright (c) 2013 Dana M. Diederich. All Rights Reserved. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 AUTHOR |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Dana M. Diederich <diederich@gmail.com> |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |