The script I'm working with is supposed to open a data file (ads.counter - which currently reads "0" and that's it), and append it with a new number when a post is made. Well, this is not working and below is the script. It is also supposed to generate a password number by using a multiplier. The multiplier in this case is the # 24. But each time you look at the post, the nuber is always 1 and the password is always 24, not matter how many posts you make. Anybody have any idea what could be the problem?
Thanks again!
...& thanks jatar, no need for chopping here! ;-)
Ramsey
------------------------
# Open the data file to read the number of the last post
open (ADFILE, "$classdir/ads.counter");
$old_counter = <ADFILE>;
# chop $old_counter;
# Update the Ad Number by one
$old_counter++;
$new_counter = $old_counter;
# Rewrite the data file
open (ADFILE, ">$classdir/ads.counter");
print ADFILE "$new_counter";
close(ADFILE);
$password = $new_counter * $password_multiplier;
Also, while this is probably not your problem yet, this script doesn't appear able to handle the situation where more than one instance is running at a time. You ought to lock your couter file or something.
Could you tell me a little about warnings and strict? (Totally new to perl coding.) Also, there is a part of the script that locks the files for such a problem.
---
# Lock the file for security so the file is not damaged if more than one user tries to access it
at the same time.
$quit = 0;
while ($quit!= 1) {
if (-e "$classdir/$short_category_name.lock") {
# The file exists, time to take a nap
sleep(1);
}
else {
# Create the lock file, thereby locking the Classified Ad file
open (LOCK,">$classdir/$short_category_name.lock");
close LOCK;
}
---
#!/usr/bin/perl -w
use strict;
The important parts are the "-w" after your "shebang" line which turns on warnings, then the "use strict;" line which tells it to give warnings under more strict guidelines. This will cause it to warn you about all kinds of things, like not using "my" to declare your variables (perhaps at the same time that you use them for the first time). The second paragraph in the style guide addresses this a bit:
[perldoc.com...]
Also, your file locking method is not appropriate. I'd advise you to take a look at the perl FAQs about this. Specifically, try:
[perldoc.com...]
[perldoc.com...]
Good luck!