in reply to read write from a file

Maybe you don't have permissions. Add error checking to your open statements:

open(DICE, ">data.txt") or die "Can't open: $!";

And probably good to use lexical file handles and the three parameter form of open.

open my $fh, '>', 'data.txt' or die "Can't open: $!";
Also, to read, you need to remove the '>' from your open:
open(DICE, "data.txt");

Replies are listed 'Best First'.
Re^2: read write from a file
by friar tux (Novice) on Apr 08, 2011 at 18:09 UTC

    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 :)

      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>";