blib/lib/Text/Shift.pm

filter: Statementstmt 55.0% 33 / 60 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.9% 10 / 13 Podpod 100% 1 / 1 totaltotal 43.1% 44 / 102 CC 13 SCAR 33.4
CC
13
Cov
39%
new
72.0
_rotate_alphabet
20.0
__ANON__
12.0
CRAP 28.1
↑ Summary
1 package Text::Shift;
2  
3 # Boilerplate package imports
4 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Implementation-specific imports
10 1 ▶use Crypt::Cipher v0.02;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use integer; # potential speed increase
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Constants
14 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 # Change the following values to modify the default alphabets
16 1 UPPER => join("","A".."Z"),
17 LOWER => join("","a".."z"),
18 NUMBS => join("",0..9)
19 1 };
20 1 ▶use constant { # constant is broken for self-reference
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 FROM => join("",UPPER,LOWER,NUMBS),
22
23 # THE FOLLOWING THREE CONSTANTS MUST NOT CHANGE
24 UPPDEX => 0,
25 LOWDEX => 1,
26 NUMDEX => 2
27 1 };
28
29 # Class variables
30 1 our @ISA = qw(Crypt::Cipher);
31 1 our $VERSION = '1.00';
32 1 our %_abs; # Hash of callers' package names pointing to arrays
33 # The array is made up of UPPERCASE, LOWERCASE, NUMBERS
34  
35  
36 #############
37 # FUNCTIONS #
38 #############
39 # The following function rotates the alphabet by magnitude amount
40 0 ▶sub _rotate_alphabet($$) {
Subroutine _rotate_alphabet: not called CC=4 SCAR=30.0
41 # Get parameters
42 0 my($string,$mag) = (shift, int(shift));
43 0 my $strlng = length($string);
44
45 # Handle outliers
46 0 ▶ $mag += $strlng while($mag < 0); # Negative magnitude
Branch: 1 branch
BranchTrueFalseTotal
$mag < 0 and do { $mag += $strlng } 0 0 0
47 0 ▶ $mag %= $strlng if($mag > $strlng); # Too large magnitude
Branch: 1 branch
BranchTrueFalseTotal
if $mag > $strlng 0 0 0
48  
49 # Return rotated string
50 0 ▶ return $string if($mag == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $mag == 0 0 0 0
51 0 $string .= substr($string,0,$mag, "");
52 0 return $string;
53 }
54  
55 ###########
56 # Methods #
57 ###########
58  
59 # Create the alphabet control methods
60 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=2.0
61 3 ▶ my $funcref = sub( $ ) {
Subroutine __ANON__: called CC=1 SCAR=4.4
62 3 my $index = shift;
63 0 ▶ return sub($$) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
64 0 (undef, my $caller) = (shift, caller);
65 0 ▶ if(scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 0 0 0
66 # Modifier return
67 0 ▶ $_abs{$caller} = [] unless($_abs{$caller});
Branch: 1 branch
BranchTrueFalseTotal
unless $Text::Shift::_abs{$caller} 0 0 0
68 0 return $_abs{$caller}->[$index] = join("",@_);
69 } else {
70 # Accessor return
71 0 return $_abs{$caller}->[$index];
72 }
73 3 };
74 1 };
75
76 1 *uppercase = $funcref->(UPPDEX);
77 1 *lowercase = $funcref->(LOWDEX);
78 1 *numbers = $funcref->(NUMDEX);
79 }
80  
81 0 ▶sub new($;$$$) {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
82 # get the parameters
83 0 my $caller = caller;
84 0 my $class = shift;
85 0 ▶ my $caps = (int(shift) or 0);
Condition0%0/2
int shift() || 0
l!l
00
MC/DC0%0/1
int shift() || 0
int shift()
Truth table0%0/2
A
A= int shift()
Aresult
11
00
86 0 ▶ my $small = @_ ? int(shift) : $caps;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
87 0 ▶ my $nums = @_ ? int(shift) : $caps;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
88
89
90 # Get the cipher mapping -- note order in source
91 0 ▶ if($_abs{$caller}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::Shift::_abs{$caller}) { } 0 0 0
92 0 our @source;
93 0 *source = $_abs{$caller};
94 0 ▶ my $upcase = ($source[UPPDEX] or UPPER);
Condition0%0/2
$Text::Shift::source[0] || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
l!l
00
MC/DC0%0/1
$Text::Shift::source[0] || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$Text::Shift::source[0]
Truth table0%0/2
A
A= $Text::Shift::source[0]
Aresult
11
00
95 0 ▶ my $lwcase = ($source[LOWDEX] or LOWER);
Condition0%0/2
$Text::Shift::source[1] || 'abcdefghijklmnopqrstuvwxyz'
l!l
00
MC/DC0%0/1
$Text::Shift::source[1] || 'abcdefghijklmnopqrstuvwxyz'
$Text::Shift::source[1]
Truth table0%0/2
A
A= $Text::Shift::source[1]
Aresult
11
00
96 0 ▶ my $numbrs = ($source[NUMDEX] or NUMBS);
Condition0%0/2
$Text::Shift::source[2] || '0123456789'
l!l
00
MC/DC0%0/1
$Text::Shift::source[2] || '0123456789'
$Text::Shift::source[2]
Truth table0%0/2
A
A= $Text::Shift::source[2]
Aresult
11
00
97 0 my $to =
98 _rotate_alphabet($upcase, $caps).
99 _rotate_alphabet($lwcase, $small).
100 _rotate_alphabet($numbrs, $nums);
101 0 return $class->SUPER::new(
102 ($upcase.$lwcase.$numbrs),
103 $to
104 );
105 } else {
106 0 my $to =
107 _rotate_alphabet(UPPER, $caps).
108 _rotate_alphabet(LOWER, $small).
109 _rotate_alphabet(NUMBS, $nums);
110 0 return $class->SUPER::new(FROM,$to);
111 }
112 }
113  
114  
115 1 return 1;
116 __END__
↑ Summary