#!/usr/bin/perl

# $rottext = rot13( $text )
#
sub rot13 {
    my $val = shift;
    $val =~ tr/a-zA-Z/n-za-mN-ZA-M/;
    return $val;
}

# $enc = caesar( $text, $key )
# $text = caesar( $enc, 26-$key )
#
sub caesar {
    my $text = shift;
    my $key = shift;

    # key of 0 does nothing
    my $ks = $key % 26 or return $text;
    my $ke = $ks - 1;

    my ($s, $S, $e, $E );
    $s = chr(ord('a') + $ks);
    $S = chr(ord('A') + $ks);
    $e = chr(ord('a') + $ke);
    $E = chr(ord('A') + $ke);
    eval "\$text =~ tr/a-zA-Z/$s-za-$e$S-za-$E/;";

    return $text;
}

$message = "the quick brown fox caught mosquito-borne encephalitis.";

$enc = caesar( $message, 5 );
$msg = caesar( $enc, 21 );          # same as original $message

print $msg, "\n";

$rotA = rot13( $message );
$rotB = caesar( $message, 13 );     # same value as $rotA
$msg = rot13( $rotA );              # back to the original $message

print $msg, "\n";
