CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
This count does not work.

 



crashinto
Novice

Sep 30, 2001, 9:20 AM

Post #1 of 13 (1817 views)
This count does not work. Can't Post

I've got another one of my questions, this time I can't get this count to work, it works from 1-9 but when I click refresh and when it should just say 10, it says 10 then 1 and then next refresh and restarts at 2 and does the same thing when I get back to 10.

Here is the code I use:


Code
sub completeorder_html { 


open(COUNT, "$datapath/orders/completed/count.txt");
while(<COUNT>) {
my ($num) = split /\n/;
$newnum = $num + 1;

open(ID, "$datapath/orders/$FORM{'id'}.txt");
while(<ID>) {
($item, $quant, $price, $num, $total) = split /\|/;

open(CART, ">$datapath/orders/completed/$newnum.txt");
print CART "$item|$quant|$price|$num|$total|\n";
close(CART);

}
close(ID);
print qq|This order has been given the number $newnum for further referencing.|;

open(COUNTS, ">$datapath/orders/completed/count.txt");
print COUNTS "$newnum";
close(COUNTS);



}
close(COUNT);





}



crashinto
Novice

Oct 2, 2001, 2:19 PM

Post #2 of 13 (1801 views)
Re: This count does not work. [In reply to] Can't Post

No one knows?



yapp
User

Oct 3, 2001, 2:11 AM

Post #3 of 13 (1797 views)
Re: This count does not work. [In reply to] Can't Post

Well, I'm just guessing...

a. You don't test whether the file is being opened. You just assume it.
Please use open(FILE, 'location') or die "Can't open: $!";
b. Please use some indent (or the code markup code of the forum). It makes it more readale.

Hope it helps.



crashinto
Novice

Oct 3, 2001, 2:27 AM

Post #4 of 13 (1795 views)
Re: This count does not work. [In reply to] Can't Post

The die ... code never works on my site... I just tried it anyway and gave me a 500 error. I have never figured out why that gives me a 500 error for. But anyway the file is their that I am trying to open.



yapp
User

Oct 3, 2001, 8:06 AM

Post #5 of 13 (1794 views)
Re: This count does not work. [In reply to] Can't Post

That has got something to do with running CGI's at a webserver. The die code works, but the result is written in the server-log (STDERR). Sorry for that, I forget. IIS doesn't do that so I didn't realize it before.

Either you should check the server log (ask your host for that), or:
add this line at top of the script:


Code
use CGI::Carp qw(fatalsToBrowser);

This way is very simple and redirects all error messages to your browser. You can also program such error trap dialog yourself, but that's off topic now.



vallely
Novice

Oct 3, 2001, 1:21 PM

Post #6 of 13 (1789 views)
Re: This count does not work. [In reply to] Can't Post

I'm just a beginner but in all the count scripts Ive seen the count.txt is setup so the file contains only one line that contains the number and no "\n".

If the file is setup in this manner you can get the number as a scalar.

like this:
open (COUNT, "count.txt");
$out = <COUNT>;
close COUNT;

You use a while loop on count.txt.
Inside the loop you open ID, close ID, open CART, Close CART, open COUNTS, print to COUNTS, close the loop, close COUNT.

Something like this is what Ive seen in other scripts.
----------------------------------------------------
open (COUNT, "count.txt");
$out = <COUNT>;
close COUNT;

$out++;

open (COUNT, ">count.txt");
print COUNT $out;
close COUNT;

$newnumber = $out;

#now do something with $newnumber
-------------------------------------------------------------
You can also skip $out and use this

open (COUNT, "count.txt");
$newnumber = <COUNT>;
close COUNT;

$newnumber++;

open (COUNT, ">count.txt");
print COUNT $newnumber;
close COUNT;

#do something with $newnumber




crashinto
Novice

Oct 3, 2001, 2:45 PM

Post #7 of 13 (1787 views)
Re: This count does not work. [In reply to] Can't Post

That still didn't work. It's still counting to 10 then restarting.



yapp
User

Oct 4, 2001, 1:43 AM

Post #8 of 13 (1782 views)
Re: This count does not work. [In reply to] Can't Post

That the line ends with \n doesn't matter. It's just the return code. (useful with more records in the same file).

After you read in one line, do

Code
chomp $line;

so the last =\n falls off.



yapp
User

Oct 4, 2001, 2:16 AM

Post #9 of 13 (1782 views)
Re: This count does not work. [In reply to] Can't Post

