Chapter 13 – Perl Example Login Script

In the past few lessons you learned about Perl and the various datatypes. You learned scalars, arrays and hashes. It is a lot to learn and unless you make a practical script that utilizes these variables and the techniques you learned, it’ll make learning and memorizing their appearance and functions very difficult.

In this lesson you will be learning how to create a password script that you can run from your command prompt. This example shows examples on scalars, arrays AND hashes so you can see how they work in the real world.

This script was written as simple as possible trying to only cover functions you have already learned about. There may be a few new things you come across, so if you have questions be sure to read the step-by-step followup.

This login script will loop until you log in successfully. It will prompt for a username and password and when successful, it will print a to do list.

The code (with line numbers):

01: #!/usr/bin/perl
02:
03: use warnings;
04: use strict;
05:
06: my %logins = (“name1″ => “pass1″, “name2″ => “pass2″);
07: my @to_do_list = (“walk the dog”, “paint the house”, “walk to the store and get some pop”, “walk on the moon”);
08:
09: while (1)
10: {
11: print “Enter your login name:”;
12: my $name = <STDIN>;
13: chomp($name);
14:
15: if (exists $logins{$name})
16: {
17: print “\nwhat is the password?”;
18: my $pass = <STDIN>;
19: chomp($pass);
20:
21: my $password = $logins{$name};
22:
23: if ($pass eq $password)
24: {
25: print “You have logged in successfully\n\n”;
26:
27: foreach my $list (@to_do_list)
28: {
29: print “$list\n”;
30: }
31:
32: exit;
33: }
34: else
35: {
36: print “Your login information was incorrect.\n”;
37: }
38: }
39: else
40: {
41: print “Username was not found in our system\n”;
42: }
43: }

Line by line Explanation:

lines (1-4)
Setting up our script and turning on warnings and strict to help us debug in the off-chance something goes terribly wrong.
lines (6-7)
We are setting up our hash of usernames and passwords for the login process and our list of to-do items which will print out after you are logged in.
lines (9-10 & 43)
Since this is a login script, we probably want to loop continuously if they don’t input the write username or password. Otherwise you’d have to start the script again each time you make a mistake. The while(1) loop loops over the ENTIRE script which is why it’s there before we prompt for user input.
line (11-13)
We are printing a message to the user that we want their username. On line 12 we are using <STDIN> which means the script will pause and await for the user to type something in and then press enter. STDIN means Standard Input. Because you click enter, we need to remove the /n character that comes with the input in like 13 by chomping it off. The input is stored in our variable $name.
line (15-16)
After prompting for their username we need to check their input with what we have stored in our hash %logins that we made earlier. We check this using an if() by checking to see if the name they entered exists by if (exists $logins{$name}).
line (17-19)
We are still in the open brace of our if() statement from line 15 which means everything within these braces WILL be executed if the user inputs a name that exists in our hash. If they are here, they got the name right so we’re asking for their password this time and prompt for more input. Again we must chomp() our variable to remove the newline feed that will be added to the end of the input.
line (21)
We are still in the open brace we were in the last few lines so this is still getting executed if they used the right name. On this line we are making a new variable $password and we’re assigning it to the key of our hash ($logins{$name}) which actually stores the value into our new variable. To get the value of any hash key, all you do is assign a $var = $hashname{“keyname”}; and the $var will hold your data. We are saving this in a variable so we can compare the password the user types in to the password we have on file.
line (23-24)
We are STILL in the open brace from before.. Here we are doing another if() test based on their password this time. We are checking to see if the $pass they entered is the same $password we have on file. eq means equal; we are checking to see if $pass is equal to $password. If it is, the first set of brackets will be executed. If it’s wrong, the second set will.
lines (25)
This is the first bracket which means the password the entered was right for the username they signed in as. Since this is all right, we print them a message letting them know they signed in!
line (27-30)
We created an array (list) of things to do near the top of the script. This list is supposed to be password protected so only YOU could login and see it. And since on lines 23-24 you were authenticated and on line 25 you were told you logged in, we are going to print out the list. Remember, to print @arrays we use a foreach() loop on the array name: foreach(@arrayname). You can assign a loop variable like we did with $list so each iteration through the loop, the array element is stored in $list so we can print it out.
lines (32)
We logged in successfully and printed out our list. Since we are in a while(1) loop, the loop will continue forever continuously until you tell it otherwise. For this reason, we are adding an exit; to make the program stop after we are done.
line (34-37)
We stopped the if() loop we created for the password checking. Now begins the second section of else() which is executed when our test fails. This means the password the user entered was wrong. All we do with the else() is print a friendly message letting them know they failed to sign in with the right credentials.
line (39-42)
Similar to lines 34-37. This else() block is executed if the user typed in a username that did not exist in our hash (from when we prompted them quite a few lines earlier). All we are doing here is printing them a message letting them know their login failed.
line (43)
We close the while(1) loop. Just because we close it, it doesn’t mean it tells the loop to stop. We have to close all open braces. This loop will continue until YOU tell it to.

Complete script (without lines) for copying and pasting:

code#!/usr/bin/perl

use warnings;
use strict;

my %logins = (“name1″ => “pass1″, “name2″ => “pass2″);
my @to_do_list = (“walk the dog”, “paint the house”, “walk to the store and get some pop”, “walk on the moon”);

while (1)
{
print “Enter your login name:”;
my $name = <STDIN>;
chomp($name);

if (exists $logins{$name})
{
print “\nwhat is the password?”;
my $pass = <STDIN>;
chomp($pass);

my $password = $logins{$name};

if ($pass eq $password)
{
print “You have logged in successfully\n\n”;

foreach my $list (@to_do_list)
{
print “$list\n”;
}

exit;
}
else
{
print “Your login information was incorrect.\n”;
}
}
else
{
print “Username was not found in our system\n”;
}
}

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)