Newbie Question

Eugene Tsyrklevich eugene at
Sat Aug 12 17:35:25 CDT 2000

On Sat, Aug 12, 2000 at 01:01:32PM -0700, Ken Loomis wrote:
> I just started learning Perl and found the San Diego Perl Mongers group.
> Trying to re-arrange my schedule to make it to this month's meeting, but I
> have a challenge today that I need some help that I would be most
> appreciative of.
> I use a small data file to keep the Customer_ID number in. Each time a new
> customer registers, I increment the number in that file. However, we are
> getting so many requests, that at time the number gets reset to 1. I am
> pretty sure this is because of the way the flock works.

you will need 4294967296 hits in order for your counter to reset

> I checked out the FAQ's and found this code segment for handling this:
>     use Fcntl;
>     sysopen(FH, "numfile", O_RDWR|O_CREAT)       or die "can't open numfile:
> $!";

fyi, man perlopentut

>     flock(FH, 2)                                 or die "can't flock numfile:
> $!";
>     $num = <FH> || 0;
>     seek(FH, 0, 0)                               or die "can't rewind
> numfile: $!";
>     truncate(FH, 0)                              or die "can't truncate
> numfile: $!";
>     (print FH $num+1, "\n")                      or die "can't write numfile:
> $!";
>     close FH                                     or die "can't close numfile:
> $!";

be careful about using 'or' and '||'.. they have different precedence and
act differently in different situations.
man perlop for more

> I ran a test of this from one domain that I host and it worked fine. However
> when I incorporated it into the client's CGI that collects the customer info,
> I get an error.
> Here is the code segment as I placed it into the CGI:
>     sysopen(FH, "/www/htdocs/freetitandslcom/CustomerID.dat",
> O_RDWR|O_CREAT)     or &safe_die("Can't open CustomerID.dat file");
>     flock(FH, 2)                               or &safe_die("can't flock
> CustomerID.dat: $!");
>     $newcustomerid = <FH> || 0;
>     seek(FH, 0, 0)                             or &safe_die("can't rewind
> CustomerID.dat: $!");
>     truncate(FH, 0)                            or &safe_die("can't truncate
> CustomerID.dat: $!");
>     (print FH $newcustomerid+1, "\n")                    or &safe_die("can't
> write CustomerID.dat: $!");
>     close FH                                   or &safe_die("can't close
> CustomerID.dat: $!");
> The error I get is:
>     can't truncate CustomerID.dat: Permission Denied
> As a shot in the dark, I tried setting the permission on the "CustomerID.dat"
> file to '766' but that had no effect.

make the owner of CustomerID.dat file to be the user under which your apache
processes run (nobody or www or whoever)
set the permissions on a file to 600


The posting address is: san-diego-pm-list at

List requests should be sent to: majordomo at

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at> .
This is the general rule for most mailing lists when you need
to contact a human.

More information about the San-Diego-pm mailing list