Nkuvu has asked for the wisdom of the Perl Monks concerning the following question:
Forgive me monks, for I have been slacking. It has been far too long since I wrote a useful Perl script. My Perl skills are a bit rusty, and I've super searched with overwhelming results. That is, too many results, I couldn't find what I needed (I searched for BEGIN, eval, and require).
I'm attempting to update a script to request a password from a user. The current implementation has the password sent in on the command line in clear text. I'd like to make it so that the password is not echoed to the screen, which is simple enough using Term::ReadKey. The problem is that the script will be used on multiple machines, and I can't guarantee that the module will be installed. I can't even guarantee that the version of Perl installed will be greater than 5.6 or so. So rather than have someone install a module on each machine (which is feasible, really, just not my preferred route) I'd like to warn the user if their password is going to be echoed. And not die if the module is not installed, of course.
I wrote a very quick hack of a script to see if I could get this to work:
#!/usr/bin/perl use strict; BEGIN { eval { require Term::ReadKey; }; }; my $use_readkey = 1 unless $@; print "Enter a secret message", (not $use_readkey ? " (NOTE: message will be visible)" : ''), ": "; if ($use_readkey) { Term::ReadKey::ReadMode 2; } my $secret = <STDIN>; if ($use_readkey) { Term::ReadKey::ReadMode 0; } print "\nYour message: $secret\n"; print "More typing, to verify normal input echo: "; my $answer = <STDIN>; print "You said: $answer\n";
On a machine with Term::ReadKey installed, it works just fine. But a machine without Term::ReadKey fails with the following:
Number found where operator expected at try.pl line 15, near "Term::Re +adKey::ReadMode 2" (Do you need to predeclare Term::ReadKey::ReadMode?) Number found where operator expected at try.pl line 19, near "Term::Re +adKey::ReadMode 0" (Do you need to predeclare Term::ReadKey::ReadMode?) syntax error at try.pl line 16, near "Term::ReadKey::ReadMode 2" syntax error at try.pl line 20, near "Term::ReadKey::ReadMode 0" Execution of try.pl aborted due to compilation errors.
It's the end of a long week, and I've spent a few hours looking at various require and BEGIN nodes. I'm not seeing why this would fail, and I've used similar Perl code in other scripts (requiring a different module). I'm sure I'm missing something obvious.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Conditional module use
by ikegami (Patriarch) on Apr 21, 2006 at 19:15 UTC | |
|
Re: Conditional module use
by gaal (Parson) on Apr 21, 2006 at 19:13 UTC | |
by Nkuvu (Priest) on Apr 21, 2006 at 21:18 UTC | |
by chromatic (Archbishop) on Apr 21, 2006 at 21:37 UTC | |
|
Re: Conditional module use
by hesco (Deacon) on Apr 21, 2006 at 19:40 UTC | |
|
Re: Conditional module use
by strat (Canon) on Apr 22, 2006 at 10:06 UTC |