in reply to Self-Modifying-Perl-Script
abatek~#!/usr/bin/perl # lives tracks the number of times the program has run. # the line number is stored for later use in an array # called @program, so if consensual hackers decide to # rearrange, the program can still find the variable. $lives = 0; $line{lives} = (__LINE__ - 1); # The source code of the file is read into @program open( THIS, "$0" ); @program = <THIS>; close( THIS ); # The line number from earlier points at the line of code # storing the variable '$lives'. # Render the line and store it back into @program over the # old line of code. $program[ $line{lives} ] = '$lives = ' . ($lives+1) . '; $line{lives} = (__LINE__ - 1);' . "\n"; # Output the program's slightly modified source # code over the old file. open( THIS, ">$0" ); print THIS @program; close( THIS ); # Clear the screen buffer and 'exec' the modified source. system( 'clear' ); exec( $0 ); # exec() does not return to this program. print "This does not get displayed.";
|
|---|