in reply to can a script change itself?
This is presented mostly for interest, as I am from the school of thought that self-modification is bad. It can lead to some very hard to track down bugs.# Get current file name $filename = __FILE__; # get old UID stored after __END__ $uid = <DATA>; chomp($uid); print "This script has been run $uid times $/"; # increment UID $uid++; # open current running file open(THIS_IN, $filename) or die "Could not open file"; @code = <THIS_IN>; close (THIS_IN); # Store new UID in last line of this code $size = @code; @code[$size-1]=$uid; # open current running file open(THIS_OUT,"> $filename") or die "Could not output to file"; print THIS_OUT @code; close (THIS_OUT); __END__ 0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A script can change itself
by CountZero (Bishop) on Mar 24, 2005 at 20:26 UTC | |
by Anonymous Monk on Mar 24, 2005 at 21:39 UTC |