Support Forums

Full Version: rot13 in perl
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
huzzah x 2!

Code:
#!/usr/bin/perl
use strict;
use warnings;

sub usage {
    print qq(
Usage:
$0 <text to encrypt>  <# of letter to rotate [13 by default]>
eg... $0 lolencryptthis 5

);
exit;
}

sub rot_encrypt {
    my ($text, $num_sub) = @_;
    $text = "\U$text";
    my %alphabet;
    
    for(my $i = 0x41; $i <= 0x5A; $i++) {
        my $sub = $i + $num_sub;

        while($sub > 0x5A) {
            $sub -= 0x1A;
        }
        $alphabet{chr($i)} = chr($sub);
    }

    my @text = split //, $text;

    for(my $i = 0; $i < @text; $i++) {

        $text[$i] = $alphabet{$text[$i]};
    }

    return join("",@text);
}

usage() if @ARGV != 2;

print rot_encrypt($ARGV[0], $ARGV[1]) . "\n";
Nice!!!!!!!!
I picked this up, while checking out POE::Component::IRC Cookbook.
Code:
$rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];

Source: http://poe.perl.org/?POE_Cookbook/IRC_Bots
(12-12-2009, 06:56 AM)0x80483fb Wrote: [ -> ]I picked this up, while checking out POE::Component::IRC Cookbook.
Code:
$rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];

Source: http://poe.perl.org/?POE_Cookbook/IRC_Bots

Yeah, but that way's no fun. And you can get rid of the capitals if you supply the 'i' modifier.