#!/usr/bin/perl
use warnings;
use strict;
my $correct_usage = $ARGV[1];
my $option = $ARGV[0];
my $message = $ARGV[1];
$correct_usage &&= ($option eq "-d" || $option eq "-q");
if (!$correct_usage) {
print(
"Usage: dvorak_cypher.pl <option> <message>\n".
"Options:\n".
" -d Converts <message> from a QWERTY layout to a Dvorak layout\n".
" -q Converts <message> from a Dvorak layout to a QWERTY layout\n";
);
exit(0);
}
my $qwerty = quotemeta(
'~!@#$%^&*()_+`1234567890-='.
'QWERTYUIOP{}|qwertyuiop[]\\'.
'ASDFGHJKL:"asdfghjkl;\''.
'ZXCVBNM<>?zxcvbnm,./'
);
my $dvorak = quotemeta(
'~!@#$%^&*(){}`1234567890[]'.
'"<>PYFGCRL?+|\',.pyfgcrl/=\\'.
'AOEUIDHTNS_aoeuidhtns-'.
':QJKXBMWVZ;qjkxbmwvz'
);
if ($option eq "-d") {
$message =~ tr/$qwerty/$dvorak/;
} elsif ($option eq "-d") {
$message =~ tr/$dvorak/$qwerty/;
}
print $message."\n";
Refactorings
No refactoring yet !
Wayne Rohret
August 8, 2009, August 08, 2009 17:36, permalink
your main problem is that translation is done at compile time. You need use eval on your tr/// in order for it to work with variables. The eval with quotes means compile at run-time and therefore can't associate/bind the variable to the tr/// ($_ is special). You can avoid all this if you just drop the strings into straight into the tr///
And I think that Getopt is a standard module for parsing command line options. Both our codes have one "bug" in that you can have both -d -q at the same time and only the -d option will get translated.
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Std;
our($opt_d, $opt_q);
getopt('dq');
my $qwerty = quotemeta(
'~!@#$%^&*()_+`1234567890-='.
'QWERTYUIOP{}|qwertyuiop[]\\'.
'ASDFGHJKL:"asdfghjkl;\''.
'ZXCVBNM<>?zxcvbnm,./'
);
my $dvorak = quotemeta(
'~!@#$%^&*(){}`1234567890[]'.
'"<>PYFGCRL?+|\',.pyfgcrl/=\\'.
'AOEUIDHTNS_aoeuidhtns-'.
':QJKXBMWVZ;qjkxbmwvz'
);
if (defined $opt_d) {
$_ = $opt_d;
eval "tr/$qwerty/$dvorak/";
} elsif (defined $opt_q) {
$_ = $opt_q;
eval "tr/$dvorak/$qwerty/";
}else {
print
"Usage: dvorak_cypher.pl <option> <message>\n".
"Options:\n".
" -d Converts <message> from a QWERTY layout to a Dvorak layout\n".
" -q Converts <message> from a Dvorak layout to a QWERTY layout\n";
exit(0);
}
print $_."\n";
draegtun
August 13, 2009, August 13, 2009 13:09, permalink
If u're happy with it just translating between two fixed keyboards then here is a version of the code refactored in a more Modern Perl style using MooseX::Declare & MooseX::Getopt (which handles the command line stuff).
perl dvorak_cypher.pl --dvorak hello
perl dvorak_cypher.pl --qwerty "d.nnr xajt"
/I3az/
use MooseX::Declare;
class Keyboard with MooseX::Getopt {
our %kb = (
qwerty => quotemeta(
'~!@#$%^&*()_+`1234567890-='.
'QWERTYUIOP{}|qwertyuiop[]\\'.
'ASDFGHJKL:"asdfghjkl;\''.
'ZXCVBNM<>?zxcvbnm,./'
),
dvorak => quotemeta(
'~!@#$%^&*(){}`1234567890[]'.
'"<>PYFGCRL?+|\',.pyfgcrl/=\\'.
'AOEUIDHTNS_aoeuidhtns-'.
':QJKXBMWVZ;qjkxbmwvz'
),
);
our @keyboards = sort keys %kb;
for my $to ( @keyboards ) {
has $to => ( is => 'rw', isa => 'Str', documentation => do {
my ( $from ) = grep { $_ ne $to } @keyboards;
sprintf( 'Converts <message> from a %s layout to a %s layout', $from, $to );
});
}
method convert {
my ( $from, $to ) = do {
my ( $first, $second ) = @keyboards;
if ( $self->$first ) { reverse @keyboards }
elsif ( $self->$second ) { @keyboards }
else { die "Invalid option\n" }
};
local $_ = $self->$to;
eval "tr/$kb{ $from }/$kb{ $to }/\n";
$_;
}
}
my $kb = Keyboard->new_with_options;
print $kb->convert, "\n";
CafDahsmeashy
June 15, 2011, June 15, 2011 06:05, permalink
Hypertension Adjusting Medications http://crueton.com/ - cialis sin receta El fA?rmaco debe ser tomado cuando se quiera tener relaciones sexuales y solo debe ser adquirido bajo prescripciA?n medica. http://crueton.com/ - cialis 20 mg
Unfainsendear
August 16, 2011, August 16, 2011 03:32, permalink
Medications In Flight Carryons http://www.boxworkshomes.net/ - clomiphene without prescription While fertility issues are the most common reason for prescribing Clomid, it may also be prescribed off label to treat hypogonadism. http://www.boxworkshomes.net/ - clomiphene without prescription
addediese
August 19, 2011, August 19, 2011 01:41, permalink
Bible And Drug Testing
http://www.hobinator.com/ - generic lasix
It is a medication that appears on the World Anti Doping AgencyпїЅs banned drug list because it is known to mask the use of other drugs.
http://www.hobinator.com/ - furosemide cost
I can't quite get the last bit to work. How can I get tr/// to interpolate the variables?
Meanwhile, is there a better way to parse the arguments?