Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Program that prints its source code

Reply
Thread Tools

Program that prints its source code

 
 
David K. Wall
Guest
Posts: n/a
 
      11-18-2003

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>;


--
David Wall
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      11-18-2003
"David K. Wall" <> wrote:
> 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

--
EAT
KIDS (...er, whoops...)
FOR
99p
 
Reply With Quote
 
 
 
 
David K. Wall
Guest
Posts: n/a
 
      11-18-2003
Ben Morrow <> wrote:

> "David K. Wall" <> wrote:
>> 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...


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

--
David Wall
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      11-19-2003
On Tue, 18 Nov 2003 18:56:02 -0000, "David K. Wall"
<> wrote:

>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
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-19-2003
Michele Dondi <> wrote in comp.lang.perl.misc:

> 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
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      11-21-2003
On Thu, 20 Nov 2003 03:02:03 -0000, "David K. Wall"
<> wrote:

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

>
>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
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
program that prints its source code Prashanth Badabagni C Programming 7 09-24-2003 10:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57