61
\$\begingroup\$

Long-time lurker, first-time poster. So here goes.

In the Wikipedia page for quine, it says that "a quine is considered to be 'cheating' if it looks at its own source code." Your task is to make one of these "cheating quines" that reads its own source code.

This is , so the shortest code in bytes - in each language - wins. This means that a 5-byte Pyth script would not beat a 21-byte Python script - but a 15-byte Python script would.

You must use file I/O to read the source code, so the following JavaScript code, taken from the official Wikipedia page, is invalid:

function a() {
    document.write(a, "a()");
}
a()

It must access the source code of the file on disk.

You are not allowed to specify the file name. You must make it detect the filename itself.

Everyone clear? Go!

\$\endgroup\$
12
  • 1
    \$\begingroup\$ Is a trailing newlines not present in the original file allowed? \$\endgroup\$ – isaacg Oct 29 '15 at 22:04
  • 3
    \$\begingroup\$ @isaacg IMHO That's not a quine, since it is not the source code. \$\endgroup\$ – user42643 Oct 29 '15 at 22:05
  • 3
    \$\begingroup\$ You should state a requirement that it determine the actual filename instead of assuming a hard-coded string for the source location. \$\endgroup\$ – feersum Oct 29 '15 at 22:26
  • 3
    \$\begingroup\$ I agree with @feersum though, that requiring a specific file name makes this challenge way to trivial. \$\endgroup\$ – user42643 Oct 29 '15 at 22:33
  • 1
    \$\begingroup\$ Can we assume that (for compiled languages) the source code is in the same folder (i.e. we can just add ".cpp" or ".hs" to arg[0] to get the source). \$\endgroup\$ – HEGX64 Nov 1 '15 at 9:15

72 Answers 72

1 2
3
0
\$\begingroup\$

Matlab, 32 bytes

disp(fileread([mfilename,'.m']))
\$\endgroup\$
0
\$\begingroup\$

APL, 8 bytes (non-competing)

APL does not use source files for single functions, rather a whole workspace (a machine-readable collection of token-representations) is saved and loaded, so I guess this is not allowed:

f     
⎕CR'f'

(⎕CR is Character Representation) as the program reads its current definition in memory (the loaded workspace), which may have been modified from the source in the file.

\$\endgroup\$
0
\$\begingroup\$

HTML and Javascript, 88 Bytes

<head></head><body><svg onload="alert(document.documentElement.innerHTML)"></svg></body>

Had to add <head></head><body></svg></body> because that's what the browser outputs regardless of the original file.

