I wrote this ages ago for a few quick scripts. It's less than slick, for example it uses global variables, but I found it useful. The Win32::Console module this script uses should be installed with AS.

use strict; use warnings; use Win32::Console; #Stores handles to stdin/out of the console my $hSTDIN = new Win32::Console(STD_INPUT_HANDLE); my $hSTDOUT = new Win32::Console(STD_OUTPUT_HANDLE); print AskPassword('Password'), "\n"; exit; ##################################################################### sub AskPassword { #Ask the user to supply a password ##################################################################### my ($szPwd, $iEventType, $bKeyDown, $iKeyCode, $iAsciiCode) = ('', 0, 0, 0, 0); my $szPrompt = shift @_; #The question to ask if (defined $szPrompt) {print $szPrompt, ': '} EVENT: while () { #Wait for a console event ($iEventType, $bKeyDown, undef, $iKeyCode, undef, $iAsciiCode, + undef) = $hSTDIN->Input(); #Check event if (not defined $iEventType) { next EVENT } if ($iEventType != 1) { next EVENT } #Not a keyboard event if (not $bKeyDown) { next EVENT } #Not a key down event #Enter key pressed if ($iKeyCode == 13) { last EVENT } #Handle backspace or delete being pressed if ($iKeyCode == 8 or $iKeyCode == 46) { if (length $szPwd == 0) { next EVENT } #Remove offending character from screen and $szPwd MoveCursor(-1, 0, 'R'); print ' '; chop $szPwd; MoveCursor(-1, 0, 'R'); #re-position for next char next EVENT; } #Part of the password if ($iAsciiCode > 32) { print '*'; $szPwd .= chr($iAsciiCode); } } #end EVENT print "\n"; return $szPwd; } ##################################################################### sub MoveCursor # Moves cursor relative to the current positon # or to an absolute position ##################################################################### { my ($iX, $iY, $szMode) = @_; my ($iCurrX, $iCurrY); ($iCurrX, $iCurrY) = $hSTDOUT->Cursor(); if ($szMode eq 'R') { #relative $hSTDOUT->Cursor($iCurrX + $iX, $iCurrY + $iY); } if ($szMode eq 'A') { #absolute if (not defined $iX) {$iX = $iCurrX} if (not defined $iY) {$iY = $iCurrY} $hSTDOUT->Cursor($iX, $iY); } }

Regards,
Dom.


In reply to Re: Reading a password from the user by dbush
in thread Reading a password from the user by rupesh

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.