[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] closing filehandles, redirecting output?



At 07:53 PM 9/30/96 -0500, you wrote:
>>open (USERS, "userfile.dat") || &catastrophicFailure;
>>while ($file=<USERS>) {
>>        chop($file);
>>        $file=~s/\W.*//;               # just get username part
>>        if ($form{'user'} eq $file) {  #$form{'user'} is from cgi parse
>>                print "Content-type: text/html\n\n";
>>                print <<"done";
>>                        <HTML><HEAD><TITLE>Oops, Try Again!</TITLE></HEAD>
>>                        <BODY><H1>Oops, that's already taken</H1>
>>                        <H3>Press your browser's <I>BACK</I> button and
>>                        select a different username</H3>
>>                        </BODY></HTML>
>>                done
>>                exit(0);
>>        } else {
>>                &createPassword;
>>        }
>>close (USERS);
>
>Well, your one problem is the text 'done' has to be flush left.  There can
>be no leading whitespace other than a newline.
>
>Also, you don't close the while() loop with a final }.

Yeah - I found out about both of these during debugging.

>And what does &createPassword do?  If it is supposed to create a password
>if the user is not in the file, it won't work.  The if() statement needs to
>iterate over the entire file through the while() loop.  AFTER the while
>statement is done, it would then proceed to create a password upon not
>finding the user.

Hmm. Well - the actual code right there is:
...
 } else {                              # next we assign the password (FINALL
            $password = &createPassword($form{'firstname'},$form{'lastname'}); 
 } #end if
...

And this seems to work okay. And it's actually nested two file-checks deep
(there is a *real* user file, and a *temp* user file, and I've got it being
opened where &createPassword is, and create password is in an identical 'if'
loop inside it. Right now the code is *extremely* convoluted, but I'm more
interested in figuring out how to actually do it, then I'll worry about a
more efficient way to do it. Like I said - this seems like it works, but
I've not tried too hard to crash it. I'm curious as to why it won't work. I
realize that it creates a password *each* time it doesn't find a match, but
I only care about the last one it *does* create. Again - not super-efficient
(not even a little efficient ;->), but it gets the work done.

>        open (USERS,$filename) || die "$!\n";
>        while ($file=<USERS>) {
>                if ($user eq $file) {
>                        print $stuff;
>                        $x = 1;
>                }
>        }
>        close(USERS);
>        &createPassword if ($x ne 1);
>        exit;

Well, this makes much more sense. Plus I can turn this little snippet into a
subroutine (from open() to close() which returns the value of $x), and call
it for both the real userfile and the temp userfile - which will eliminate
the duplicity in the code.

Thanks Chris, for your help. Maybe someday I'll be able to answer questions
here, too, instead of just posing them. Someday. ;-)

[Daniel]

Daniel Hedrick                                 mailto:daniel@icorp.net
ICorp Web Development                            http://www.icorp.net/
----------------------------------------------------------------------
             *-*-*-*-*-Tuesday, October 1, 1996-*-*-*-*-*
  (1989) Berlin Wall collapses - DDR and BDR unite
  (1938) Germany annexes Sudetenland (1/3 of Czechoslovakia)
(331 BC) Alexander of Macedon defeats Persian army at Gaugamela