i have a question

P

pereges

I have written the code for an order form in html and there is an
order number associated with an order. I have written some code in
perl for form handling and what this basically does is display a table
with items purchased, print the total cost and the order number
associated with the order.

This order number is initialized to some arbitrary number and must be
incremented ever time a new order is made. How can I do this ? Should
I store the order number in a file or something (retrieve the last
stored order number and increment by 1) ?
 
P

pereges

Btw this is how I tried to do it but its not working:

....

open (FPTR,"data.txt");
$order_num = <FPTR>;
$new_order_num = $order_num + 1;
close (FPTR);

my $datafile = '/home2/s09/webber36/apache2/cgi-bin/data.txt';

open (FPTR, ">$datafile");
print MYFILE $new_order_num ;
close (MYFILE);

print $order_num;
print "<br />";
print $new_order_num;

.....

The file data.txt contains the most recent order number, we have
initialize it with some random value.
 
T

Tad J McClellan

Subject: i have a question


Please put the subject of your article in the Subject of your article.

I have written the code for an order form in html and there is an
order number associated with an order. I have written some code in
perl

s/perl/Perl/;


for form handling and what this basically does is display a table
with items purchased, print the total cost and the order number
associated with the order.

This order number is initialized to some arbitrary number and must be
incremented ever time a new order is made. How can I do this ?


Using an RDBMS of some sort would be the most expedient way of
dealing with the locking problem associated with a multitasking
environment such as the CGI.
 
T

Tad J McClellan

open (FPTR,"data.txt");


You should always, yes *always*, check the return value from open().

You should use the 3-argument form of open().

You should use a lexical filehandle.

You should use single quotes unless you need one of the two extra
things that double quotes gives you.

open my $FPTR, '<', 'data.txt' or die "could not open 'data.txt' $!";


I am guessing that there is some money involved here somewhere?

If so, you really should hire someone who knows what they're doing
to handle this for you.

Do you know what file locking is?

Do you know why you need file locking for this?

Do you know what can happen if you proceed without proper locking?


(Those are all rhetorical questions, as the answers are obvious.)
 
J

Jürgen Exner

Please put the subject of your article into the Subject of your article.
"I have a question" is about as useless as it gets.
open (FPTR,"data.txt");

Most people would suggest to use the 3-argument form of open() instead
of the 2-argument form.
Most people would suggest to use lexical file handles.
You should always check the success of any open() statement.
$order_num = <FPTR>;

Most people would strongly suggest to
use strict;
use warnings;
which would require you to declare $order_num
$new_order_num = $order_num + 1;

Same for $new_order_num.
The assignment works only by chance, because Perl automatically uses the
leading numerical portion of a string when a string is used in numerical
context. It is far better to remove the trailing newline explicitely via
a chomp().
close (FPTR);

my $datafile = '/home2/s09/webber36/apache2/cgi-bin/data.txt';

open (FPTR, ">$datafile");

Again:
- three argument form
- use lexical file handle
- provide error checking and handling
print MYFILE $new_order_num ;

You never declared or opened MYFILE

You do realize that your code when used in a CGI-application contains a
race condition, do you?

jue
 
K

Krishna Chaitanya

The answer to problems posed by file locking and concurrency control
is a database (don't kill yourself trying to reinvent the wheel). For
your problem, a sequence object in the DB sounds good. I've done this
many times and your need sounds simple, so yeah a sequence should do.

One more piece of free advice. If you're doing this for a live web-
shop kinda thing, please pay attention to encryption, floating-point
arithmetic, etc.
 
T

Ted Zlatanov

BM> I used to think that, until http://markmail.org/message/e4i3ngej24an7uch
BM> convinced me otherwise. The section on double-quoting starts about a
BM> third of the way down (search for 'politically-correct Bowdlerization').

I firmly believe that whichever quotes you use, you'll find out later it
was the wrong kind because the code, requirements, or data has changed.
It's "Murphy's" Law, I guess.

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,213
Messages
2,571,108
Members
47,700
Latest member
Naveed baloch

Latest Threads

Top