void has asked for the wisdom of the Perl Monks concerning the following question:

If I use the -e option when running the program everything works OK. When I try the -d option I get the line telling me to "Use -e to encrypt or -d to decrypt". I'm sure it's something simple that I'm missing or left out. Any ideas?

#!/usr/bin/perl -w use Crypt::RC4; use Getopt::Std; system('clear'); getopts('ed:', \%opts); if ($opts{e}) { print "Enter Passphrase: "; chomp ($passphrase = <stdin>); print "Enter Plaintext: "; chomp ($plaintext = <stdin>); $encrypted = RC4 ( $passphrase, $plaintext ); $passphrase = ""; print "Enter the file name to save to:"; chomp ($filename = <stdin>); print "Encrypting........\n"; open(FILE,">$filename"); print FILE $encrypted; close FILE or die "There was an error closing the file\n"; } elsif ($opts{d}) { print "option e not used\n"; } else{ print "Use -e to encrypt or -d to decrypt\n"; }


"The Universe is not only more complex than we imagine, it is more complex than we can imagine." - Albert Einstein

Replies are listed 'Best First'.
Re: GetOpts problem..........I think.........
by rchiav (Deacon) on Sep 07, 2001 at 22:57 UTC
    This line here..
    getopts('ed:', \%opts);
    the colon means that 'd' takes arguments. If you're not giving it an argument, the value part of the hash is empty.. which in turn means that your if check will return false.

    Hope this helps..
    Rich

      Thanks a bunch Rich. I knew it was something small that I was doing.

      "The Universe is not only more complex than we imagine, it is more complex than we can imagine." - Albert Einstein