Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Variable/if question...

by tretin (Friar)
on Nov 04, 2001 at 12:05 UTC ( [id://123148]=perlquestion: print w/replies, xml ) Need Help??

tretin has asked for the wisdom of the Perl Monks concerning the following question:

Monks,
I have once again come to harken at your gates for an answer to one of my many problems.

I just finished going through both the file input/output and looping tutorials and decided I'd try to write up a script that incorporated both aspects. I'm trying to create a simple login script, that prompts the user for his/her name and then the password, then runs a check to see if the names/passwords match; if they do then a file is opened and the lines displayed. I've gotten it to recognize correct/incorrect password/username identification, but I'm having trouble with the opening of the file. If the user/pass check is successfull the file does not open, and if it is unsuccessful it does not open. Maybe it's just too late for me to be attempting to script but here is what I've gotten thus far:
#!/usr/bin/perl -w my $cusr; $cusr=bob; my $cpword; $cpword=pass; print "What is your username? "; $usr=<>; chomp($usr); if($usr==$cusr){ print "One moment please...\n "; print "Please enter your password:\n "; $pword=<>; chomp($pword); if($pword==$cpword){ print "One moment please...\n "; print "Authentication Complete!\n "; } } if($pword==$cpword){ open (FILE, "memberlist.txt"); while(<FILE>){ push @lines,$_; close FILE; } } else{ print "ERROR: username or password incorrect, please try again\n " +; }
and specifically (or at least I'd assume specifically)where the problem lies, in the following lines :
if($pword==$cpword){ open (FILE, "memberlist.txt"); while(<FILE>){ push @lines,$_; close FILE; } }
That's it... and I can't seem to find the problem. Any help at all is immensly appreciated, and I ask that you please bare with me, as I am very very new to the world of Perl (and of programming on the whole) so if there's an obvious problem here please don't laugh too hard at my incompetence :).

Thank you again for your time monks, and again I appreciate any help you can give.

just my 5 cents (ran out of pennies!)

Edit: chipmunk 2001-11-04

Replies are listed 'Best First'.
Re: Variable/if question...
by mkmcconn (Chaplain) on Nov 04, 2001 at 12:24 UTC

    Indomitus beat me to the submit button, I see.

    if($pword eq $cpword){ open (FILE, "< memberlist.txt") or die $!; while(<FILE>){ push @lines,$_; } close FILE or die $!; }
    • You'll be able to zero in on problems like this if you habitually use the die statement, to test whether you've had success.
    • Unless your passwords are numeric, you'll want to test equivalence with 'eq', unstead of '=='.
    • And finally - your code closed the filehandle inside of your while loop (another thing that die would have told you).

    mkmcconn

Re: Variable/if question...
by Indomitus (Scribe) on Nov 04, 2001 at 12:17 UTC
    You should have a <FILE> in the while() loop. The <FILE> tells perl to loop through your file one line at a time. You can also read the whole file into memory by doing @lines = <FILE> but it's up to you if you want to use that memory.
    if($pword==$cpword){ open (FILE, "memberlist.txt"); while(<FILE>){ push @lines,$_; close FILE; } }
    UPDATE: You might also want to try checking your open statement to make sure it worked.
    open (FILE, "memberlist.txt") || die "Cannot open: $!";
    The $! gives you the error message from the open command if it failed.
Re: Variable/if question...
by Amoe (Friar) on Nov 04, 2001 at 16:52 UTC
    Also:
    • You can use @lines = <FILE> to get all the lines of a file in an array.
    • You start off declaring variables with my, but then suddenly stop. Carry on doing it; also, use strict and don't use barewords like $cusr=bob, instead use $cusr='bob'.
    • In my opinion, it's clearer to use <STDIN> whenever you're reading from standard input, apart from when you're using magic while (<>)


    --
    my one true love

    Edit kudra, 2001-11-04 Corrected markup

Re: Variable/if question...
by danger (Priest) on Nov 05, 2001 at 15:05 UTC

    There are a few problems with the code you have posted (as noted by others and which I won't repeat here). However, once all of those little bugs are fixed you also might want to consider reworking the logic of the code. Here is a cleaned up version of your code, largely following your posted code in level and style (ie, not trying to add anything beyond what you are already doing), but rearranging the logic somewhat:

    #!/usr/bin/perl -w use strict; # first initialize necessary variables: my $err_msg = "Authorization Failed: please try again\n"; my $cusr = 'bob'; my $cpword = 'pass'; # next obtain the user's data: my($usr, $pword); print "What is your username? "; $usr = <STDIN>; chomp($usr); print "Please enter your password:\n"; $pword=<STDIN>; chomp($pword); # now we can authenticate or die unless ($usr eq $cusr and $pword eq $cpword) { die $err_msg; } print "Authentication Complete!\n"; # authorized file processing may ensue: my @lines; my $file = 'memberlist.txt'; open (FILE, $file) || die "Can't open $file: $!"; while(<FILE>){ push @lines,$_; } close FILE; # now do whatever with @lines. print @lines; __END__

    I am not saying the above is optimal --- it can be shortened and simplified further. But, we've now broken out the logic into 4 self contained tasks: 1)setting initial values, 2)obtaining user authentication data, 3)perform the authentication or die with a message, 4)read and otherwise process the file. In your original, those last three tasks were all intermixed in the code rather than proceeding logically from one stage to the next. This reordering not only reduces the complexity of the code (no more nested conditionals), it makes it easier to code and test section by section in the first place --- which can greatly help in avoiding (or at least catching early) the scattering of syntactical errors and ommissions that crept into your code. It is also good practice for when you want to break out logical tasks into subroutines.

      I like how that is set up... it is a whole lot clearer than my jumbeled mess at the top, and for some reason the file output was still not working even after I changed it as stated above (however it was more likely due to flaws in my reproduction of the before stated code), so I very much appreciate the cleaned version, and I will work from there to add more on to this script. Oh, and thanks, now I understand unless statements a bit better, and see a reason for their existance (which I previously was searching for)Thanks again!

      Make something idiot proof and they'll make a better idiot...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://123148]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-24 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found