Newbie Question

Ken Loomis kloomis at bigplanet.com
Sun Aug 13 14:26:18 CDT 2000


Thanks for all the help and suggestions. I read thru the recommended man pages and
found a slightly different code segment in perlopentut.

When I tried that and still had problems, I realized I had left off the 'use' line
in the customer's program.

It still took some slight mods to make it work for my application, , so, FYI, for
anyone interested, here is the code segment that finally worked:

# # Set the new customer ID
sysopen(DATAF, "/www/htdocs/freetitandslcom/CustomerID.dat", O_RDWR | O_CREAT)
        or die "can't open CustomerID.dat: $!";

# autoflush DATAF
$ofh = select(DATAF); $| = 1; select ($ofh);

flock(DATAF, LOCK_EX) or die "can't write-lock CustomerID.dat: $!";

chomp($newcustomerid = <DATAF> || 0);

$newcustomerid = $newcustomerid +1;

seek(DATAF, 0, 0) or die "can't rewind CustomerID.dat : $!";

print DATAF "$newcustomerid\n" or die "can't write CustomerID.dat: $!";

truncate(DATAF, tell(DATAF)) or die "can't truncate CustomerID.dat: $!";

close(DATAF) or die "can't close CustomerID.dat: $!";

###### end of Set the new customer ID ######



Eugene Tsyrklevich wrote:

> ~sdpm~
> 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:
> > $!";
> >     # DO NOT UNLOCK THIS UNTIL YOU CLOSE
> >     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: $!");
> >     # DO NOT UNLOCK THIS UNTIL YOU CLOSE
> >     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
>
> cheers
> ~sdpm~
>
> The posting address is: san-diego-pm-list at hfb.pm.org
>
> List requests should be sent to: majordomo at hfb.pm.org
>
> If you ever want to remove yourself from this mailing list,
> you can send mail to <majordomo at happyfunball.pm.org> 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 happyfunball.pm.org> .
> This is the general rule for most mailing lists when you need
> to contact a human.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kloomis.vcf
Type: text/x-vcard
Size: 333 bytes
Desc: Card for Ken Loomis
Url : http://mail.pm.org/archives/san-diego-pm/attachments/20000813/04b59b8b/kloomis.vcf


More information about the San-Diego-pm mailing list