Thread Counter (4 answers)
Opened by Us0R at 2005-02-19 12:32

esskar
 2005-02-19 15:02
#4437 #4437
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
ja schreiben musst du es ja auch wieder :)

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use strict;
use IO::File;
use Fcntl qw(:flock);

sub counter {
  my $filename = shift;

  my $count = 0;
  { # Counter File lesen
     my $fd = IO::File->new();
     if($fd->open("< $filename")) { # oeffnen okay
        flock($fd, LOCK_EX); # sperren
        if(my $line = $fd->getline) { #Zeile lesen
           chomp $line; # Zeilenumbruch entfernen
           $count = $line; # count lesen
        }
     }
  }
# genau hier ist ein Fehler; ein lücke im system quasi
# schau dubu's tipp weiter unten für den fix!!!
  { # Counter File schreiben
      my $fd = IO::File->new();
      if($fd->open("> $filename")) { # oeffnen okay
         flock($fd, LOCK_EX); # sperren
         print $fd ++$count; # neuen count schreiben
      }
  }
  return ($count); # unn fertig
}

print counter('counter.txt');
\n\n

<!--EDIT|esskar|1108823830-->

View full thread Counter