Adam Butler wrote:
I forgot to mention that $file is set via a form. On first run of theThe source of "$file" doesn't really matter, but I hope you are sanitizing that input!
script, it prints a form to the browser asking for a file name, which you
enter, and then it's submited and sent to the script. The way I understood
it, when open tries to open a file that doesn't exist, it creates it.
Thanks,
Open(GAMELOG, "$file");
@entries = <GAMELOG>;
close(GAMELOG);
This snippet will not create the file if it doesn't exist. As mentioned before, you can open a file for read or write. When opening for write, the file will be created if it does not exist but what would be the point of this if you are only reading the file?
If this is actually what you want, add a test routine that creates the file if it doesn't exist:
if (!( -f $file )) { # create the file open(FH, "> $file"); close(FH); }
open(GAMELOG, "$file") || die "$!\n"; @entries = <GAMELOG>; close(GAMELOG);
If you want to just test for the file instead of creating it too:
if ( -f $file ) {
open(GAMELOG, "$file") || die "$!\n"; # you may still die with permission problems even though it exists.
@entries = <GAMELOG>;
close(GAMELOG);
}
-- cs
You've had your answer. in your script 'Open' means nothing and the file won't be created even if you use 'open'.
#!/usr/bin/perl chdir "/tmp"; $log = "game.log"; open LOG, ">$log" or die $!; # ---> > ! print LOG "success !"; close LOG; open LOG, $log; for (<LOG>) { print };
Make it a rule NEVER to open a filehandle without testing.
JD