File Coverage

blib/lib/Regexp/Tr.pm
Criterion Covered Total %
statement 31 36 86.1
branch 8 18 44.4
condition 3 11 27.2
subroutine 7 8 87.5
pod 4 4 100.0
total 53 77 68.8


line stmt bran cond sub pod time code
1             package Regexp::Tr;
2              
3             ### IMPORTS
4             # Boilerplate package beginning
5 1     1   35228 use 5.008;
  1         5  
  1         37  
6 1     1   5 use strict;
  1         1  
  1         35  
7 1     1   5 use warnings;
  1         5  
  1         27  
8 1     1   14 use Carp;
  1         2  
  1         563  
9              
10             ### PACKAGE VARIABLES
11              
12             # UNIVERSAL package variables
13             our $VERSION = "0.05";
14              
15             # The following hash contains caller package names
16             # as keys and arrayrefs as values. These arrayrefs
17             # begin with the parameters passed, and end with the
18             # object (therefore, $last{pkg}[-1] is the last object
19             # created for the namespace "pkg".
20             my %last;
21              
22             # This is a scratch opening in the symbol table and
23             # IS NOT GUARANTEED TO BE ANYTHING AT ALL.
24             our @_called;
25              
26             # This method creates a new instance of the object
27             sub new {
28             # Get parameters and suppress warnings
29 1     1 1 12 my($class,$from,$to,$mods) = @_;
30 1 50       5 $from = "" unless(defined($from));
31 1 50       4 $to = "" unless(defined($to));
32 1 50       3 $mods = "" unless(defined($mods));
33              
34             # Name (or make) the anonymous array in the
35             # %last hash for the caller's package.
36             # (The typeglob assignment saves a hash
37             # access each time @_called is...well, called.)
38 1   50     10 *_called = ($last{caller()} ||= []);
39              
40             # Work the efficiency for loops
41 1 0 33     6 unless(scalar(@_called) and
      33        
      0        
42             ($from eq $_called[0]) and
43             ($to eq $_called[1]) and
44             ($mods eq $_called[2]) )
45             {
46 1         112 my $subref = eval '
47             sub(\$) {
48             my $ref = shift;
49             return ${$ref} =~ tr/'.$from.'/'.$to.'/'.$mods.';
50             };';
51 1 50       4 carp 'Bad tr///:'.$@ if $@;
52 1         7 @_called = ($from,$to,$mods,bless($subref,$class));
53             }
54 1         4 return $_called[-1];
55             }
56              
57             # Performs the actual tr/// operation set up by the object.
58             sub bind {
59 2     2 1 614 my $self = shift;
60              
61             # Verify reference passed
62 2 50       8 (my $ref = shift)
63             or carp "No reference passed";
64 2         5 my $reftype = ref($ref);
65 2 50       8 if(!$reftype) {
    50          
66 0         0 carp "Parameter is not a reference.\n"
67             ."You might have forgotten to backslash the scalar";
68             } elsif($reftype ne "SCALAR") {
69 0         0 carp "Parameter not a scalar reference";
70             }
71              
72             # Perform the operation
73 2         3 return &{$self}($ref);
  2         60  
74             }
75              
76             # Performs the tr/// operation on a scalar passed to the object.
77             sub trans {
78 1     1 1 722 my($self,$val) = @_;
79 1         5 my $cnt = $self->bind(\$val);
80 1 50       6 return wantarray ? ($val, $cnt) : $val;
81             }
82              
83             # Flushes the efficiency storage
84             sub flush {
85 0     0 1   %last = ();
86 0           @_called = ();
87 0           return;
88             }
89              
90             return 1;
91             __END__