\$\endgroup\$
2
  • \$\begingroup\$ #HTML and Javascript, 109 Bytes# <html><head></head><body onload="alert('<html>'+document.documentElement.innerHTML+'</html>')"></body></html> @Masterzagh, Actually browsers add html tag too. Sorry, Couldn't add a comment as I don't have enough reputation. \$\endgroup\$ – prathapa reddy Jan 10 '17 at 12:32
  • \$\begingroup\$ I tested it on firefox and chrome and neither added the html (at least on the alert which is what we're interested in). @prathapareddy \$\endgroup\$ – user64039 Jan 10 '17 at 16:45
0
\$\begingroup\$

Lua, 32 29 bytes

3 bytes saved thanks to Jonathan Frech

print(io.open(arg[0]):read())

Try it online!

How?

Lua uses the arg table to store it's arguments, the script name is always stored at index 0 of this table despite the fact there are no further arguments. After using this table to get the name of the file, printing it was as easy as opening the file and reading its content.

\$\endgroup\$
2
  • \$\begingroup\$ I think you can toss your variable and save three bytes. \$\endgroup\$ – Jonathan Frech Sep 24 '18 at 20:40
  • \$\begingroup\$ It is interesting, how your script's file name is stored at 'index' zero, even though LUA is 1-indexed ... And it goes on: arg[-1] contains the language name ... \$\endgroup\$ – Jonathan Frech Sep 24 '18 at 21:49
0
\$\begingroup\$

Perl 6 / POSIX shell, 16 bytes

run 'cat',$?FILE

Try it online!

\$\endgroup\$
0
\$\begingroup\$

CopyPasta Language, 32 bytes

But I think this is interesting enough to compete here.

CopyFile
TheFuckingCode
Pasta!

Copy the source code, display the clipboard, and stop the program.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Worthy of note: This language did not have an interpreter when you posted this, so it would have been invalid. As of rn, I have implemented an interpreter for the language, since the linked interpreter on the esolang is a dead link \$\endgroup\$ – Conor O'Brien Oct 3 '19 at 3:06
0
\$\begingroup\$

stacked, 11 bytes

program put

Try it here!

Outputs the program stored in the program variable. Simple!

\$\endgroup\$
0
0
\$\begingroup\$

Jelly u, 26 bytes

“open(sys.argv[2])”ŒV

Try it online!

Not encoded in Jelly's SBCS, because 1. by meta consensus on flags Jelly u is a different language from Jelly (so it's not like I'm handicapping myself or anything), 2. I'm having some difficulties deciding on what form of output would be valid for an SBCS Jelly cheating quine, and 3. I'm having some difficulties actually executing on the methods I've considered (note that that's not even shorter than this).

“open(sys.argv[2])”ŒV    Evaluate "open(sys.argv[2])" as Python, within Jelly's interpreter.py.

      sys.argv           Get the arguments with which this was invoked on the command line.
                         (jelly fu [whatever the file's called])
              [2]        Take the third argument,
 open(           )       and open the file at that location.

                   ŒV    Iterate through the file object, obtaining a Jelly string of its contents.
\$\endgroup\$
0
\$\begingroup\$

05AB1E (legacy), 43 39 30 27 bytes

’ƒÛ(__£Ø__('Ë‚').Òœv[1])’.E

Try it online! I would've used the rewrite, if only I knew Elixir… Output doesn't contain the program, however it is the program in a singleton iterable object (a file). This is allowed.

’ƒÛ(__£Ø__('Ë‚').Òœv[1])’.E  # full program
’ƒÛ(__£Ø__('Ë‚').Òœv[1])’    # push "open(__import__('sys').argv[1])"...
                         .E  # evaluated as Python code
                             # implicit output

============================================================================

open(__import__('sys').argv[1])  # full Python program
open(                         )  # return file at path...
                           [1]   # second...
     __import__('sys').argv      # command-line argument

Note: This program assumes no command-line flags or extra arguments are passed. If this assumption is not allowed, it can be fixed at a cost of 1 byte by adding a - before the 1.

\$\endgroup\$
0
\$\begingroup\$

Pxem (esolang-box notation), 21 bytes.

.fak.-.f.p
.fak.-.f.p

Try it online!

How it works

In this notation,

  • 1st line is main routine.
  • .f is for push its own content except 1st line.

ak.- is an idiom to push LF; .p pops all to print them.

\$\endgroup\$
4
  • \$\begingroup\$ This is one of the most controversial posts. \$\endgroup\$ – tail spark rabbit ear Apr 3 at 17:38
  • 1
    \$\begingroup\$ This is certainly not "one of the most controversial posts", when no controversy has arisen yet. Personally, I would say this answer follows the spirit of the rules but not the letter of them, and won't flag this answer for deletion myself, but I wouldn't be surprised if someone else does. \$\endgroup\$ – pppery Apr 4 at 1:12
  • 1
    \$\begingroup\$ Unfortunately, your second answer is invalid as considering the filename+NUL+content to be the source code isn't correct, and your first answer is invalid as you're not allowed to specify the file name. I'm sorry, but this will have to be flagged for deletion. \$\endgroup\$ – Makonede Apr 14 at 2:33
  • \$\begingroup\$ @makonede fixed last month. \$\endgroup\$ – tail spark rabbit ear Jun 8 at 11:56
0
\$\begingroup\$

Julia, 29 bytes

print(read(@__FILE__,String))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Zsh, 17 bytes

x () {
	which x
}

Try it online!

Much longer than a program cheating quine, which would be <$0; this is a function called x.

which is a built-in that prints the definition of the given command or function, but it uses a particular format means we need to slightly adjust the way we define the function which is not actually the shortest way.

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.