Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

(code) RE: Scalars as hash keys? (solution!)

by ybiC (Prior)
on Oct 27, 2000 at 08:34 UTC ( #38773=note: print w/ replies, xml ) Need Help??


in reply to Scalars as hash keys?

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"


Comment on (code) RE: Scalars as hash keys? (solution!)
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://38773]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2015-07-02 00:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls