i have a question

Discussion in 'Perl Misc' started by pereges, Mar 22, 2009.

  1. pereges

    pereges Guest

    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) ?
    pereges, Mar 22, 2009
    #1
    1. Advertising

  2. pereges

    pereges Guest

    Re: i have a question

    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.
    pereges, Mar 22, 2009
    #2
    1. Advertising

  3. pereges <> 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/"
    Tad J McClellan, Mar 23, 2009
    #3
  4. Re: i have a question

    pereges <> 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/"
    Tad J McClellan, Mar 23, 2009
    #4
  5. Re: i have a question

    pereges <> 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
    Jürgen Exner, Mar 23, 2009
    #5
  6. Re: i have a question

    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.
    Krishna Chaitanya, Mar 23, 2009
    #6
  7. pereges

    Justin C Guest

    Re: i have a question

    On 2009-03-23, Ben Morrow <> wrote:
    >
    > Quoth Tad J McClellan <>:
    >>
    >> 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.
    Justin C, Mar 24, 2009
    #7
  8. pereges

    Ted Zlatanov Guest

    Re: i have a question

    On Mon, 23 Mar 2009 16:06:57 +0000 Ben Morrow <> wrote:

    BM> Quoth Tad J McClellan <>:
    >>
    >> 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
    Ted Zlatanov, Mar 24, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cmdyYW5kaWRpZXI=?=

    Can I have base class derived from System.Web.UI.Page have design

    =?Utf-8?B?cmdyYW5kaWRpZXI=?=, Jul 22, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    475
    Patrice
    Jul 22, 2005
  2. okaminer
    Replies:
    3
    Views:
    304
    okaminer
    Aug 1, 2005
  3. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    6
    Views:
    5,491
    Kevin Spencer
    Aug 18, 2005
  4. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Does a resource have to have all phrases in it?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Jan 30, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    374
    Neil Ni
    Feb 3, 2006
  5. dolphin
    Replies:
    3
    Views:
    363
    rossum
    Mar 9, 2007
Loading...

Share This Page