in reply to Re: read write from a file
in thread read write from a file

I tried the or die line before and it kept causing a internal server error. as far as permissions the data.txt is chmod 666 and the dice.cgi is chmod 755. I tried you suggestions. now the screen just comes up blank with no errors!! lol any other suggestions any help is greatly appreciated :)

Replies are listed 'Best First'.
Re^3: read write from a file
by wind (Priest) on Apr 08, 2011 at 18:17 UTC

    What was the internal server error?

    If it's throwing an error because you add the or die statement, then there's a problem with your open. Most likely dealing with write permissions. Uncomment this line, and share the error with us:

    #use CGI::Carp "fatalToBrowser";

      lol I got it for some reason chmod 777 data.txt fixed the problems. I am posting the code to because I am not sure what went wrong the data.txt was already chmod 666 changing to 777 shouldnt effect it that much. thanks for your help

      #!/usr/bin/perl use CGI ':standard'; #use CGI::Carp "fatalToBrowser"; $i=param('i'); $i++; print header; $dice1=int(rand(6)+1); $dice2=int(rand(6)+1); #this line is to see if it carries over the $i variable #print "this is $i"; if($i ==1) { $firstroll1=$dice1; $firstroll2= $dice2; } #don't forget to chmod 777 data.txt and 755 dice.cgi if ($i == 1){ open (DICE, ">data.txt")or die print "ERROR $!";; print DICE "$firstroll1 | $firstroll2"; close DICE; }elsif ($i > 1){ open (DICE, "data.txt")or die print "ERROR $!"; while (<DICE>) { ($firstroll1,$firstroll2)=split (/\|/); } close DICE;} print "$firstroll1 and $firstroll2"; print "<center> <table border=1> <tr><td><img src=http://localhost/pix/dice$dice1.gif> <td><img src=http://localhost/pix/dice$dice2.gif> </tr> </tables>"; print "$firstroll1 and $firstroll2"; if (($dice1 + $dice2 ==7) or ($dice1 + $dice2 ==11)) { print "you win"; print "<hr><a href = http://localhost>play again?</a>"; exit() }elsif (($dice1 + $dice2 ==12) or ($dice1 + $dice2 ==2)) { print "you lose"; print "<hr><a href = http://localhost>play again?</a>"; exit() }elsif (($i > 1) and ($dice1 ==$firstroll1) and ($dice2 == $firstroll2 +)) { print "match"; print "<hr><a href = http://localhost>play again?</a>"; exit() }elsif (($i > 1) and ($dice1 ==$firstroll2) and ($dice2 == $firstroll1 +)) { print "match"; print "<hr><a href = http://localhost>play again?</a>"; exit() } #print "this is roll number $i"; print "<a href=dice.cgi?&i=$i>Roll Again?</a>";

        Cool, here's a few suggestions

        • 1) Add use strict; and use warnings; to the beginning of every script. This will require you to declare all your variables with my which will help you avoid spelling errors in your code.
        • 2) use CGI::Carp 'fatalsToBrowser': You simply misspelled fatalsToBrowser in your commented out line.
        • 3) Don't use a file to save state at all, instead pass additional cgi parameters along with 'i'

        Here's a quick clean up of your code with the above suggestions:

        #!/usr/bin/perl use CGI ':standard'; use CGI::Carp "fatalsToBrowser"; use strict; use warnings; my $i = 1 + (param('i') || 0); my $firstroll1 = param('roll1'); my $firstroll2 = param('roll2'); print header; my $dice1 = 1 + int rand 6; my $dice2 = 1 + int rand 6; print "($i) $dice1, $dice2<br>"; if (!$firstroll1 || !$firstroll2) { $firstroll1 = $dice1; $firstroll2 = $dice2; my $sum = $dice1 + $dice2; if ( $sum == 7 || $sum == 11 ) { print "you win"; print qq{<hr><a href="dice.cgi">play again?</a>}; exit; } elsif ( $sum == 2 || $sum == 12 ) { print "you lose"; print qq{<hr><a href="dice.cgi">play again?</a>}; exit; } } else { if ($dice1 == $firstroll1 && $dice2 == $firstroll2 ) { print "match"; print qq{<hr><a href="dice.cgi">play again?</a>}; exit; } elsif ($dice1 == $firstroll2 && $dice2 == $firstroll1 ) { print "match"; print qq{<hr><a href="dice.cgi">play again?</a>}; exit; } } print qq{<a href="dice.cgi?i=$i&roll1=$firstroll1&roll2=$firstroll2">R +oll Again?</a>};