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.


In reply to Conditional module use by Nkuvu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.