#!perl -w # self-contained counter script increments and prints value in DATA # set permissions to allow script to read and write itself - epoptai use strict; my$e = 0; my$d; while(){$d .= $_} # read data $d++; # increment open (ME,"+>> $0") or die "$!"; # open self unless($^O=~/mswin/i){ flock(ME,2) or die "$!"} # lock unless win32 my@me = ; # read self into array for(@me){ if($_=~m|_\_DATA_\_|){ $e = 1; next } # find last line if($e == 1){ $_ =~ s/\d+/$d/o } # and replace with incremented value } seek ME, 0, 0; # goto top of self truncate ME, 0; # and clear it print ME @me; # write modified contents close ME or die "$!"; print $d; # print incremented value __DATA__ 0