CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Counter...

 



Rivotti
User

Apr 10, 2000, 1:58 AM

Post #1 of 8 (1176 views)
Counter... Can't Post

Hi:

I have this program that take's information from a Form and them save this information in a file "something.pl". How to create a counter to save files ordered by number?
ex:
0000002.pl # this is the last file saved
0000003.pl # next time someone save is info would be in this file
and so on...

I just need a simple counter to auto save the files in this order.

Thanks
Rivotti


Jasmine
Administrator / Moderator

Apr 10, 2000, 3:09 PM

Post #2 of 8 (1176 views)
Re: Counter... [In reply to] Can't Post

Here's a simple counter program that will keep a counter of 8 digits that can be preceeded by lots of zeros.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl

use strict;

my $number;

if (-e "counter.txt"){
open COUNTER,"<counter.txt";
$number = <COUNTER>;
close COUNTER;
$number++;
}
else {
$number = '00000000';
}

open COUNTER,">counter.txt";
print COUNTER $number;
close COUNTER;

print $number;
exit;
</pre><HR></BLOCKQUOTE>

After you have the new order number ($number), you can create new order files with $number.txt.

Hope this helps -- good luck!


Rivotti
User

Apr 11, 2000, 2:42 AM

Post #3 of 8 (1176 views)
Re: Counter... [In reply to] Can't Post

Thanks Jasmine.

What if several people use the form at the same time. For example: the next number is 00001234 and 10 user enter the form and save their info at the same time. This can cause confusion in counter.txt? Or override data? If so, there is a way to prevent this? I've been reading about FLOCK but I'm a little confused.

Keep the good work Smile
Rivotti

[This message has been edited by Rivotti (edited 04-11-2000).]


Jasmine
Administrator / Moderator

Apr 11, 2000, 6:35 AM

Post #4 of 8 (1176 views)
Re: Counter... [In reply to] Can't Post

Basically flock is file locking. Once a file is opened and flocked, it can prevent another process from opening it and/or writing to it. Then, unlocking it would allow access to the file again.

There is a millisecond that two files could be opening a file simultaneously before flock is invoked. The chances for this are very slim.

To use flock,

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open (FILE, ">counter.txt) or die "Couldn't open counter.txt $!";
flock FILE, 2;
# do stuff to file
close FILE or die "Couldn't close counter.txt $!";
flock FILE, 8;
</pre><HR></BLOCKQUOTE>

Basically the command is flock FILEHANDLE operation. 2 locks the file exclusively and 8 unlocks the file.

Hope this helps!

[This message has been edited by Jasmine (edited 04-11-2000).]


Rivotti
User

Apr 11, 2000, 6:44 AM

Post #5 of 8 (1176 views)
Re: Counter... [In reply to] Can't Post

Thanks Jasmine. Smile
Rivotti.


Cure
User

Apr 11, 2000, 7:43 PM

Post #6 of 8 (1176 views)
Re: Counter... [In reply to] Can't Post

Hi

(GENERAL)->When your program reads or writes files in a multitasking world-> You would want to protect both reads and writes
with some type of locking. You would ask for an exclusive lock when
your about to write to a file and a shared lock when your about to read from the file.
The reason why -> You would want an shared lock for a read is to ensure that you
do not get some half flushed data from some process that might still be
writing to the file. BUT if you have a shared lock and a exclusive lock is called there you might get a file that might not have data in it.


Note:: Lock-Mode Number Description
LOCK_SH 1 Shared lock
LOCK_EX 2 Exclusive lock
LOCK_UN 8 UNLOCK
LOCK_NB 4 Nonblocking lock

RECOMMENDATION: I would use the Fcntl module(- load the C Fcntl.h defines) to handle youe file-locking.

Example::
use Fctnl ":flock";

open(CURE,>>flocktest.txt" or warn $!;
flock(CURE,LOCK_EX) or warn $!;
$in=<CURE>;
flock(CURE, LOCK_UN) or warn $!;
close(CURE);


Cure




Rivotti
User

Apr 12, 2000, 1:40 AM

Post #7 of 8 (1176 views)
Re: Counter... [In reply to] Can't Post

Thanks for the Lesson Cure. It was very usefull. Smile
Rivotti


Cure
User

Apr 12, 2000, 1:28 PM

Post #8 of 8 (1176 views)
Re: Counter... [In reply to] Can't Post

Your Welcome Rivotti

Cure

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives