I am trying to read and write from a file but it only seems to write to the file but not read it while in the shell-konsole. If I run the script through my web browser it will not read or write to data.txt. I just need to know how to carry over the two variables from the data.txt I realize it is probably a simple solution I just don't know even the proper terminology for what I want to do so I can't google it

#!/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; } if ($i == 1){ open (DICE, ">data.txt"); print DICE "$firstroll1 $firstroll2"; close DICE; }elsif ($i > 1){ open (DICE, ">data.txt"); 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>";

I know how to keep the variables in other ways but I would really rather learn to do it this way


In reply to read write from a file by friar tux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.