(note: thanks to the magic of soft links, I've learned/remembered (I don't remember if I knew it before) that self-replicating programs such as I'm talking about are called quines)

Writing programs that print out their own source code is/was a popular diversion. This is a (rather poorly done) example that I wrote in Perl a year or so ago, after reading the paper Reflections on Trusting Trust:

#!/usr/bin/perl
$i='';
$i = $i . 'p';
$i = $i . 'r';
$i = $i . 'i';
$i = $i . 'n';
$i = $i . 't';
$i = $i . ' ';
$i = $i . '"';
$i = $i . '#';
$i = $i . '!';
$i = $i . '/';
$i = $i . 'u';
$i = $i . 's';
$i = $i . 'r';
$i = $i . '/';
$i = $i . 'b';
$i = $i . 'i';
$i = $i . 'n';
$i = $i . '/';
$i = $i . 'p';
$i = $i . 'e';
$i = $i . 'r';
$i = $i . 'l';
$i = $i . '\\';
$i = $i . 'n';
$i = $i . '\\';
$i = $i . '$';
$i = $i . 'i';
$i = $i . '=';
$i = $i . '\'';
$i = $i . '\'';
$i = $i . ';';
$i = $i . '\\';
$i = $i . 'n';
$i = $i . '"';
$i = $i . ';';
$i = $i . ' ';
$i = $i . '$';
$i = $i . 'a';
$i = $i . ' ';
$i = $i . '=';
$i = $i . ' ';
$i = $i . '$';
$i = $i . 'i';
$i = $i . ';';
$i = $i . ' ';
$i = $i . '$';
$i = $i . 'a';
$i = $i . ' ';
$i = $i . '=';
$i = $i . ' ';
$i = $i . 'r';
$i = $i . 'e';
$i = $i . 'v';
$i = $i . 'e';
$i = $i . 'r';
$i = $i . 's';
$i = $i . 'e';
$i = $i . ' ';
$i = $i . '$';
$i = $i . 'a';
$i = $i . ';';
$i = $i . ' ';
$i = $i . 'w';
$i = $i . 'h';
$i = $i . 'i';
$i = $i . 'l';
$i = $i . 'e';
$i = $i . ' ';
$i = $i . '(';
$i = $i . '$';
$i = $i . '_';
$i = $i . ' ';
$i = $i . '=';
$i = $i . ' ';
$i = $i . 'c';
$i = $i . 'h';
$i = $i . 'o';
$i = $i . 'p';
$i = $i . '(';
$i = $i . '$';
$i = $i . 'a';
$i = $i . ')';
$i = $i . ')';
$i = $i . ' ';
$i = $i . '{';
$i = $i . ' ';
$i = $i . 's';
$i = $i . '/';
$i = $i . '\\';
$i = $i . '\\';
$i = $i . '/';
$i = $i . '\\';
$i = $i . '\\';
$i = $i . '\\';
$i = $i . '\\';
$i = $i . '/';
$i = $i . 'g';
$i = $i . ';';
$i = $i . ' ';
$i = $i . 's';
$i = $i . '/';
$i = $i . '\\';
$i = $i . '\'';
$i = $i . '/';
$i = $i . '\\';
$i = $i . '\\';
$i = $i . '\\';
$i = $i . '\'';
$i = $i . '/';
$i = $i . 'g';
$i = $i . ';';
$i = $i . ' ';
$i = $i . 'p';
$i = $i . 'r';
$i = $i . 'i';
$i = $i . 'n';
$i = $i . 't';
$i = $i . ' ';
$i = $i . '"';
$i = $i . '\\';
$i = $i . '$';
$i = $i . 'i';
$i = $i . ' ';
$i = $i . '=';
$i = $i . ' ';
$i = $i . '\\';
$i = $i . '$';
$i = $i . 'i';
$i = $i . '.';
$i = $i . ' ';
$i = $i . '\'';
$i = $i . '"';
$i = $i . ',';
$i = $i . ' ';
$i = $i . '$';
$i = $i . '_';
$i = $i . ',';
$i = $i . ' ';
$i = $i . '"';
$i = $i . '\'';
$i = $i . ';';
$i = $i . '\\';
$i = $i . 'n';
$i = $i . '"';
$i = $i . ';';
$i = $i . ' ';
$i = $i . '}';
$i = $i . ';';
$i = $i . ' ';
$i = $i . 'p';
$i = $i . 'r';
$i = $i . 'i';
$i = $i . 'n';
$i = $i . 't';
$i = $i . ' ';
$i = $i . '"';
$i = $i . '$';
$i = $i . 'i';
$i = $i . '\\';
$i = $i . 'n';
$i = $i . '"';
$i = $i . ';';
print "#!/usr/bin/perl\n\$i='';\n"; $a = $i; $a = reverse
$a; while ($_ = chop($a)) { s/\\/\\\\/g; s/\'/\\\'/g;
print "\$i = \$i. '", $_, "';\n"; }; print "$i\n";
(line breaks inserted for readability)

This example is actually the output of a non-identical program, but the example, when run, will output itself.

The trick is to create a variable containing most of the program's source ($i in the example), then print out the variable's contents twice - once as a statement or set of statements that create the variable, and once to create the body of the program. Any bits and pieces not in can be written out in the body.

It's best to write the program sans the aforementioned variable, then copy the program's text into the variable. The first generation dosen't have to look like the second generation, but successive generations should look like the second.

A self-replicator can be used when writing a cross-platform worm, because, given the presence of a compiler, it gives the worm the ability to recomiple itself for new platforms.

As an experiment, I'm going to present the challenge of writing a self-replicator on the chat mailing list of the CP SC 2010 class I'm taking. Results will be posted here. I think they'll be learning about arrays on October 4, 2000, so they should be ready soon.

Ok, so they haven't learned about arrays, but I'm going to spring it on 'em anyway.