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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


New User

May 4, 2014, 4:24 PM

Post #1 of 2 (4746 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 :(

#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
$Access = 0


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


May 4, 2014, 8:21 PM

Post #2 of 2 (4657 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.

chomp $userpassword;

I can suggest many improvements to your code.

Start every perl program with

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,


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

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