Re: prob w/ gatekeeper subroutine
by toolic (Bishop) on Sep 29, 2010 at 15:53 UTC
|
It always sets $x to 'fail' for me, probably because I do not populate the hash. You should show a complete code example that any of us can run: how you call your sub, how you know if it passes or fails, how you initialize your hash. Also, tell us what you enter on the keyboard once you run it.
| [reply] |
|
$x = ();
%userpass = ('john','ball','tim','super','matt','password','dan','nad3
+2');
&entersys;
if($x == "pass")
{
print("Welcome to the system!\n");
}
else
{
print("That user/password is incorrect\n");
}
I use them as global variables and a global hash so that the subroutines permanently change them. | [reply] [d/l] |
|
Use string comparison operator (eq) instead of numeric (==).
if($x eq "pass")
Equality Operators
| [reply] [d/l] [select] |
|
| [reply] |
|
(The error regarding the check for numerical vs. string equality has been pointed out and was not contained in your original post, so I ignore it in my response.)
I believe you are encountering a variable scoping issue. Make sure your hash contents are visible to the subroutine (which they aren't unless you assign the correct global scope to the hash variable). The following declaration of the hash inside a naked block will throw an error as the hash is unknown to the subroutine.
#! /usr/bin/perl -w
use strict;
{
our %hash;
$hash{ 1 } = "OK";
&routine;
}
sub routine
{
print $hash{ 1 };
}
Hope this helps.
Pat
| [reply] [d/l] |
Re: prob w/ gatekeeper subroutine
by GrandFather (Saint) on Sep 29, 2010 at 20:54 UTC
|
As others have suggested: Always use strictures (use strict; use warnings; - see The strictures, according to Seuss)!
You seem to be writing Perl circa 4.x rather than 5.x. There were many enhancements made to Perl between 4.x and 5.x with most of them improving robustness of code in various ways (using strictures is an example of that). A few things we do differently now:
- Use chomp instead of chop. chop simply removes the last character - whatever it was. chomp removes the a trailing copy of the contents of $/ (the input record separator special variable), normally a \n.
- You don't need to initialise variables - Perl always does that by setting them to undef.
- Don't call subs using &. That has a special meaning and will most often result in surprises - surprises are generally bad.
- Don't use global variables. In small programs it's not such an issue, but as programs grow global variables make it very difficult to figure out how things work.
True laziness is hard work
| [reply] |
|
Thanks for everyone's responses. I am actually using Perl v 5.8.8. Also, what would I call a subroutine with other than &? I am currently learning to use strict but as this is an assignment, I need to get it to work before the due date and I cannot spend the extra time on learning strict before I have to turn it in. Also, if I were to stop using global variables, how would I change the admin password in this program. At the moment I have a subroutine that changes the global variable $adminpass.
| [reply] |
|
use strict;
use warnings;
main (); # Only to ensure no global variables are used - look ma, no v
+ariables
sub main {
my ($dogName, $color) = getDogData ();
print "The $color dog's name is $dogName\n";
($dogName, $color) = getDogData (3);
print "The $color dog's name is $dogName\n";
}
sub getDogData {
my ($select) = @_;
my @names = qw(woof k9 fred max);
my @colors = qw(spotted white black brown);
$select = rand (@names) if ! defined $select;
return $names[$select], $colors[$select];
}
Prints:
The black dog's name is fred
The brown dog's name is max
Please note that using strictures is not something you do when you have time, it is something you do to save time! Strictures tell you you have done something wrong early and can save huge amounts of time that would otherwise be spent poking around in the dark after black beetles with your eyes closed.
Update: In a follow up question weglarz asked 'What does the "qw" in "my @names = qw(woof k9 fred max);" do?'. See Quote-Like Operators in perlop for a description of Perl's various quote operators like qw.
True laziness is hard work
| [reply] [d/l] [select] |
Re: prob w/ gatekeeper subroutine
by MidLifeXis (Monsignor) on Sep 29, 2010 at 16:44 UTC
|
This is not directly related to your problem, but may I suggest hashing your passwords?
if ($hasheduserpass{$euser} eq hashfunction($epass))
{
...
}
Unless you need the unencrypted password elsewhere in the program (may I suggest, then, that you evaluate why you need it), you should not store your user's passwords in the clear. At least in my opinion.
Update: OTOH, if this is an assignment, as it appears it could be, hashing is probably not appropriate. Remember the above advice (and not just for passwords, but for any information that you only use for validation) once you get beyond classroom assignments.
| [reply] [d/l] |
|
sub adduser
{
print("Please enter the admin password: ");
$epass = <STDIN>;
chop($epass);
if($epass eq $adminpass)
{
print("Please enter the user you would like to add: ");
$usertoadd = <STDIN>;
print("Please enter the password for the user: ");
$passtoadd = <STDIN>;
$userpass{$usertoadd} = $passtoadd;
print("That password has been added to the system\n");
}
else
{
print("Wrong admin password\n");
}
}
print("Would you like to: \n");
print("1. Add new user\n");
print("2. Change admin password\n");
print("3. Enter the system\n");
$select = <STDIN>;
if($select == 1)
{
&adduser;
}
Also the $adminpass is set to 'root'. | [reply] [d/l] |
|
| [reply] |
|
By "hashing" MidLifeXis means crytopgree-phying your passwords using some algorithm. A similar conceptual-algorithmic method is used in what computer science calls "hash tables" via which perl implements the "hash" datatype in the underlying C code using arrays, since C does not have such a native datatype. But really this is a coincident of nomenclature, and if you have not been asked
to do it, don't, it has nothing to do with perl hash syntax. Do:
use strict;
use warnings;
| [reply] [d/l] |