• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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.
0
bhagawatula
Asked:
bhagawatula
  • 4
  • 2
  • 2
  • +5
1 Solution
 
jhurstCommented:
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);
0
 
bhagawatulaAuthor Commented:
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
0
 
ozoCommented:
Is there a maximum length to the records in the file?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bhagawatulaAuthor Commented:
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.
0
 
PC_User321Commented:
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";
0
 
PC_User321Commented:
The 'pop' gets rid of the empty line at the end of the file.
0
 
Kim RyanIT ConsultantCommented:
# 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);



0
 
pitonyakCommented:
My concern is that the entire file is read in each time.. I see this as a problem...  Consider the following program below.

Notice that this will seek to the end and then backup... This means that your file can be big and it is still ok.
Also note that BEFORE I write a new line I write a \n because I am assuming that this hasnot already been done. If it has, the code still works...

Cheers..

Andy


use IO::file;

my $fh = new IO::File;
my $file_name = 'tst';
my $max_line_len = 100;

if ($fh->open("+<$file_name")) {

    my $file_size = ($fh->stat())[7];
    my $new_pos = $file_size - $max_line_len;
    print "File Size = $file_size\n";
    my $next_num = 1;

    if ($file_size > 0) {
        $fh->seek($new_pos, 0) if $new_pos > 0;
        while (<$fh>) {
            my ($tmp) = /^\s*(\d+)/;
            $next_num = $tmp if defined($tmp);
        }
    }
    ++$next_num;
    print "New number is $next_num\n";
    # If you do not do this you can not
    # write because we have an eof
    $fh->seek(0, 2);
    print $fh "\n$next_num This is a new line";


    $fh->close();
} else {
    print "Unable to open file $file_name because $!\n";


undef $fh;
0
 
jhurstCommented:
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.
0
 
bhagawatulaAuthor Commented:
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.
0
 
lambdaCommented:
I agree with jhurst... write the last number (or incremented by one) into another file & read it.
0
 
alien_life_formCommented:
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
 
0
 
bhagawatulaAuthor Commented:
Thank you to all the experts who gave the input.
Regards
Hemanth
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now