appending to file appends twice!?

G

Guy

Hi all, :)

I wrote a Perl script to append Questions and Answers to a text data file.
The file would look like this and would grow when Q&As are added.
1 Q This is a question
1 A This is an answer
2 Q This is another question
2 A This is another answer

All I do is open the file for append, print 2 lines (Q line and A line) and
then close the file. What's happening however is that my data is appended
twice, like this: This is an actual cut & Paste of the entire file.

1 Q Here is my question
1 A Hee is my answer
2 Q Here is my question
2 A Hee is my answer
3 Q here is another question
3 A this is another answer
4 Q here is another question
4 A this is another answer
5 Q qqqqq
5 A zaaaaaa
6 Q qqqqq
6 A zaaaaaa
7 Q sdfkhj asdlfh fih asdfh ;alsdfh
7 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef
8 Q sdfkhj asdlfh fih asdfh ;alsdfh
8 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef

What's worst is that you notice on the above file that the count does
increment as if my routine is called twice. So what I've done was added a
few check-point print commands inside the routine and these only print once,
as if my routine is only called once.

Any ideas,
Guy

FYI
They tell me the server is a SUN Sparc V100 with dual 100mb ports.
The Web is provided by an iPlanet webserver.
It is Perl 5.008

Curious
Here is the code for the updating routine...

sub f25
{
my $chk=0;
my $number=0;
my $qaline;
my @qafield;
&prt_head; # Prints HTML code back to browser
$chk=get_qa(); # see below
if($chk==0)
{
foreach $qaline (@qafile)
{
@qafield=();
push(@qafield,split(/\t/,$qaline));
$number=$qafield[0];
}
print"Number = $number <BR> \n"; # checkpoint print command
$number++;
print"Number=$number<BR><BR><BR>\n"; # checkpoint print command
$chk=3840;
if(-d "$fd")
{
$chk=256;
if(open(FILE,">>$qafil"))
{
print"Added<BR>\n";
print FILE "$number\tQ\t$fq\t\n$number\tA\t$fa\t\n";
close(FILE);
print"Done<BR>\n";
$chk=0;
}
}
}
if($chk != 0){erreur($chk);}
print"</FORM></BODY></HTML>\n";
}

sub get_qa
{
my $chk=3840;
if(-d "$fd")
{
$chk=256;
if(open(FILE,"$qafil"))
{
@qafile=<FILE>;
close(FILE);
$chk=0;
}
}
return $chk;
}
 
T

Tad McClellan

Guy said:
if($chk==0)


Space characters are not a scarce resource.

Feel free to use as many as you like to make your code
easier to read and understand:

if ( $chk == 0 )

@qafield=();
push(@qafield,split(/\t/,$qaline));
$number=$qafield[0];


You can replace those 3 lines with one line that uses a "list slice":

my $number = (split(/\t/,$qaline))[0]; # untested

if(-d "$fd")
^ ^
^ ^
A useless use of double quotes:

perldoc -q quot

What's wrong with always quoting "$vars"?
 
W

Walt Mankowski

Hi all, :)

I wrote a Perl script to append Questions and Answers to a text data file.
The file would look like this and would grow when Q&As are added.
1 Q This is a question
1 A This is an answer
2 Q This is another question
2 A This is another answer

All I do is open the file for append, print 2 lines (Q line and A line) and
then close the file. What's happening however is that my data is appended
twice, like this: This is an actual cut & Paste of the entire file.

1 Q Here is my question
1 A Hee is my answer
2 Q Here is my question
2 A Hee is my answer
3 Q here is another question
3 A this is another answer
4 Q here is another question
4 A this is another answer
5 Q qqqqq
5 A zaaaaaa
6 Q qqqqq
6 A zaaaaaa
7 Q sdfkhj asdlfh fih asdfh ;alsdfh
7 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef
8 Q sdfkhj asdlfh fih asdfh ;alsdfh
8 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef

What's worst is that you notice on the above file that the count does
increment as if my routine is called twice. So what I've done was added a
few check-point print commands inside the routine and these only print once,
as if my routine is only called once.

Any ideas,

The code looks OK, so chances are it is being called twice somehow.

This looks like CGI code. Is it possible the script itself is being
called twice? What do your weblogs say?

Walt
 
G

Guy

Well I think I found the cause, but I still don't understand what's wrong...
(BTW, I will read up on perldoc -q quot, thanks for that :)

I recently added a locking scheme to prevent 2 instances of my Perl script
from updating the text data files at the same time.

Basically it locks a dummy file for exclusive - once the file is locked it
does its stuff - then it closes that dummy file.

In any case, it turns out that if I don't call those locksys and unlocksys
routines of mine, this problem doesn't occur.
Am I doing anything wrong.
The only thing I can see different is that I have 2 files open at the same
time: LCKFILE and FILE
Do I need to so anything before opening a 2nd file or using a 2nd file
handle?

sub locksys
{
my $chk=3840;
if(-d "$fd")
{
$chk=3584;
if(open(LCKFILE,">$lockfil"))
{
$chk=flock(LCKFILE,2);
}
}
return $chk;
}

sub unlocksys
{
close LCKFILE;
}

---------------------
Once that dummy file is locked, I open , read, and close other files using
FILE, for example:
--------------------

sub get_qa
{
my $chk=3840;
if(-d "$fd")
{
$chk=256;
if(open(FILE,"$qafil"))
{
@qafile=<FILE>;
close(FILE);
$chk=0;
print"QAFILE = @qafile<BR><BR>\n";
}
}
return $chk;
}

sub f25
{
my $chk=0;
my $number=0;
my $qaline;
my @qafield;

&prt_head;

$chk=get_qa();
if($chk==0)
{
$chk=256;
if(open(FILE,">$qafil"))
{
$chk=1;
foreach $qaline (@qafile)
{
@qafield=();
push(@qafield,split(/\t/,$qaline));
if($qafield[0] == $fn)
{
$qaline="$fn\t$fq\t$fa\t\n";
$chk=0;
}
print FILE $qaline;
}
if($chk==1)
{
$fn=$qafield[0]+1;
$qaline="$fn\t$fq\t$fa\t\n";
print FILE $qaline;
}
close(FILE);
$chk=0;
}
}
if($chk != 0){erreur($chk);}
print"</FORM></BODY></HTML>\n";
}
 
B

Brian McCauley

@qafield=();
push(@qafield,split(/\t/,$qaline));
$number=$qafield[0];


You can replace those 3 lines with one line that uses a "list slice":

my $number = (split(/\t/,$qaline))[0]; # untested

Or, better still, a list assignment:

my ($number) = split(/\t/,$qaline); # untested

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
W

Walt Mankowski

Well I think I found the cause, but I still don't understand what's wrong...
(BTW, I will read up on perldoc -q quot, thanks for that :)

I recently added a locking scheme to prevent 2 instances of my Perl script
from updating the text data files at the same time.

Basically it locks a dummy file for exclusive - once the file is locked it
does its stuff - then it closes that dummy file.

In any case, it turns out that if I don't call those locksys and unlocksys
routines of mine, this problem doesn't occur.
Am I doing anything wrong.
The only thing I can see different is that I have 2 files open at the same
time: LCKFILE and FILE
Do I need to so anything before opening a 2nd file or using a 2nd file
handle?

This code looks fine as well. I think the locking here is a symptom, not the
cause of your problem. My theory remains that somehow your Perl script is being
called twice. If you have locking, then they're effectively single-threaded
and update your file one after another. Without locking they both run
simultaneously and whichever process happens to finish last overwrites the work
of the one that finished first.

Have you checked your weblogs yet to see if the perl script is being hit twice?

Walt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top