blib/lib/Regexp/Tr.pm

filter: Statementstmt 87.1% 34 / 39 Branchbran 50.0% 9 / 18 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 87.5% 7 / 8 Podpod 100% 4 / 4 totaltotal 68.2% 58 / 85 CC 14 SCAR 21.9
CC
14
Cov
69%
new
15.8
bind
4.7
trans
2.0
CRAP 8.9
↑ Summary
1 package Regexp::Tr;
2  
3 ### IMPORTS
4 # Boilerplate package beginning
5 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 ### PACKAGE VARIABLES
11  
12 # UNIVERSAL package variables
13 1 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 1 my %last;
21  
22 # This is a scratch opening in the symbol table and
23 # IS NOT GUARANTEED TO BE ANYTHING AT ALL.
24 1 our @_called;
25  
26 # This method creates a new instance of the object
27 1 ▶sub new {
Subroutine new: called CC=10 SCAR=27.6
Pod documented
28 # Get parameters and suppress warnings
29 1 my($class,$from,$to,$mods) = @_;
30 1 ▶ $from = "" unless(defined($from));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 0 1 1
31 1 ▶ $to = "" unless(defined($to));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 0 1 1
32 1 ▶ $mods = "" unless(defined($mods));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mods 1 0 1
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 ▶ *_called = ($last{caller()} ||= []);
Condition50%1/2
$last{caller()} ||= []
l!l
01
MC/DC0%0/1
$last{caller()} ||= []
$last{caller()}
Truth table50%1/2
A
A= $last{caller()}
Aresult
11
00
39  
40 # Work the efficiency for loops
41 1 ▶ unless(scalar(@_called) and
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @Regexp::Tr::_called and $from eq $Regexp::Tr::_called[0] and $to eq $Regexp::Tr::_called[1] and $mods eq $Regexp::Tr::_called[2]) 1 0 1
Condition33%3/9
scalar @Regexp::Tr::_called and $from eq $Regexp::Tr::_called[0] and $to eq $Regexp::Tr::_called[1] and $mods eq $Regexp::Tr::_called[2]
!ll&&!rl&&r
100
scalar @Regexp::Tr::_called and $from eq $Regexp::Tr::_called[0] and $to eq $Regexp::Tr::_called[1]
!ll&&!rl&&r
100
scalar @Regexp::Tr::_called and $from eq $Regexp::Tr::_called[0]
!ll&&!rl&&r
100
MC/DC0%0/4
scalar @Regexp::Tr::_called and $from eq $Regexp::Tr::_called[0] and $to eq $Regexp::Tr::_called[1] and $mods eq $Regexp::Tr::_called[2]
scalar @Regexp::Tr::_called $from eq $Regexp::Tr::_called[0] $to eq $Regexp::Tr::_called[1] $mods eq $Regexp::Tr::_called[2]
Truth table20%1/5
A and B and C and D
A= scalar @Regexp::Tr::_calledB= $from eq $Regexp::Tr::_called[0]C= $to eq $Regexp::Tr::_called[1]D= $mods eq $Regexp::Tr::_called[2]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
42 ($from eq $_called[0]) and
43 ($to eq $_called[1]) and
44 ($mods eq $_called[2]) )
45 {
46 1 my $subref = eval '
47 sub(\$) {
48 my $ref = shift;
49 return ${$ref} =~ tr/'.$from.'/'.$to.'/'.$mods.';
50 };';
51 1 ▶ carp 'Bad tr///:'.$@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
52 1 @_called = ($from,$to,$mods,bless($subref,$class));
53 }
54 1 return $_called[-1];
55 }
56  
57 # Performs the actual tr/// operation set up by the object.
58 2 ▶sub bind {
Subroutine bind: called CC=4 SCAR=15.5
Pod documented
59 2 my $self = shift;
60  
61 # Verify reference passed
62 2 ▶ (my $ref = shift)
Branch: 1 branch
BranchTrueFalseTotal
my $ref = shift() or carp("No reference passed") 2 0 2
63 or carp "No reference passed";
64 2 my $reftype = ref($ref);
65 2 ▶ if(!$reftype) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $reftype) { } 0 2 2
elsif ($reftype ne 'SCALAR') { } 0 2 2
66 0 carp "Parameter is not a reference.\n"
67 ."You might have forgotten to backslash the scalar";
68 } elsif($reftype ne "SCALAR") {
69 0 carp "Parameter not a scalar reference";
70 }
71  
72 # Perform the operation
73 2 return &{$self}($ref);
74 }
75  
76 # Performs the tr/// operation on a scalar passed to the object.
77 1 ▶sub trans {
Subroutine trans: called CC=2 SCAR=7.1
Pod documented
78 1 my($self,$val) = @_;
79 1 my $cnt = $self->bind(\$val);
80 1 ▶ return wantarray ? ($val, $cnt) : $val;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
81 }
82  
83 # Flushes the efficiency storage
84 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
85 0 %last = ();
86 0 @_called = ();
87 0 return;
88 }
89  
90 1 return 1;
91 __END__
↑ Summary