quine: /kwi:n/ /n./ [from the name of the logician Willard van Orman Quine, via Douglas Hofstadter] A program that generates a copy of its own source text as its complete output. Devising the shortest possible quine in some given programming language is a common hackish amusement.
SelfGOL was Damian Conway's intended entry to the Obfuscated Perl Contest. It can print its own source code (self-replicate), rewrite other Perl programs so they can print their own source code (and still perform their original functions), detect un-rewritable Perl programs, play John Conway's Game of Life using its own source code or a specified file as a pattern for the board with a board of arbitrary size, or animate a rotating banner of an arbitrary short amount of text. SelfGOL's source is under 1000 bytes of standard Perl, does not import any modules, and doesn't use a single if, unless, while, until, for, foreach, goto, next, last, redo, map, or grep.
#!/usr/bin/perl -s
$;=$/;seek+DATA,undef$/,!$s;$_=<DATA>;$s&&print||$g&&do{$y=($x||=20)*($y||8);sub
i{sleep&f}sub'p{print$;x$=,join$;,$b=~/.{$x}/g,$;}sub'f{pop||1}sub
n{substr($b,&f%$y,3)=~tr,O,O,}sub'g{$m=substr($b,($f=&f)--,1);($w,$w,$m,O)
[n($f-$x)+n($x+$f)-(${m}eq+O)+n$f]||$w}$w="\40";$b=join'',@ARGV?<>:$_,$w
x$y;$b=~s).)$&=~/\w/?O:$w)gse;substr($b,$y)=q++;$g='$i=0;$i?$b:$c=$b;
substr+$c,$i,1,g$i;$g=~s?\d+?($&+1)%$y?e;$i-$y+1?eval$g:do{$b=$c;p;i
}';sub't{$d=$d-1?$d:$0;s;';\t#$d#;,$_}sub'e{eval$g;&e};e}||eval||die+No.$;
__DATA__
$d&&do{{$^W=$|;*_=sub{$=+s=#([A-z])(.*)#=#$+$1#=g}}
@s=(q[$_=sprintf+pop@s,@s],t$_=q[
$d&&do{{$^W=$|;*_=sub{$=+s=#([A-z])(.*)#=#$+$1#=g}}'
@s=(q[%s],q[%s])x2;%s;print"\n"x&_,$_;i;eval};
])x2;$_=sprintf+pop@s,@s;print"\n"x&_,$_;i;eval};$/=$y;$"=",";print
q<#!/usr/bin/perl -sw
!$s?do{>.($_=<>).q<}:do{@s=(q[printf+pop@s,@s],q[#!/usr/bin/perl -sw
!$s?do{>.(s$%$%%$g,y=[====y=]==||&d,$_).q<}:do{@s=(q[%s],q[%s])x2;%s}
])x2;printf+pop@s,@s}
>
Usage:
- selfgol -s
outputs its own source code.
- selfgol <perlprog.pl
makes perlprog.pl self-replicating (with a command line switch of -s), outputs new source to STDOUT.
- selfgol -g [-y=## -x=##] [<gamefile]
plays the Game of Life with its own source code or an arbitrary file, with optional height and width restrictions.
- selfgol -d[=bannertext]
displays a rotating banner with the provided text. If no text is provided, it uses the program name as the text.
SelfGOL was written to win all four categories in the Obfuscated Perl contest (3rd year, I believe, whose web pages are no longer available) and still comply with other restrictions of not using modules and being under 1000 bytes. Alas, the rules were drastically changed for the following year's contest, and so it was never entered.
Damian is touring with a presentation of how SelfGOL works, and because he is very generously not charging admission, I got to see the presentation last night at the Seattle Perl User's Group meeting. It was surprisingly instructional to see how some of the more obscure features of Perl work, and recognize why outdated features existed in the first place. (The $; special variable just shouldn't be, both for what it does and what it looks like. But that's how things worked in Perl 4, and still work in Perl 5.)
Academic exercises in recursion and self-reference are always fun. If you don't think the animated banner or the Game Of Life have anything to do with quines or self-reference, you're not looking closely enough. Not that I recommend looking closely, though you're welcome to try.