By now, we have all seen self modifying code. So, I won't bore you with something simple as that. Instead, I will present you with self correcting code. The program below does not compile. However, if you save the code into a file, (make sure you do not put a trailing newline in the file), and execute the file, it will print the famous message. When the process is finished, you are left with code that compiles - and executing the file again will not print anything.

#!/opt/perl/bin/perl -- # Remove trailing newline! BEGIN{$SIG{__WARN__}=sub{$_=pop;y-_- -;print/".*(.)"/; truncate$0,-1+-s$0;exec$0;}}//rekcaH_lreP_rehtona_tsuJ

-- Abigail

Replies are listed 'Best First'.
(tye)Re: Self correcting code
by tye (Sage) on Jun 30, 2001 at 19:53 UTC

    Replacing "exec$0" with "exec$^X,$0" makes this much more portable.

            - tye (but my friends call me "Tye")
Re: Self correcting code
by petral (Curate) on Jun 30, 2001 at 01:10 UTC
    needed:  BEGIN{$|++;$SIG...  to work for me.

      p

Re: Self correcting code
by mikeB (Friar) on Jun 30, 2001 at 00:25 UTC
    Almost like watching ticker tape on my little computer! Can't say I've ever seen this interpretation of 'recursive program' before :)