CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
why won't my password program work?

 



ahanif64
New User

May 4, 2014, 4:24 PM

Post #1 of 2 (3149 views)
why won't my password program work? Can't Post

I'm trying to make a program that checks passwords but it isnt working :(


Quote
#Asking the user for their password.
print "Please enter your password : ";

#Creating the password variable and also getting it read from the user.
$userPassword = <STDIN>;

#Password checking sub.
sub passwordChecker
{

@Passwords = ("CookieChan", "Gon", "Killua");

$Access = 0;

if($userPassword eq $Passwords[0]){
$Access = 1
}elsif ($userPassword eq $Passwords[1]){
$Access = 1
}elsif ($userPassword eq $Passwords[2]){
$Access = 1
}else{
$Access = 0
}
}

&passwordChecker;

if($Access == 1){
print "Your password is correct! Access granted.";
}elsif($Access == 0){
print "Your password is incorrect! Access denied.";
}



BillKSmith
Veteran

May 4, 2014, 8:21 PM

Post #2 of 2 (3060 views)
Re: [ahanif64] why won't my password program work? [In reply to] Can't Post

The short answer is that you must remove the newline from $userPassword.

Code
chomp $userpassword;


I can suggest many improvements to your code.

Start every perl program with

Code
use strict; 
use warnings;


This will require you declare all your variables with "my".

Use grep to search the list @Passwords.

Do not use they syntax &passwordChecker unless it is really needed. Use passwordChecker().

Use else rather than elsif for access denied. (There is no other possibility)

It would be even better to use modules. Some prompt modules even have an option not to echo the password. Use the function 'any' from the module List::MoreUtils rather than grep. It is slightly faster, and the intent is clearer.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives