File Coverage

blib/lib/Crypt/Caesar.pm
Criterion Covered Total %
statement 24 24 100.0
branch 4 4 100.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 33 33 100.0


line stmt bran cond sub pod time code
1             package Crypt::Caesar;
2 1     1   7421 use base 'Exporter';
  1         2  
  1         126  
3 1     1   6 use strict;
  1         1  
  1         38  
4 1     1   6 use vars qw($VERSION @EXPORT);
  1         11  
  1         355  
5              
6             $VERSION = '0.01';
7             @EXPORT = qw(caesar);
8              
9             my %weight = qw(
10             a 7.97 b 1.35 c 3.61 d 4.78 e 12.37 f 2.01 g 1.46 h 4.49
11             i 6.39 j 0.04 k 0.42 l 3.81 m 2.69 n 5.92 o 6.96 p 2.91
12             q 0.08 r 6.63 s 8.77 t 9.68 u 2.62 v 0.81 w 1.88 x 0.23
13             y 2.07 z 0.06
14             );
15              
16             $_ = log($_) + log(26/100) for values %weight;
17              
18             sub caesar ($) {
19 3     3 1 113 my ($string) = @_;
20 3         6 my $copy = lc $string;
21 3         7 $copy =~ tr/a-z//cd;
22 3 100       13 return $string unless length $copy;
23 2         2 my $winner = 0;
24 2         3 my $winscore = 0;
25 2         5 for my $i (1..26) {
26 52         53 my $score = 0;
27 52         55 $copy =~ tr/a-z/b-za/;
28 52         545 $score += $weight{$_} for split //, $copy;
29 52 100       189 if ($score > $winscore) {
30 3         4 $winner = $i;
31 3         4 $winscore = $score;
32             }
33             }
34 2         14 $string =~ tr/A-Za-z/B-ZAb-za/ for 1..$winner;
35 2         9 return $string;
36             }
37            
38             1;
39              
40             =head1 NAME
41              
42             Crypt::Caesar - Decrypt rot-N strings
43              
44             =head1 SYNOPSIS
45              
46             use Crypt::Caesar;
47             print caesar("Vn tjp xvi nzz, do rjmfn.\n");
48              
49             =head1 DESCRIPTION
50              
51             This module is based on the caesar utility from the bsdgames package, made by
52             Stan King and John Eldridge, based on the algorithm suggested by Bob Morris.
53              
54             The caesar utility attempts to decrypt caesar cyphers using English letter
55             frequency statistics.
56              
57             =head2 C
58              
59             This is the only function this package provides. It is exported by default and
60             prototyped C<($)>.
61              
62             =head1 AUTHOR
63              
64             Juerd
65              
66             =cut
67