| 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
| |||||||||||||
| 47 | 0 | ▶ | $mag %= $strlng if($mag > $strlng); # Too large magnitude | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | |||||||||||||
| 49 | # Return rotated string | ||||||||||||
| 50 | 0 | ▶ | return $string if($mag == 0); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 66 | # Modifier return | ||||||||||||
| 67 | 0 | ▶ | $_abs{$caller} = [] unless($_abs{$caller}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
MC/DC0%0/1
int shift() || 0
int shift()
Truth table0%0/2
A
A= int shift()
| |||||||||||||
| 86 | 0 | ▶ | my $small = @_ ? int(shift) : $caps; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 0 | ▶ | my $nums = @_ ? int(shift) : $caps; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | |||||||||||||
| 89 | |||||||||||||
| 90 | # Get the cipher mapping -- note order in source | ||||||||||||
| 91 | 0 | ▶ | if($_abs{$caller}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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'
MC/DC0%0/1
$Text::Shift::source[0] || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$Text::Shift::source[0]
Truth table0%0/2
A
A= $Text::Shift::source[0]
| |||||||||||||
| 95 | 0 | ▶ | my $lwcase = ($source[LOWDEX] or LOWER); | ||||||||||
|
Condition0%0/2
$Text::Shift::source[1] || 'abcdefghijklmnopqrstuvwxyz'
MC/DC0%0/1
$Text::Shift::source[1] || 'abcdefghijklmnopqrstuvwxyz'
$Text::Shift::source[1]
Truth table0%0/2
A
A= $Text::Shift::source[1]
| |||||||||||||
| 96 | 0 | ▶ | my $numbrs = ($source[NUMDEX] or NUMBS); | ||||||||||
|
Condition0%0/2
$Text::Shift::source[2] || '0123456789'
MC/DC0%0/1
$Text::Shift::source[2] || '0123456789'
$Text::Shift::source[2]
Truth table0%0/2
A
A= $Text::Shift::source[2]
| |||||||||||||
| 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__ | ||||||||||||