When I read that book, I saw the C snippet of code which printed itself out using a self-referential printf(). I immediately wrote a quick Perl version, and then tweaked and got a fun little self-ref, a little more Perl-ish in flavor.
#!/usr/bin/perl
$q=chr(39);$n=chr(10);$t=chr(96);$a='#!/usr/bin/perl%s$q=chr(39);$n=ch
+r(10);$t=chr(96);$a=%s%s%s;$s-sprintf($a,$n,$q,$a,$q,$n);eval$s;%s';$
+s=sprintf($a,$n,$q,$a,$q,$n);eval$s;
This code uses sprintf to generate a string which is the same as its own code. It then evals that string. It's a roundabout method of saying something along the lines of 1 while fork;.
I find it a little more exciting than the alternative and more obvious solution:
#!/usr/bin/perl
open(F,"<$0");
undef $/;
eval <F>;
Although I find this one easier to maintain ;)
I'd like to explore this area a little more, and have some Perl that mutates itself before the eval. And find some way to not run out of pids. Update: oops, eval != fork so it won't use up pids, but hopefully you get the point. After a few levels deep in the recursion, the program gets painfully slow. Maybe mutate it to the point where it doesn't eval itself anymore after so many levels.
blokhead |