Hi folks,
Please look at the following code, where I'm trying to demonstrate the file locking concept through Perl/CGI.
Here is the script which I tried.
Code:
#!/usr/bin/perl -w
use strict;
use warnings;
print "Content-type: text/html\n\n";
print <<EOF;
<html>
<head>
<title>File Locking</title>
</head>
<body bgcolor = "#556677"></body>
<br><br>
<center><h1>Demonstrating File locking concept</h1></center>
EOF
my $myfile = "Employee.txt";
my $employee = "Manoj";
open(MYFILE, "$myfile");
flock(MYFILE, 1);
while(defined($a=<MYFILE>)) {
if ($a=~m/^$employee$/) {
print "<center>You are already in the list!\n</center>";
exit;
}
}
flock(MYFILE,8);
close(MYFILE);
open(ASH, ">> $myfile");
flock(ASH, 2);
print ASH $employee;
print "<center>You are now in the list!\n</center>";
flock(ASH,8);
close(ASH);
print "<body><html>";
Here, I'm not able to append the text and also unable to lock(shared lock) the file.
For your information, this is executing fine when I run the same as as
But when I run the same script in a browser, I'm not getting it fine.
Please assist me.