I am working on a cgi script for a number guessing game and I want to store the target value in a readable and writeable file. I don’t know how to do that, but I believe I can use a system() call to do it and some type of expression to extract the value from that file. I need help determining that solution; I have the following already:
#!/usr/bin/perl -w
use CGI qw(:standard);
print header, start_html("Guessing Game"), h2("Guessing game"), "\n";
//need some type of system call to store value if one does not exist
//or read it if it does (random value generated below)
srand( time() ^ ($$ + ($$ << 15)) );
my $target = int(rand 100) + 1;
if ( !param() ) {
print hr, "\n", start_form;
print p("Try to guess a number between 1 and 100: ", textfield("guess")), "\n";
print end_form, "\n", hr;
} else {
print hr, "\n", start_form;
my $guess = param("guess");
if ($guess > $target) {
print p ("$guess is too high; try again: ", textfield("guess")), "\n";
} elsif ($guess < $target) {
print p ("$guess is too low; try again: ", textfield("guess")), "\n";
} else {
print p ("You got it: $guess!");
//erase value from file
}
print end_form, "\n", hr;
}
print end_html, "\n";
Use open to open a file.