Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > i have a question

Reply
Thread Tools

i have a question

 
 
pereges
Guest
Posts: n/a
 
      03-22-2009
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) ?
 
Reply With Quote
 
 
 
 
pereges
Guest
Posts: n/a
 
      03-22-2009
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.
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      03-23-2009
pereges <(E-Mail Removed)> wrote:


> 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.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      03-23-2009
pereges <(E-Mail Removed)> wrote:


> 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.)


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-23-2009
pereges <(E-Mail Removed)> wrote:

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
 
Reply With Quote
 
Krishna Chaitanya
Guest
Posts: n/a
 
      03-23-2009
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.
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      03-24-2009
On 2009-03-23, Ben Morrow <(E-Mail Removed)> wrote:
>
> Quoth Tad J McClellan <(E-Mail Removed)>:
>>
>> You should use single quotes unless you need one of the two extra
>> things that double quotes gives you.

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


Makes a strong argument!

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-24-2009
On Mon, 23 Mar 2009 16:06:57 +0000 Ben Morrow <(E-Mail Removed)> wrote:

BM> Quoth Tad J McClellan <(E-Mail Removed)>:
>>
>> You should use single quotes unless you need one of the two extra
>> things that double quotes gives you.


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there someone have written some programs about RSA-1024?I have some question. dolphin C++ 3 03-09-2007 02:47 PM
have you got any of these i can have spike240 Case Modding 4 09-14-2005 03:48 AM
a question: Do I have to have some work experience before taking the exam jessi MCAD 3 04-04-2005 10:04 AM
do i have to have.... =?Utf-8?B?amFrZQ==?= Wireless Networking 1 03-11-2005 06:05 PM
Warehouse Dell dimension 3000 advert. This is a question so TNO doesn't have to download this message and read it to waste his time reading messages that I could have possibly got the answer .. (damn, run out of space in the subject line!) cowboyz NZ Computing 68 12-21-2004 02:19 PM



Advertisments