Here is my guess.
to be onest, your code had some strange lines in it.
You can't write to a file when you've just opened it. That overwrites the data. Also very strange is your "file copying" procedure. (if that's wat you wanted to do). Please open a file once, and keep it open. Opening a file for every write and then closing it doesn't do you any good. Other scripts get the opportunity to open the file. Both read/writes to the same file will result in some strange file (mixed ascii's, or corruped files). It even get's worse when the file isn't locked.


Code
# -w is very remommented at the #! line 

use strict; # Explicit declarations
use CGI qw(:cgi);
use CGI::Carp qw(fatalsToBrowser);
use Fcntl qw(:DEFAULT :flock);

sub completeorder_html
{
# Get parameters using CGI module
my $ID = param('id');

# Save an array with new data.
# you can't write to a file when you still need
# to read from it.
my @NewCounts;


# Open and lock (LOCK_SH for read, LOCK_EX for write mostly)
# The O_RDWR makes the file readable and writable at the same time.
# P.S. you can't write until you're done reading
# That was one bug in your code.
sysopen(COUNT, "$datapath/orders/completed/count.txt", O_RDWR) or die "Can't open count.txt: $!";
flock(COUNT, LOCK_SH);

while(my $num = <COUNT>)
{
chomp $num; # remove \n
my $newnum = ($num + 1);

# Open the new id
open(ID, "$datapath/orders/$ID.txt") or die "Can't open data file for $ID: $!";
flock(ID, LOCK_SH);

# Just open the file once. We keep in in use until we're done.
# Combined with the flock, nobody can edit the file while this script
# is running. Otherwise someother user (running the script at the same time)
# could corrupt the entire file while we were busy with it at the same time.
open(CART, ">$datapath/orders/completed/$newnum.txt") or die "Can't save $newnum: $!";
flock(CART, LOCK_EX);

#### The following code it don't get: there are better ways of copying a file.
#### Besides, you were opening the file for each print,
#### and truncating it every time you opened it. (because of the >, not >> )
#
# while(my $line = <ID>)
# {
# chomp $line;
# my ($item, $quant, $price, $num, $total) = split(/\|/, $line, 5);
# print CART "$item|$quant|$price|$num|$total|\n";
# }
#
# It can be done with this line...
pring CART <ID>; # <ID> returns an array because print can handle it.
# and everything is being printed in the the new file

# Close and unlock the files
flock(CART, LOCK_UN);
close(CART);
flock(ID, LOCK_UN);
close(ID);

print qq|This order has been given the number $newnum for further referencing.|;
push @NewCounts, "$newnum";
}

seek(COUNT, 0, 0) or die "Can't rewind file: $!"; # go to the first line
truncate(COUNT, 0) or die "Can't truncate file: $!"; # erase everything from the first character.

# Write the new data into the file,
# we're done reading from it now.
print COUNT, @NewCounts;

# Close the count file
flock(COUNT, LOCK_UN);
close(COUNT);
}

I hope my code can help you, since I don't know the exact context of the code in your program I can't tell you that this works literally as it has been typed.



crashinto
Novice

Oct 4, 2001, 3:46 AM

Post #10 of 13 (1780 views)
Re: This count does not work. [In reply to] Can't Post

It's saying no comma allowed after filehandle at line 539.. which is around


print qq|This order has been given the number $newnum for further referencing.|;


push @NewCounts, "$newnum"; }


seek(COUNT, 0, 0) or die "Can't rewind file: $!"; # go to the first line
truncate(COUNT, 0) or die "Can't truncate file: $!";


# erase everything from the first character.
# Write the new data into the file,
# we're done reading from it now.

print COUNT, @NewCounts;



yapp
User

Oct 5, 2001, 2:04 AM

Post #11 of 13 (1772 views)
Re: This count does not work. [In reply to] Can't Post

Sorry, I just typed this in, guessing it was OK. This is a error I get ofset when programming in perl (I'm used to write it different).
You just need to remove the comma before the filehandle


Code
print COUNT @NewCounts;

I guess you've found that out yourself aswell.

P.S. If you don't get the Perl error messages, type

Code
use diagnostics

somewhere at the beginning of your script



crashinto
Novice

Oct 6, 2001, 4:36 AM

Post #12 of 13 (1765 views)
Re: This count does not work. [In reply to] Can't Post

The script runs now after I fixed the errors but now it only works if their is 0 completed orders in their. Once I have 1 order it does'nt work anymore, just comes up with my header and footer and nothing is printed.

(This post was edited by crashinto on Oct 6, 2001, 3:41 AM)


crashinto
Novice

Oct 7, 2001, 1:13 PM

Post #13 of 13 (1756 views)
Re: This count does not work. [In reply to] Can't Post

Just to tell you all, I got it working but I had to redo what i wanted to do, I made the db to print
1|
2|
3|
...

and now it will count properly.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives