I found this sweet perl 'quine' on a page referencing Douglas Hofstadter:

sub f{($f)=@_;print"$f(q{$f});";}f(q{sub f{($f)=@_;print"$f(q{$f});";}f});

Anyone up for a 'quine' contest?? It'll be in the spirit of "Godel, Escher, Bach"....Perl style!

Replies are listed 'Best First'.
Re: Self Referential Code
by BrowserUk (Patriarch) on Sep 08, 2002 at 01:11 UTC
      Wow! You just made my week with that code! I should have done a search on quines.

        I wish I could claim the credit for it, but that goes squarely to len.

        It was just one of the first things I saw when I came here, and so sticks in my mind. Damdest thing I ever saw in code.


        Well It's better than the Abottoire, but Yorkshire!
This quine won't Deparse -- bug?
by laughingboy (Monk) on Sep 11, 2002 at 17:46 UTC
    Hmmm. I wanted to Deparse this beastie to study it more carefully. Deparse goes BOOM! as shown below:
    c:/perl/misc $ perl -MO=Deparse quine.pl Can't call method "PADLIST" on an undefined value at c:/Perl/lib/B/Dep +arse.pm line 1039. CHECK failed--call queue aborted.
    I'm running the latest ActivePerl distribution for Win32.
    c:/perl/misc $ perl -v This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2001, Larry Wall Binary build 633 provided by ActiveState Corp. http://www.ActiveState. +com Built 21:33:05 Jun 17 2002
    Anyone else have better luck with this?

    laughingboy

Re: Self Referential Code
by blokhead (Monsignor) on Sep 11, 2002 at 06:09 UTC
    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