Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Short quines

by ambrus (Abbot)
on Jan 11, 2008 at 17:45 UTC ( #661934=obfuscated: print w/replies, xml ) Need Help??

This node contains a couple of short non-empty non-cheating perl quines. I've had most of this collection for ages, but have apparently never posted them on perlmonks.

More precisely, a perl quine is a perl script that when run with perl without any command line arguments, it outputs precisely its source code on stdout. (If you also allow stderr instead of stdout, you can get a little shorter by changing print to warn in some of these. I'm not sure which definition is better.) Some quines are considered cheating, namely those that read their own source code from the beginning, e.g. with the famous open 0 trick. It is also considered cheating to depend on the filename of the source code, or an environment variable you have to set beforehand, or, in general, any information source that's outside the source code which you have to specifically set up; that is, the quines should just work without any special tricks on how to invoke them.

Before each quine, I list a short name for identifying it, the length of its source code in bytes, and the number of non-whitespace bytes in its source.

Note that 323b and 323e end in two newlines; 323a and 323g end in one newline; while 323c, 323d, 323f and 323h ends in a graphic character.

One of these quines (323e) is the shortest non-empty one I know, but it has the disadvantage that it only works in perl 5.6.0 or later.

323a: 44 40

print<DATA>x2 __END__ print<DATA>x2 __END__

323b: 35 32

print<<''x2,"\n" print<<''x2,"\n"

323c: 44 44


323d: 50 48

$\="\47";print,print for'$\="\47";print,print for'

323e: 31 26

print<< x2,v10 print<< x2,v10

323f: 46 44

$\=v39;print,print for'$\=v39;print,print for'

323g: 58 57


323h: 52 52


Update 2012-12-12: see also General quine, How I Learned About Quines.

Replies are listed 'Best First'.
Re: Short quines
by truedfx (Monk) on Jan 13, 2008 at 13:53 UTC
    print<<''x2,"\n" print<<''x2,"\n"
    Would you consider it cheating to shorten this to
    print<<''x2,$/ print<<''x2,$/
    ? It relies on the default value for $/, but I wouldn't think it's a cheat.

      No, that's not cheating of course.

Re: Short quines
by Snarius (Sexton) on Jan 12, 2008 at 00:23 UTC
    Here's mine quine.
    use strict; #yesindeed
    use LWP;
    use HTTP::Request;
    my $ua = LWP::UserAgent::new ('LWP::UserAgent');
    LWP::UserAgent::agent ($ua, 'Mozilla/5.0');
    my $url = "";
    my $request = HTTP::Request::new ('HTTP::Request', GET => $url);
    my $response = LWP::UserAgent::request ($ua, $request);
    my $page = $response->content;
    my $beginning = q/use strict/;
    my $end = q/q.positively./;
    $page =~ /($beginning.*$end)/s;
    my $code = $1 . "\n";
    print $code; 
      If you use <code> tags your quine becomes much simpler:
      use strict; use LWP; use HTTP::Request; my $ua = LWP::UserAgent::new ('LWP::UserAgent'); LWP::UserAgent::agent ($ua, 'Mozilla/5.0'); my $url = ";node_id=6 +62072"; my $request = HTTP::Request::new ('HTTP::Request', GET => $url); my $response = LWP::UserAgent::request ($ua, $request); print $response->content;
      Also, you don't break formatting for replies to your node.

      Edit: I think you want a question mark after dot-star. Otherwise it's q-positively trivial for me to break your code.

      Edit2: I just realized that, if you put a question mark after dot-star, the end of your regex matches itself. But there are ways around that.

        As the title of this thread is "short quines", let me shorten that code a bit.

        use LWP::Simple;getprint""

        Update: fixed code. It should now work.

        Update 2012-05-24:</c> see also Usually J.A.P.H..

        I tried using <code. It renders > as & gt; or something. I actually tried to eliminate the >'s from my code, which is why there are lines like LWP::UserAgent::request ($ua, $request); instead of $ua->request ($request);.

      It seems to be broken...

      use strict; my $code = "death to dot star!\n"; print $code;

      update: jdalbec already re-broke the code. Just noting it was silently updated -- boo! -- after I showed it broken the first time.

Re: Short quines
by mr_mischief (Monsignor) on Jan 29, 2008 at 00:25 UTC
    print<DATA> is cheating less than reading from the file through the OS, but is it still cheating?

      I think print<DATA> is not cheating as you're only reading the part of the file that's not interpreted as a program, so it's not much different from printing a here document. On the other hand, if you seek DATA,0,0, that's cheating just like open 0 is, because you're reading what was already interpretted as a program. At least that is my opinion.

        That's my first impression of it, too, but I didn't know exactly where others would draw the line. I think the line is at any form of accessing the program text or a parsed representation of it directly. Reading the DATA section clearly doesn't do that.

        Part of the challenge of producing a quine in many languages is the careful formatting and escaping required. Reading a special section of data that can look like code without consequence makes that much easier. Perl's always been good at making things easier though (or rather, to give proper credit, Larry and the Porters have been good at it). That your language makes something easier (without going to the point of having a built-in quine() subroutine, anyway) probably shouldn't be considered cheating.

Re: Short quines
by ambrus (Abbot) on Sep 30, 2014 at 21:04 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://661934]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2017-03-29 03:27 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (343 votes). Check out past polls.