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

Re: [MacPerl] closing filehandles, redirecting output?



>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 }.

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.

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

>Since I'm fairly new (WAY NEW) to MacPerl and Perl in general, when I first
>coded this I was worried about printing with <USERS> open, but now that I
>think about it, the print statements are directed to the handle so it's not
>a problem.

Yes, there is no problem with printing while filehandles are open.  The
only problem is printing to a filehandle that is not open for writing.


>What might be a problem is that I'm performing an "exit(0)"
>before I "close(USERS)"
>
>Is *THAT* bad, or will exit(0) force a close?

exit forces a close, so you don't have to worry about it.


#================================================================
perl -e 'srand();if(rand>.5){$i=0;foreach(@ARGV){@$i=split(//);$z
[$i]=0;foreach$l(@$i){$s[$i][$z[$i]]=$l;$z[$i]++;}$i++;}foreach$m
(@s){foreach$g(@{$m}){print$g}print" ";$m++}}else{print(join(" ",
@ARGV))}print"\n"' McClellan Clan Motto: Think On

Chris Nandor                                       pudge@pcix.com
PGP Key 0xB76E72AD                    http://pudge.petersons.com/