Link to home
Start Free TrialLog in
Avatar of bhagawatula
bhagawatula

asked on

Record number, file locking

I need perl code to parse a html form and write it to a flat file, but with a TRACKING number generated along with, which should be n+1 of the previous record.
ex.
1000^email^tel^unix
1001^email^tel^unix  and so on

the rest of the data is from the form, the 1st field needs to be generated.
Also this needs to be locked, so if 2 guys submit, either reject one or wait and try again after 1 sec.


--Thanks in advance.
Avatar of jhurst
jhurst

Assuming that we are discussing onky the file part and you know how to get the data:

open(LOCKFILE,"<yourLockFile"); file used for locking only
flock(2,LOCKFILE); #will lock the file
             #if already locked, will wait for other lock to be freed
open(DATAFILE,">>yourDataFile");

#do whatever
close(DATAFILE);
flock(8,LOCKFILE); #unlock it
close(LOCKFILE);
Avatar of bhagawatula

ASKER

Thanks for the locking part, but my main question still remains, how to generate the next tracking number. if my flat file has the last record 1005, next time I write to the file, It should append the form data, with the number 1006.
-Thanks
Avatar of ozo
Is there a maximum length to the records in the file?
No ozo, I am trying to write a helpdesk call tracking system, where users can open a support request, and the helpdesk can monitor all cases and close cases as and when required, all thro the browser. I am running apache on linux. I already have a app running using 'c', but i wanted to go for perl.
The record is a delemited single line flat record.
OK, this is how you would manage the record numbers:

open (DATAFILE, "< yourDataFile");
@OldData = <DATAFILE>;
pop @OldData if ($OldData[-1] =~ /^$/);
($Track, @Other) = split '^', $OldData[-1];
close DATAFILE;
open (DATAFILE, ">> yourDataFile");
$Track++;
print DATAFILE "$Track^New info\n";
The 'pop' gets rid of the empty line at the end of the file.
# get id of last record
open(FH,"<data.txt");
@all_lines = <FH>;
$last_line = pop(@lines);
($id,$email,$tel,$unix) = split(/\^/,$last_line);
close(FH);

# append a new record
open(FH,">>data.txt");
$new_line = join('^',$id++,$email,$tel,$unix);
print(FH $new_line);
close(FH);



ASKER CERTIFIED SOLUTION
Avatar of pitonyak
pitonyak

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so then - lets put the next tracking number in a file.  Just open another file after the lock, read the last value, increment it and re-write it.
jhurst,
Please note that it is prefered to keep a question open for long enough to get the best results. please try to post your ans. as a comment. this will not lock the question for others.
--Thank you

pitonyak , PC_User321 ,teraplane , jhurst
Thank you all for the help, will try it now.
I agree with jhurst... write the last number (or incremented by one) into another file & read it.
Greetings:

Different take, no-perl-required comment: chuck the whole shebang and get rt Request Tracker: http://www.fsck.com/projects/rt/

This is based on the assumption your goal is to use a tracking system, rather than writing one.

Rt is free, mail/web activated, does all you require, and then some. You'll need mySQL, DBI, and an up-to-date CGI.pm

As per your problem, I agree with the separate file comments. Using a RDB package (postgres, mySQL) would give you more options, (bur more headaches).

Cheers,
    alf
 
Thank you to all the experts who gave the input.
Regards
Hemanth