我正在为一个猜数游戏编写一个 cgi 脚本,我想将目标值存储在一个可读和可写的文件中。我不知道该怎么做,但我相信我可以使用 system() 调用和某种类型的表达式来从该文件中提取值。我需要帮助确定该解决方案;我已经有以下内容:
#!/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";