Program that prints its source code

Discussion in 'Perl Misc' started by David K. Wall, Nov 18, 2003.

  1. 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
    David K. Wall, Nov 18, 2003
    #1
    1. Advertising

  2. David K. Wall

    Ben Morrow Guest

    "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
    Ben Morrow, Nov 18, 2003
    #2
    1. Advertising

  3. 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
    David K. Wall, Nov 18, 2003
    #3
  4. 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__
    Michele Dondi, Nov 19, 2003
    #4
  5. David K. Wall

    Anno Siegel Guest

    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
    Anno Siegel, Nov 19, 2003
    #5
  6. 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__
    Michele Dondi, Nov 21, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    23
    Views:
    3,812
    George W. Cherry
    Oct 6, 2003
  2. Prashanth Badabagni

    program that prints its source code

    Prashanth Badabagni, Sep 21, 2003, in forum: C Programming
    Replies:
    7
    Views:
    440
    Kevin Easton
    Sep 24, 2003
  3. thunk
    Replies:
    1
    Views:
    294
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    458
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    605
    thunk
    Apr 3, 2010
Loading...

Share This Page