I don't mean to offend you, usually i'd not have answered at all, but: You haven't thought before you wrote that code, apart from the fact that it shows lack of basic knowledge about Perl 5.
But it may help you if i'm constructive. Let's strip syntax errors and nonsense:
sub verify { # open FILE, "$user_file" || die "Can't open user's login files: $!" +; # the previous line doesn't obey operator precedence rules # see `perldoc perlop` # as well as it uses "" for nothing.. open FILE, $user_file or die "... $!"; while(<FILE>) { my ($user,$pass)=split(/\|/); # foreach $user($user) { # the previous line does exactly nothing but aliasing $user # to $user, that's really quite useless # See `perldoc perlsyn` # if ($user eq $q->param('ueq &encrypt_pass) { # hmm? i try to guess this might be if( $user eq $q->param('u') ){ # &good; # you're calling a sub and i'm sure you don't know what the # & means and does. See `perldoc perlsub` # Apart from that this routine will sometime return and # you want to go on reading the file then? # } else { &bad; # same problem + you will call 'bad' for every name in the # file that doesn't equal the submitted username. # Is that your intention? } } } close FILE; }
After all, Perl is only a procedural language and it will do what you tell it to and Perl won't do much more and nothing less. You have to know which steps have to be taken to achieve what you want and that's what you tell Perl. If you want it differently, use Prolog.
Try to formulate the procedure, use pseudo-code:
for each line in the file split up into name and password if the name is the needed name verify the password if it is correct end the loop successfully otherwise cry for help or simply die of error ... if the name doesn't match go to the next line
This directly translates to Perl:
while( <FILE> ){ # = for each line of the file chomp; # (remove trailing newline) my ($name,$pass) = split /\|/; # = split up into name and password if( $name eq $wanted_name ){ # = if the name is the needed name if( $pass eq $submitted_passord ){ # = verify the password # = if it is correct last; # = end the loop successfully } else { # = otherwise die "bastard $name gave me wrong password!\n" # = cry for help or simply die of error ... } } # = if the name doesn't match # go to the next line }
But before you copy that code and try to put it into your script now, be self-ciritcal. You need to learn the basics of Perl and some procedural programming. Then you should write a lot of small programs to grok concepts of clean design, because your code has also problems with more abstract aspects of programming: you don't use function parameters, you don't localize handles/variables, you try to do everything on globals.
CGI is, although a lot of dumb AOL-Kiddies think differently, NOT the most trivial programming environment and you're (sorry) far from "working" on that.
Try Learning Perl if the manpages don't get you started, but try something before you go on writing "CGI programs" that will never work correctly.
Thanks.
--In reply to Re: Reading from multi line file -> CGI.pm
by fruiture
in thread Reading from multi line file -> CGI.pm
by FireBird34
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |