Program that prints its source code

D

David K. Wall

If I want a program to print its own source code, is there any reason
to prefer one of these code snippets over the other? Or is there an
even better way to do it?

The context I'm thinking of is a CGI program that can be used as
working code and also optionally provide a copy of itself to interested
persons. (Obviously I'd have to provide proper HTTP headers....)


Snippet 1:

seek DATA, 0, 0;
print <DATA>;
__DATA__


Snippet 2:

open SELF, $0 or die "Error opening self for reading";
print <SELF>;
 
B

Ben Morrow

David K. Wall said:
If I want a program to print its own source code, is there any reason
to prefer one of these code snippets over the other? Or is there an
even better way to do it?

Snippet 1:

seek DATA, 0, 0;
print <DATA>;
__DATA__


Snippet 2:

open SELF, $0 or die "Error opening self for reading";
print <SELF>;

I would prefer the first, just because I always prefer to use a handle
I've got on the file rather than open another... also, I believe $0 is
not always perfectly reliable, as in there are some situations on some
OSen where it is impossible for perl to correctly find the path to the
running script.

In any case, the second should use FindBin (modulo the thread a while
ago about whether or not it's broken...).

Ben
 
D

David K. Wall

Ben Morrow said:
I would prefer the first, just because I always prefer to use a
handle I've got on the file rather than open another...

Good point. That in itself is enough to convince me.
 
M

Michele Dondi

If I want a program to print its own source code, is there any reason
to prefer one of these code snippets over the other? Or is there an
even better way to do it?

This is called a 'quine'. Try and search this NG for that word on
google!
The context I'm thinking of is a CGI program that can be used as
working code and also optionally provide a copy of itself to interested
persons. (Obviously I'd have to provide proper HTTP headers....)

Snippet 1:

seek DATA, 0, 0;
print <DATA>;
__DATA__

Snippet 2:

open SELF, $0 or die "Error opening self for reading";
print <SELF>;

I think that these would be considered both "illegal" quines for most
definitions of quine.

Benjamin Goldberg (who used to be one of the most valuable
contributors to this NG since when I first joined it - BTW: where is
he now?) posted more complex, nontrivial quines in the thread "Perl
Golf on my Mind" in January.


Michele
 
A

Anno Siegel

Michele Dondi said:
Benjamin Goldberg (who used to be one of the most valuable
contributors to this NG since when I first joined it - BTW: where is
he now?) posted more complex, nontrivial quines in the thread "Perl
Golf on my Mind" in January.

He was pretty active on p5p, last I looked.

Anno
 
M

Michele Dondi

Yeah, well, this wasn't for a contest or anything, just a toy program I

Fine then: I was just repeating something (falling in the category of
"interesting information", of course) I had heard somewhere else.
wrote that I wanted to make available to other people for them to play with
or extend if they were interested. It's completely trivial, but fun, so
here's the program: http://ylatis.com/cgi-bin/tile

I just went there... FWIW, I think it's cool!


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top