Solved

Record number, file locking

Posted on 2000-03-23
13
203 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
ID: 2651054
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
ID: 2651089
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
ID: 2651129
Is there a maximum length to the records in the file?
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 1

Author Comment

by:bhagawatula
ID: 2651171
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
ID: 2651250
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
ID: 2651263
The 'pop' gets rid of the empty line at the end of the file.
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 2651277
# 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
ID: 2651458
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
ID: 2651550
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
ID: 2651785
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
ID: 2652255
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
ID: 2654170
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
ID: 2662166
Thank you to all the experts who gave the input.
Regards
Hemanth
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
transpose into pipe delemited 8 69
Rename file based on contents in the file? 3 88
perl CPAN issue 3 109
Perl string filter 5 79
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

772 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