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

This is a Tic Tac Toe program I made recently, I used a HTML document with frames that contains the following CGI page and a HTML document (tic2.htm) that refreshes automatically so both players can see when the other makes his move. Well here's the CGI part.
#!/usr/local/bin/perl use CGI ':standard'; print header; print start_html('Tic Tac Toe'), start_form, "Clear the boxes? ", radio_group( -name=>'clear', -Values=>['yes','no',], -default=>'no'), p, "What are you playing? ", radio_group( -name=>'team', -Values=>['X','O',], -default=>'X'), p, "Across: ", radio_group( -name=>'across', -Values=>[1,2,3,], -default=>1), p, "Down: ", radio_group( -name=>'down', -Values=>[1,2,3,], -default=>1), p, submit, end_form, hr; if (param()) { $across = (param('across')); $down = (param('down')); $team = (param('team')); $move = $down."_".$across; $file="../tic2.htm"; $file2="../tic.htm"; open FILE, "<$file" or die "Sorry $!"; open FILE2, ">$file2" or die "Sorry $!"; for $line (<FILE>) { $line =~ s/<! $move ><img src=bla.bmp>/<! $move ><img src=$team.bmp>/g +; #The above checks each line of tic2.htm for the move selected (ex. 1_2 +) and replaces #that squares image bla.bmp with either O.bmp or X.bmp print FILE2 $line; }; close FILE2; close FILE; #Closes the filehandles so that their uses (read, write) can be revers +ed open FILE, ">$file" or die "Sorry no luck opening file"; open FILE2, "<$file2" or die "Sorry"; for $line (<FILE2>) { print FILE $line; }; close FILE; close FILE2; if ((param('clear')) eq 'yes') { open FILE, "<$file" or die "Sorry $!"; open FILE2, ">$file2" or die "Sorry $!"; for $line (<FILE>) { $line =~ s/X/bla/g; $line =~ s/O/bla/g; print FILE2 $line; }; close FILE; close FILE2; open FILE, ">$file" or die "Sorry $!"; open FILE2, "<$file2" or die "Sorry $!"; for $line (<FILE2>) { print FILE $line; }; }; }; print end_html;
Any feedback you could offer would be appreciated, thanks.

Replies are listed 'Best First'.
Re: Seeking feedback on Tic-Tac-Toe
by synapse0 (Pilgrim) on Jul 12, 2001 at 03:20 UTC
    Ok, aside from -Tw and use strict; here's a few other suggestions..
    In your param snarfing part, you might want to use something that works with defaults..
    for example:
    $across = param('across') ? param('across') : $across_default; $down = param('down') ? param('down') : $down_default; $team = param('team') ? pram('team') : $team_default; # $move is safe, will use defaults or set params $move = $down."_".$across;
    CGI::Carp would be a good thing for error reporting (where the file open may die).

    You open files inside if (param()) { but try and close them outside of the block.. it wont error, but it's ugly.
    and lastly, probably more of a nitpick, you have a ton of file opening and closing.. there's probably a better way to handle that (like rewinding file)

    -Syn0
Re: Seeking feedback on Tic-Tac-Toe
by synapse0 (Pilgrim) on Jul 12, 2001 at 02:59 UTC
    for starters (i haven't looked through the whole thing yet) you're missing -Tw switches (taint and warnings) and your missing use strict;
    very bad habits even in casual coding.
    -Syn0
      #!/usr/local/bin/perl -w use strict; use CGI ':standard'; my ($clear,$team,$across,$down,$move,$file,$file2,$line);
      is now the begining of the program, however when I tried to put -Tw in I got a error stating it was to late for -T...
        If you are attempting to run from the command line you must run it as perl -wT scriptname. Also, if running on windows with IIS, you have to either change the association of (.cgi|.pl) to use perl.exe -wT, or simply run without. I run without, but build my scripts and test on my localmachine (from command line) with perl -wT.
        seems to be an issue with IIS, as the_slycer pointed out.
        to get more info, do a super search on "too late for -T".
        -Syn0