Counter with an NT twist 
Author Message
 Counter with an NT twist

I've had this script working in Unix and Win 98 but am now hosting on an NT
server and it don't work no more!  Can anyone help as to what I need to
change?

 I've got this script as part of a larger script

 $logwwwcounter = "counter.log";

 open(DATA,"$logwwwcounter");
    $www_counter = <DATA>;
 close(DATA);

 $www_counter_new = $www_counter;
 $www_counter_new++;
 open(DATA,">$logwwwcounter");
 print DATA "$www_counter_new";
 close(DATA);

 however it is not working on the NT Server hosting
 but runs fine on my win 98 machine -- WHY???

Tks in advance,
Andrew



Wed, 18 Jun 1902 08:00:00 GMT  
 Counter with an NT twist

Quote:
> I've had this script working in Unix and Win 98 but am now hosting on an
NT
> server and it don't work no more!  Can anyone help as to what I need to
> change?

ALWAYS, I mean ALWAYS check the success of your opens!

Quote:
>  I've got this script as part of a larger script

>  $logwwwcounter = "counter.log";

>  open(DATA,"$logwwwcounter");

You need to check the success or otherwise of this open.
open(DATA,"$logwwwcounter") or die "unable to read file $!\n";

By including $! in the die message, you will see why it failed to open (if
it did - permission denied, file not found etc)

Quote:
>     $www_counter = <DATA>;
>  close(DATA);

>  $www_counter_new = $www_counter;
>  $www_counter_new++;
>  open(DATA,">$logwwwcounter");

You need to check the success or otherwise of this open.
open(DATA,">$logwwwcounter") or die "unable to create file $!\n";

same here.

Quote:
>  print DATA "$www_counter_new";
>  close(DATA);

You may find that you are not working where you think you're are working,
and need to specify a full path to the file ie
d:/inetpub/wwwroot/client/cgi-bin or something similar.  See your hosting
provider for the actual path.

Wyzelli



Wed, 18 Jun 1902 08:00:00 GMT  
 Counter with an NT twist

Quote:

>  I've got this script as part of a larger script

Others have pointed out that you're not checking whether your attempts to
open files are successful, and that you're making (probably incorrect)
assumptions about what your working directory is going to be when your
script runs.  But there's still another problem: your counter will
accidentally and seemingly randomly reset itself if two hits occur in the
"right"  sequence.

Assume we're dealing with the first hit:

Quote:
>  $logwwwcounter = "counter.log";
>  open(DATA,"$logwwwcounter");
>     $www_counter = <DATA>;
>  close(DATA);
>  $www_counter_new = $www_counter;
>  $www_counter_new++;
>  open(DATA,">$logwwwcounter");

Now let's suppose that right after this statement, the server gets another
hit, suspends execution of the current instance of the script and starts
another instance.  When the new instance goes to read the count, it's
going to be reading from an empty file, which will cause its copy of
$www_counter to be set to undef, which will be converted to zero when you
try to increment it.

What happens then will depend on whether the original instance regains
control before the new instance finishes.  If it doesn't, then the new
instance will write out a "1" and then the old instance will overwrite it,
meaning that one hit will have been skipped.  If it does, the new instance
will write out a "1" after the old instance writes out its count, and
you'll get an undesired reset.

Quote:
>  print DATA "$www_counter_new";
>  close(DATA);

In order to solve this problem, you need to read the entries about
"locking" in perlfaq5 and then implement the ideas discussed there.


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Because twisted minds think twisted things...

2. counter for NT

3. Accessing Win NT performance counters

4. Matt Wright's Counter on Windows NT

5. How about a little twist on the shredding?

6. char to number conversion w/ a twist

7. search with a twist

8. s2p twisted

9. Sourcing with a twist of Parsing ?

10. Random Numbers with a Twist

11. A problematic twist with =~ and regex

 

 
Powered by phpBB® Forum Software