While browsing thru some notes from one of my old college notebooks, I came across this interesting contest that we had in one of my data structures classes. The idea is to create a program that will clone itself when it is ran, generating an exact copy of its source code as output that can be compiled and ran again, and then repeated using the second generation copy, and so on and so on... I thought it would be a great idea here, just to see how other people would solve it. See who could do it in the least amount of lines, the most amount of lines, the most obfuscated code. Anyway, here is my attempt to solve the puzzle.

#!/usr/bin/perl #Programmer : cerberus use strict; my @p = (); my $t = chr(9); my $spc = chr(32); my $s = chr(36) . "p"; my $LB = chr(91); my $RB = chr(93); my $eql = chr(61); my $tick = chr(39); my $sc = chr(59); my $i = 0; $p[0] = '#!/usr/bin/perl'; $p[1] = '#Programmer : cerberus'; $p[2] = 'use strict;'; $p[3] = 'my @p = ();'; $p[4] = 'my $t = chr(9);'; $p[5] = 'my $spc = chr(32);'; $p[6] = 'my $s = chr(36) . "p";'; $p[7] = 'my $LB = chr(91);'; $p[8] = 'my $RB = chr(93);'; $p[9] = 'my $eql = chr(61);'; $p[10] = 'my $tick = chr(39);'; $p[11] = 'my $sc = chr(59);'; $p[12] = 'my $i = 0;'; $p[13] = 'for($i=0; $i<13; $i++) { print "$p[$i]" . chr(10);}'; $p[14] = 'for($i=0; $i<17; $i++) { print "$t$s$LB$i$RB$spc$eql$spc +$tick" . $p[$i]. "$tick$sc" . chr(10); }'; $p[15] = 'for($i=13; $i<17; $i++){ print "$t$p[$i]" . chr(10);}'; $p[16] = 'exit 0;'; for($i=0; $i<13; $i++) { print "$p[$i]" . chr(10);} for($i=0; $i<17; $i++) { print "$t$s$LB$i$RB$spc$eql$spc$tick" . $ +p[$i]. "$tick$sc" . chr(10); } for($i=13; $i<17; $i++){ print "$t$p[$i]" . chr(10);} exit 0;

In reply to The 6th Day Challenge by cerberus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.