-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_decode.pl
34 lines (30 loc) · 1.37 KB
/
encode_decode.pl
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
#!/usr/bin/perl
# encode_decode.pl
# A quick example of how the tr (transliteration) function works.
use strict;
use warnings;
my $message; # Required by "use strict", a good practice.
print "Enter a message to encode:\n>";
$message = <STDIN>;
chomp $message;
# The tr (transliteration) function works by replacing one character with another.
# In this case we are replacing each character with one that appears in the
# alphabet 13 characters later. So an "A" becomes an "N", a "B" becomes a "O", etc.
# The old folks might recognize this scheme as ROT13.
# By the way, the tr function does not use regular expressions at all.
$message =~ tr/A-Za-z/N-ZA-Mn-za-m/;
print "Here is the encoded message:\n>$message\n";
print "\nPress Enter to decode the message:";
<STDIN>;
# To decode ROT13, we just do the exact same encoding we did before. Note
# that we are only messing with the alphabetic characters, not numbers, spaces,
# or punctuation. ROT13 is not a very good way to encode messages.
$message =~ tr/A-Za-z/N-ZA-Mn-za-m/;
print "Here is the decoded message:\n>$message\n";
# Here's a way to only add the extra input when we are in Windows.
# We check for an environment variable called OS and see if it is
# set to "Windows_NT". I don't know how reliable this will be.
if ( ($ENV{OS}) && ($ENV{OS} eq "Windows_NT") ) {
print "\nPress Enter to close window";
<STDIN>;
}