File Coverage

blib/lib/ReplaceMultiple.pm
Criterion Covered Total %
statement 20 23 86.9
branch n/a
condition n/a
subroutine 6 7 85.7
pod 0 5 0.0
total 26 35 74.2


line stmt bran cond sub pod time code
1             package ReplaceMultiple;
2              
3 1     1   17003 use strict;
  1         2  
  1         50  
4 1     1   4 use warnings;
  1         2  
  1         300  
5              
6             our $VERSION = 1.00;
7              
8             our (@ISA, @EXPORT);
9             require Exporter; @ISA = qw(Exporter);
10             @EXPORT = qw(replace_multiple_inplace replace_multiple);
11              
12             sub replace_multiple_inplace {
13 0     0 0 0 my ($hash, $str_ref) = @_;
14 0         0 my $obj = ReplaceMultiple->new($hash);
15 0         0 return $obj->apply_inplace($str_ref);
16             }
17              
18             sub replace_multiple {
19 1     1 0 11 my ($hash, $str) = @_;
20 1         7 my $obj = ReplaceMultiple->new($hash);
21 1         4 return $obj->apply($str);
22             }
23              
24             sub new {
25 1     1 0 2 my ($class, $hash) = @_;
26 1         5 my $re_str = "(" . (join '|', map { "\Q$_\E" } keys %$hash) . ")";
  2         9  
27 1         39 my $self = bless {HASH=>$hash, RE=>qr/$re_str/}, $class;
28 1         4 return $self;
29             }
30              
31             sub apply_inplace {
32 1     1 0 2 my ($self, $str_ref) = @_;
33 1         23 $$str_ref =~ s/$self->{RE}/$self->{HASH}{$1}/g;
34 1         12 return $$str_ref;
35             }
36              
37             sub apply {
38 1     1 0 2 my ($self, $str) = @_;
39 1         2 my $str2 = $str;
40 1         5 return $self->apply_inplace(\$str2);
41             }
42              
43             1;