> -----Original Message----- > From: Rafael Cotta [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, May 07, 2002 2:15 AM > To: [EMAIL PROTECTED] > Subject: Re: Little code fragment request (Is this code ok?) > > > Well, after searching I come to this: > > #------------------- > sub WriteLog > { > open(OUTF,">>$filenamedate.dat") or die("Couldn't open > $filenamedate.dat > for writing: $!"); > > # This locks the file so no other CGI can write to it at the > # same time... > flock(OUTF,2); ^^^ You should import the constants from Fcntl instead of using magic numbers:
use Fcntl qw(:flock); > > # Reset the file pointer to the end of the file, in case > # someone wrote to it while we waited for the lock... > seek(OUTF,0,2); ^^^^^^^^^^^^^^ This is unecessary. When a file is opened in append mode, writes always and only go to the end. > > print OUTF "$linha\n"; > close(OUTF); > } > #------------------- > > Where $filenamedate will have something like "2002-05-06" and > $linha will > contain the information I need to append to the file. > > Will this code run fine on a website with several > simultaneous accesses? Additional considerations: 1. Where is $filename coming from? If it's from outside the script, you should run with -T and properly untaint it. 2. You shouldn't assume any particular current working directory in the CGI environment. You need to specify a path name in your open() or use chdir() to set a specific directory. 3. Perl 5.6 allows you to use the following form for open: open(my $fh, ">>$filename.dat") or die ... This makes your code more modular as it avoids use of a global filehandle. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]