gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

A friend of mine posed an interesting challenge: write a program that would output its own content to the screen. Without much thought, I wrote him this solution:

exec 'cat',$0;

However, as you can imagine, this is not what he had in mind. According to him, this problem is possible without any system calls, and I quote, "you can use your loop constructs, your variable/constant declarations, and a basic print to stdout function. that's it". I was completely stomped. Can anyone even suggest a means of getting this done yet ablidging to these harsh rules?

P.S. My friend is doing this in Delphi. He posed this to me not as a personal challenge but a challenge to Perl.

Replies are listed 'Best First'.
Re: Program prints out it's own content
by blakem (Monsignor) on Feb 08, 2002 at 09:06 UTC
    Programs that display their own source are called quines. I know a few have been posted here on perlmonks (since at least one is mine ;-) a quick search on 'quine' turns up several.....

    -Blake

      Thank you for the reply. I had no idea such programs were called Quines. With that in mind, does my one-liner seen above count as one?
Re: Program prints out it's own content
by grinder (Bishop) on Feb 08, 2002 at 09:47 UTC
    There's a web page with quines in languages that I didn't even know existed. It hasn't been updated in a while, but it's still worth perusing. There are lots of examples in Perl, although none, it would seem, in Delphi. There's your chance to be famous.

    www.nyx.net/~gthompso/quine.htm

    Oh, and it's not it's, it's its, by the way :)

    --
    g r i n d e r
    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';
Re: Program prints out it's own content
by Zaxo (Archbishop) on Feb 08, 2002 at 09:12 UTC

    The idea represented by this problem is called "quining". Super Search for 'quine' will turn up some Perl examples. Googling "quine" will get you more code and theory.

    After Compline,
    Zaxo

Re: Program prints out it's own content
by dreadpiratepeter (Priest) on Feb 08, 2002 at 13:27 UTC
    As blake said, these programs are quines. The most amazing quine (actually a viral, self-replicating quine) is Damian Conway's SelfGOL program- designed to win every category of the obsfucated perl contest some years back. He occasionally gives lectures explaining the monstrosity.
    In addition to being a quine that can make other programs into quines, it's a scrolling marquee and a game of life.
    It's also a great example of the nooks and crannies of perl.
    Unfortunately, I don't know that he has released the program for general consumption.
    Sorry, I'm a little off-topic, but it really is an amazing program and the SelfGOL lecture was the highlight of the conference for me last year.

    -pete
    Entropy is not what is used to be.

      Damian's description of it is here.

      However, at the time of writing, yetanother.org appears to be down for some reason. Check out an alternate description from O'Reilly.

Re: Program prints out it's own content
by iakobski (Pilgrim) on Feb 08, 2002 at 09:17 UTC
    Also of interest to you would be Damian's Acme::Bleach module and other Acme:: modules. They don't exactly write out their own content, check them out for some hints (and a laugh!)

    -- iakobski

Re: Program prints out it's own content
by Rhose (Priest) on Feb 08, 2002 at 14:55 UTC
    Well, if you can use seek, here you go... *Smiles*

    seek(DATA,0,0); while(<DATA>) { print; } __DATA__
Re (tilly) 1: Program prints out it's own content
by tilly (Archbishop) on Feb 09, 2002 at 06:49 UTC
    Hand him a blank page and tell him that this is a Perl solution that lacks character. :-)

    The discussion at Quine Whine should give you pointers on writing a few more of your own. In particular the link to here should make you understand that any Turing complete language has quines, therefore this is not a test of Perl, but merely of whether you could figure out how to do it. (When you know the tricks, they really aren't that hard.)

      Hand him a blank page and tell him that this is a Perl solution that lacks character.

      I really like this phrase. Now, I only hope that in my last few months of school, I will get a chance to use it on one of my professors. :-)