Jump to content

Quine: Difference between revisions

1,415 bytes removed ,  16 years ago
Clarified. Removed Perl, PHP and UNIX Shell examples
(Remove Java example which just copies the file Quine.java to standard output.)
(Clarified. Removed Perl, PHP and UNIX Shell examples)
Line 1:
{{puzzle}}
{{clarified-review}}
Print out a program's own source code. Since this is a puzzle, shorter/more efficient versions are encouraged along side more obvious versions.
Print out a program's own source code without reading from the source file.
{{clarify_task}}
 
 
=={{header|Ada}}==
Line 29 ⟶ 30:
 
Haskell does not keep the code in an uncompiled-equivalent form around at runtime, so the "quotation trick" has to be used.
 
=={{header|Perl}}==
This example retrieves the name of the source file from the $0 variable, which represents the file path and name used to invoke the script.
 
#!/usr/bin/perl
open FILE, "< $0" or die "Failed to open self\n";
map { print $_ } <FILE>;
close FILE;
 
Assuming the code is saved in the file quine.pl, this works when executed as
./quine.pl
or
perl quine.pl
but fails when fed to the perl interpreter's standard input as
cat quine.pl|perl
 
If we may assume that the source file is named "quine.pl", replacing
open FILE, "< $0" or die "Failed to open self\n";
with
open FILE, "< quine.pl" or die "Failed to open self\n";
 
will allow us to execute the code through the interpreter's [[standard input]]:
cat quine.pl|perl
 
=={{header|PHP}}==
This example obtains its own filename from the __FILE__ built-in variable.
 
<?
$fh = fopen( __FILE__, 'r' );
echo fread($fh, filesize(__FILE__));
fclose( $fh );
?>
 
Strictly speaking, however, the example prints more than its own source code. Because PHP is designed to service HTTP requests, the output looks like this:
 
X-Powered-By: PHP/5.2.3-1ubuntu6
Content-type: text/html
<?
$fh = fopen( __FILE__, 'r' );
echo fread($fh, filesize(__FILE__));
fclose( $fh );
?>
 
(The X-Powered-By line will vary from system to system.)
 
=={{header|Seed7}}==
Line 103 ⟶ 59:
 
Original source: [http://seed7.sourceforge.net/algorith/puzzles.htm#self]
 
=={{header|UNIX Shell}}==
 
#!/bin/sh
cat "$0"
Cookies help us deliver our services. By using our services, you agree to our use of cookies.