[comp.lang.perl] Self-reproducing program

worley@compass.com (Dale Worley) (03/27/91)

I don't know if this has been hashed over before, but: What is the
shortest self-reproducing Perl program?  (Well, the null file is, but
what is the shortest non-trivial program?)

The best I've been able to do is:

print @a=<DATA>,@a
__END__
print @a=<DATA>,@a
__END__

Dale

Dale Worley		Compass, Inc.			worley@compass.com
--
So you want to have a shameful affair?
Yet, somehow you can't justify it?

The next time you are contemplating a decision in which you are debating
whether or not to go for the gusto, ask yourself this Important Question:

	How long will I be dead?

With that perspective, you can now make a free, fearless choice to do
just about any goddamned sneaky thing your devious little mind can think
up.  Go ahead.  Have your fun.  You're welcome.  Go on.  See you in
Hell.
		--Matt Groening

flee@cs.psu.edu (Felix Lee) (03/27/91)

Here's a shorter non-null self-reproducing program:

print<DATA>x 2 __END__
print<DATA>x 2 __END__

And here's one shorter, but less portable:

open(Z,$0);print<Z>

What's the shortest pair of mutually-reproducing programs?  i.e., two
different programs A and B such that A produces B and B produces A.
Neither A nor B should rely on hard-coded file names.
--
Felix Lee	flee@cs.psu.edu

merlyn@iwarp.intel.com (Randal L. Schwartz) (03/28/91)

In article <.c5G=2v91@cs.psu.edu>, flee@cs (Felix Lee) writes:
| Here's a shorter non-null self-reproducing program:
| 
| print<DATA>x 2 __END__
| print<DATA>x 2 __END__
| 
| And here's one shorter, but less portable:
| 
| open(Z,$0);print<Z>

Here's a short *and* portable version of that:

seek(DATA,0,0);print<DATA>__END__

It even works as an arg to "perl -e"!

$_ = "Just5another8Perl5hacker,"; s/(\D+)(\d)/pack("A$2",$1)/eg; print
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/

hansm@cs.kun.nl (Hans Mulder) (03/28/91)

In article <1991Mar26.210348.12286@uvaarpa.Virginia.EDU> worley@compass.com writes:
>I don't know if this has been hashed over before, but: What is the
>shortest self-reproducing Perl program?  (Well, the null file is, but
>what is the shortest non-trivial program?)
>
>The best I've been able to do is:
>
>print @a=<DATA>,@a
>__END__
>print @a=<DATA>,@a
>__END__

Last time we had this debate, several shorter scripts were posted:

The first example is a 3-liner; the third line being empty:

print<<''x 2,$/
print<<''x 2,$/


The ohter four examples do not contain newlines; according to wc(1),
they are 0-liners:

This one assumes ascii, to the extent that ord("'")==39:

$s='$s=%c%s%c;printf$s,39,$s,39';printf$s,39,$s,39

This one does not unduly assume anything:

$_=q $_=qx;s/x/$"$_$"/;print ;s/x/$"$_$"/;print

The whitespace in this one must be tabs:

$_=q	print"\$_=q\t$_\t;eval"	;eval

This one again uses "\47" eq "'":

$_='print"\$_=\47$_\47;eval"';eval


Have a nice day,

Hans Mulder	hansm@cs.kun.nl

flee@cs.psu.edu (Felix Lee) (03/28/91)

Why does
	perl -e 'seek(DATA,0,0);print<DATA>__END__'
emit a newline?

Bonus question.  Why doesn't this do the same thing?
	perl -e 'seek(DATA,0,0);print<DATA>^Z'
where ^Z is a control-Z character
--
Felix Lee	flee@cs.psu.edu

jbw@bigbird.bu.edu (Joe Wells) (03/29/91)

merlyn@iwarp.intel.com (Randal L. Schwartz) writes:

   seek(DATA,0,0);print<DATA>__END__

Is this guaranteed?  Will Larry guarantee that DATA is always open on a
seekable file that contains the entire text of the program?

-- 
Joe Wells <jbw@bu.edu>

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (03/29/91)

In article <JBW.91Mar28120741@bigbird.bu.edu> jbw@bigbird.bu.edu (Joe Wells) writes:
: merlyn@iwarp.intel.com (Randal L. Schwartz) writes:
: 
:    seek(DATA,0,0);print<DATA>__END__
: 
: Is this guaranteed?  Will Larry guarantee that DATA is always open on a
: seekable file that contains the entire text of the program?

Nope.  It doesn't work if you say

    echo 'seek(DATA,0,0);print<DATA>__END__' | perl

However, it's guaranteed to work if the script is coming from a file.
For the moment, that includes when you use -e, because it builds a temp file.

Larry