Here's what I finally came up with.
This time around thanks go to Fastolfe and chromatic for all but drawing me a picture of how to use Tie::IxHash, to tilly for pointing me to Data::Dumper recently on an unrelated question in the Chatbox, to ChOas for Yet Another (simpler) Way To Do It, and to AgentM even though I didnt' use his idea.
/msg's or replies welcome if any fine Monks spot something I'm doing in a dumb or dangerous manner.
cheers,
Don
striving for Perl Adept
1 #!/usr/bin/perl -w
2 # getpass.pl
3
4
5 use strict;
6 use Term::ReadKey;
7 use Tie::IxHash;
8 #use Data::Dumper; # Only for testing.
9 #use vars qw(); # Don't need if all vars declared with "my".
10
11
12 # Tied hash of on-screen prompts for old/new passwords
13 tie my %prompts, "Tie::IxHash"
14 %prompts = (
15 'Enter old password:' => 'oldpass',
16 'Enter old enable password:' => 'oldenable',
17 'Enter new password:' => 'newpass',
18 'Confirm new password:' => 'passconf',
19 'Enter new enable password:' => 'newenable',
20 'Confirm new enable password:' => 'enableconf',
21 );
22
23
24 # Walk through each prompt to populate a new tied Hash of passwords
+:
25 print "Prompting for passwords (no screen echo)\n\n"
26 tie my %passwds, "Tie::IxHash"
27 for my $prompt (keys %prompts) {
28 print " $prompt "
29 ReadMode('noecho'); # don't echo to screen.
30 chomp(my $input = <STDIN>);
31 $passwds{$prompts{$prompt}} = $input;
32 ReadMode(0); # re-activate screen echo.
33 print "\n"
34 }
35 print "\n"
36
37
38 # Create named scalars for %passwds values:
39 # Probably a kludge-free way to do this,
40 # but I haven't encountered it yet.
41 my $oldpass = ($passwds{"oldpass"});
42 my $oldenable = ($passwds{"oldenable"});
43 my $newpass = ($passwds{"newpass"});
44 my $passconf = ($passwds{"passconf"});
45 my $newenable = ($passwds{"newenable"});
46 my $enableconf = ($passwds{"enableconf"});
47
48
49 ## Following stuff only for testing,
50 ## since they display what's so carefully hidden
51 ## using Term::Readky's ReadMode('noecho').
52
53
54 # print values via named scalars:
55 #print $oldpass, "\n"
56 #print $oldenable, "\n"
57 #print $newpass, "\n"
58 #print $passconf, "\n"
59 #print $newenable, "\n"
60 #print $enableconf, "\n"
61
62
63 # print values individually:
64 #print $passwds{"oldpass"}, "\n"
65 #print $passwds{"oldenable"}, "\n"
66 #print $passwds{"newpass"}, "\n"
67 #print $passwds{"passconf"}, "\n"
68 #print $passwds{"newenable"}, "\n"
69 #print $passwds{"enableconf"}, "\n"
70 #print "\n"
71
72
73 # print all key/value pairs for both hashes:
74 #print Dumper(%prompts), "\n"
75 #print Dumper(%passwds), "\n"
76
77
78 # print all 6 key/value pairs:
79 #while ((my $key, my $value) = each %passwds) {
80 # print " $key => $value\n"
81 #}
82 #print "\n"
83
84
85 # print all 6 keys:
86 #for my $passwd (keys %passwds) {
87 # print " $passwd \n"
88 #}
89 #print "\n"
90
91
92 # print all 6 values:
93 #for my $passwd (values %passwds) {
94 # print " $passwd \n"
95 #}
96 #print "\n"
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.