File Coverage

blib/lib/String/Compile/Tr.pm
Criterion Covered Total %
statement 30 30 100.0
branch 8 10 80.0
condition 3 3 100.0
subroutine 7 7 100.0
pod 1 1 100.0
total 49 51 96.0


operator shall be operator are overloaded at runtime , . target may be given as an argument to the generated sub operation cannot be compiled. does not support these neither. 's operands
line stmt bran cond sub pod time code
1 4     4   863657 use 5.010;
  4         19  
2 4     4   59 use strict;
  4         10  
  4         185  
3 4     4   25 use warnings;
  4         14  
  4         546  
4              
5             package String::Compile::Tr;
6              
7             =encoding UTF-8
8              
9             =head1 NAME
10              
11             String::Compile::Tr - compile tr/// expressions
12              
13             =head1 VERSION
14              
15             Version 0.06
16              
17             =cut
18              
19             our
20             $VERSION = '0.06';
21              
22              
23             =head1 SYNOPSIS
24              
25             use String::Compile::Tr;
26            
27             my $search = '/+=';
28             my $replace = '-_';
29             my $s = 'De/0xv5y3w8BpLF8ubOo+w==';
30             trgen($search, $replace, 'd')->($s);
31             # $s = 'De-0xv5y3w8BpLF8ubOo_w'
32              
33             =head1 DESCRIPTION
34              
35             The usual approach when operands of a C
36             variables is to apply C on a string to interpolate the operands.
37             The drawback of this approach are possible unwanted side effects induced
38             by the variables' content, e.g.
39              
40             $search = '//,warn-trapped,$@=~tr/';
41             eval "tr/$search//d";
42              
43             C offers an alternative where the content of a
44             variable can be used as operand without C'ing it.
45             Instead the operands of a C
46             inside a constant C.
47              
48             C compiles an anonymous sub that
49             performs almost the same operation as C
50             but allows variable operands.
51              
52             C is imported by default by C.
53              
54              
55             =head1 FUNCTIONS
56              
57             =head2 trgen
58              
59             trgen(search, replace, [options])
60              
61             C returns an anonymous subroutine that performs an almost identical
62             operation as C
63             The C
64             or is the default input C<$_> otherwise.
65              
66              
67             =head1 ERRORS
68              
69             C will throw an exception if an invalid option is specified
70             or the C
71              
72             =head1 EXAMPLES
73              
74             Proposed usages of this module are:
75              
76             use String::Compile::Tr;
77              
78             my $search = 'abc';
79             my $replace = '123';
80             my $tr = trgen($search, $replace);
81             my $s = 'fedcba';
82             $tr->($s);
83             # $s is 'fed321' now
84              
85             my @list = qw(axy bxy cxy);
86             $tr->() for @list;
87             # @list is now ('1xy', '2xy', '3xy');
88              
89             print trgen($search, $replace, 'r')->('fedcba'); # 'fed321'
90              
91             =head1 RESTRICTIONS
92              
93             Character ranges are not supported in the search and replace lists.
94             All characters are interpreted literally.
95             This is caused by the fact that C
96             It's the compiler that expands character ranges in C
97             before handing them over.
98              
99             Overloading constants in C requires perl v5.10.
100              
101             =head1 AUTHOR
102              
103             Jörg Sommrey, C<< >>
104              
105             =head1 LICENSE AND COPYRIGHT
106              
107             This software is copyright (c) 2025 by Jörg Sommrey.
108              
109             This is free software; you can redistribute it and/or modify it under
110             the same terms as the Perl 5 programming language system itself.
111              
112             =head1 SEE ALSO
113              
114             L
115              
116             L
117              
118             L
119              
120             L provides a similar functionality, though this C's
121             its oprands.
122              
123             =cut
124              
125 4     4   31 use Carp;
  4         11  
  4         334  
126 4     4   2317 use String::Compile::Tr::Overload;
  4         14  
  4         19  
127              
128 4     4   2258 use Exporter::Shiny our @EXPORT = qw(trgen);
  4         35679  
  4         39  
129              
130             *search = *String::Compile::Tr::Overload::search;
131             *replace = *String::Compile::Tr::Overload::replace;
132              
133             sub trgen {
134 7     7 1 848410 local our ($search, $replace);
135 7         15 my $options;
136 7         31 ($search, $replace, $options) = @_;
137 7 50       40 $replace = '' unless defined $replace;
138 7 100       32 $options = '' unless defined $options;
139 7         53 my ($opt) = $options =~ /^([cdsr]*)$/;
140 7 100       28 $opt = '' unless defined $opt;
141 7 100 100     345 croak "options invalid: $options" if $options && $options ne $opt;
142 6         13 my $ret;
143 6         15 my $template = <<'EOS';
144             $ret = sub {
145             local *_ = \$_[0] if @_;
146             tr/:search:/:replace:/%s;
147             }; 1
148             EOS
149 6         29 my $code = sprintf $template, $opt;
150              
151 6 50       621 eval $code or croak $@;
152 6         43 $ret;
153             }
154              
155             1;