-
Notifications
You must be signed in to change notification settings - Fork 2
/
rotn
executable file
·60 lines (52 loc) · 1.2 KB
/
rotn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw/:config posix_default no_ignore_case bundling permute/;
GetOptions(
'a|all' => \my $all,
'f|file=s' => \my $filename,
) or die;
my $str = shift;
if (defined $filename) {
$str = do {
open my $fh, '<', $filename or die $!;
local $/; <$fh>;
};
}
$str // die "Usage: $0 [-a -f FILE] STR [SLIDES]\n";
my $slides = shift // 13;
$slides %= 26;
if ($all) {
for my $i (1..26) {
printf "%2d: ", $i;
print_rotn($str, $i);
}
} else {
print_rotn($str, $slides);
}
sub print_rotn {
my ($str, $slides) = @_;
my @upper = ('A'..'Z');
my @lower = ('a'..'z');
if ($slides >= 0) {
for (1 .. $slides) {
push @upper, shift @upper;
push @lower, shift @lower;
}
} else {
for (1 .. abs $slides) {
unshift @upper, pop @upper;
unshift @lower, pop @lower;
}
}
for my $c (split //, $str) {
if ($c =~ /[A-Z]/) {
print $upper[ord($c) - 0x41];
} elsif ($c =~ /[a-z]/) {
print $lower[ord($c) - 0x61];
} else {
print $c;
}
}
print "\n";
}