Solved

Record number, file locking

Posted on 2000-03-23
13
183 Views
Last Modified: 2010-03-05
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
Comment
Question by:bhagawatula
  • 4
  • 2
  • 2
  • +5
13 Comments
 
LVL 8

Expert Comment

by:jhurst
Comment Utility
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
 
LVL 1

Author Comment

by:bhagawatula
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Is there a maximum length to the records in the file?
0
 
LVL 1

Author Comment

by:bhagawatula
Comment Utility
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
 
LVL 5

Expert Comment

by:PC_User321
Comment Utility
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
 
LVL 5

Expert Comment

by:PC_User321
Comment Utility
The 'pop' gets rid of the empty line at the end of the file.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 19

Expert Comment

by:Kim Ryan
Comment Utility
# 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
 
LVL 5

Accepted Solution

by:
pitonyak earned 100 total points
Comment Utility
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
 
LVL 8

Expert Comment

by:jhurst
Comment Utility
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
 
LVL 1

Author Comment

by:bhagawatula
Comment Utility
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
 
LVL 1

Expert Comment

by:lambda
Comment Utility
I agree with jhurst... write the last number (or incremented by one) into another file & read it.
0
 
LVL 3

Expert Comment

by:alien_life_form
Comment Utility
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
 
LVL 1

Author Comment

by:bhagawatula
Comment Utility
Thank you to all the experts who gave the input.
Regards
Hemanth
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now