[comp.lang.perl] shortest self-reproducing Perl program

merlyn@iwarp.intel.com (Randal Schwartz) (03/21/90)

Okay, here's my entry for the shortest self-reproducing Perl program.
Any takers for shorter?  Any one liners?

print<<'E'x 2,"E\n";
print<<'E'x 2,"E\n";
E

The program must be fed to an unadorned Perl command 'perl' and
produce the output that would make 'perl' look like it has been
replaced with 'cat'.  The null program is not a valid submission.  (I
claim it first... :-)

$_='5O1v3v5y9)1b7u2q4x1i0e3u2"3S9n5w7s6&7o7h8k1l6k3u3/';s/(.)(.)/pack('C',ord($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: "Welcome to Portland, Oregon, home of the California Raisins!"=/

raymond@russet.berkeley.edu (Raymond Chen) (03/21/90)

>Any one liners?

 [[ don't put a newline at the end ]]

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

Unfortunately, it doesn't work even though it should; the output is

$s='$s=%c%sc;printf$s,39,$s,39;';printf$s,39,$s,39;
	   ^ huh?

This happens with

	$Header: perly.c,v 3.0.1.4 90/02/28 18:06:41 lwall Locked $
	Patch level: 12

on a VAX8700 running ULTRIX.

Was this fixed in one of the more recent patches?

Adjust another curl attacker,		[motto for a curling iron repair shop?]
--
raymond@math.berkeley.edu     Maintainer of the csip Frequently Asked Questions

tneff@bfmny0.UU.NET (Tom Neff) (03/21/90)

Well this works slightly differently:

	#! /usr/bin/perl
	system "cat $0";

-- 
To have a horror of the bourgeois   (\(    Tom Neff
is bourgeois. -- Jules Renard        )\)   tneff@bfmny0.UU.NET

emv@math.lsa.umich.edu (Edward Vielmetti) (03/21/90)

In article <1990Mar20.215103.23101@agate.berkeley.edu> raymond@russet.berkeley.edu (Raymond Chen) writes:


   >Any one liners?

    [[ don't put a newline at the end ]]

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

   Unfortunately, it doesn't work even though it should; the output is

   $s='$s=%c%sc;printf$s,39,$s,39;';printf$s,39,$s,39;
	      ^ huh?

the spot marked "huh?" has an ascii NUL here, run it through
cat -v or od -a to see it.

--Ed

merlyn@iwarp.intel.com (Randal Schwartz) (03/21/90)

In article <1990Mar20.215103.23101@agate.berkeley.edu>, raymond@russet (Raymond Chen) writes:
| >Any one liners?
| 
|  [[ don't put a newline at the end ]]
| 
| $s='$s=%c%s%c;printf$s,39,$s,39;';printf$s,39,$s,39;
| 
| Unfortunately, it doesn't work even though it should; the output is
| 
| $s='$s=%c%sc;printf$s,39,$s,39;';printf$s,39,$s,39;
| 	   ^ huh?
| 
| This happens with
| 
| 	$Header: perly.c,v 3.0.1.4 90/02/28 18:06:41 lwall Locked $
| 	Patch level: 12
| 
| on a VAX8700 running ULTRIX.
| 
| Was this fixed in one of the more recent patches?

With pl15, it puts out a null in place of that last percent, yup.
And, it looks pretty good in other terms, so I presume that's a bug.
Larry?

eval <<EOF;
print "Just another Perl hacker,"
EOF
-- 
/=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: "Welcome to Portland, Oregon, home of the California Raisins!"=/

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/21/90)

In article <1990Mar20.203516.8429@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
: Okay, here's my entry for the shortest self-reproducing Perl program.
: Any takers for shorter?  Any one liners?
: 
: print<<'E'x 2,"E\n";
: print<<'E'x 2,"E\n";
: E
: 
: The program must be fed to an unadorned Perl command 'perl' and
: produce the output that would make 'perl' look like it has been
: replaced with 'cat'.  The null program is not a valid submission.  (I
: claim it first... :-)

An obvious abridgement is to delete all the E's:

print<<''x 2,"\n";
print<<''x 2,"\n";


Note the blank line at the end.  A not-so-obvious improvement is that
the last semicolon is now optional.  (For shame!  You of all people
should have thought of this one, Randal!)

print<<''x 2,"\n"
print<<''x 2,"\n"


It's too bad you said the thing about cat.  Otherwise

die<<''x 2."\n"
die<<''x 2."\n"


would qualifiy.

But, we can shorten the "\n" by two characters:

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


I haven't thought about one liners yet.

Larry

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/21/90)

In article <1990Mar20.215103.23101@agate.berkeley.edu> raymond@math.berkeley.edu (Raymond Chen) writes:
: >Any one liners?
: 
:  [[ don't put a newline at the end ]]
: 
: $s='$s=%c%s%c;printf$s,39,$s,39;';printf$s,39,$s,39;
: 
: Unfortunately, it doesn't work even though it should; the output is
: 
: $s='$s=%c%sc;printf$s,39,$s,39;';printf$s,39,$s,39;
: 	     ^ huh?
: 
: This happens with
: 
: 	$Header: perly.c,v 3.0.1.4 90/02/28 18:06:41 lwall Locked $
: 	Patch level: 12
: 
: on a VAX8700 running ULTRIX.
: 
: Was this fixed in one of the more recent patches?

Depends on how recent you think patch 16 is.   :-)

do_sprintf() was assuming that you wouldn't sprintf a string using itself
as the format, so it temporarily installed nulls in the string to
handle each % interpolation.  Obviously a bad assumption...

Life's a beach.  And then you dry.

Larry

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/21/90)

In article <7490@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
: 
: print<<''x 2,$/
: print<<''x 2,$/
: 
: 
: I haven't thought about one liners yet.

Here is a one-liner.  There may be shorter one-liners.

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

Larry

merlyn@iwarp.intel.com (Randal Schwartz) (03/22/90)

In article <7490@jpl-devvax.JPL.NASA.GOV>, lwall@jpl-devvax (Larry Wall) writes:
| Note the blank line at the end.  A not-so-obvious improvement is that
| the last semicolon is now optional.  (For shame!  You of all people
| should have thought of this one, Randal!)

I thought that was just for 'eval' (because of my original request).

Does that mean the book is off? :-)

$_="7072696e7420274a75737420616e6f74686572205065726c206861636b65722c27";
s/../pack('C',hex($&))/eg; eval;
-- 
/=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: "Welcome to Portland, Oregon, home of the California Raisins!"=/

tneff@bfmny0.UU.NET (Tom Neff) (03/22/90)

The following ILLEGAL two-liner reproduces itself via descriptor 2:

	syntax error in file - at line 1, next 2 tokens "syntax error"
	Execution aborted due to compilation errors.

-- 
"We must never forget that if the war in Vietnam    \ $   Tom Neff
is lost... the right of free speech will be          X    tneff@bfmny0.UU.NET
extinguished throughout the world." -- RN 10/27/65  $ \   uunet!bfmny0!tneff

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/22/90)

In article <1990Mar21.163855.13099@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
: In article <7490@jpl-devvax.JPL.NASA.GOV>, lwall@jpl-devvax (Larry Wall) writes:
: | Note the blank line at the end.  A not-so-obvious improvement is that
: | the last semicolon is now optional.  (For shame!  You of all people
: | should have thought of this one, Randal!)
: 
: I thought that was just for 'eval' (because of my original request).

That was for -e.  eval has assumed a semicolon for a long time.

But it was easier to assume ; on all perl scripts than just on -e.

: Does that mean the book is off? :-)

It just means it doesn't have to end with a semicolon.
Now if we could just get it to reproduce itself...

Actually, I'd settle for it PROducing itself.  I'm still slogging through
the tutorial...

If you folks out there have any examples you think are instructive for
novices, we'd sure like to see them.  We can't promise to work them
in, but we're particularly looking for small scripts that illustrate
particular constructs constructively.  You could get your name in print...

Say, Randal, do you think we should have an appendix of signature scripts?

Larry

tkacik@rphroy.UUCP (Tom Tkacik) (03/23/90)

In article <15274@bfmny0.UU.NET> tneff@bfmny0.UU.NET (Tom Neff) writes:
>Well this works slightly differently:

>	#! /usr/bin/perl
>	system "cat $0";


Let's take this one to the extreme. And we don't even need perl ;-(.

Just put the following line in the file junk, and make it executable.
		#!/bin/cat junk


-- 
Tom Tkacik		GM Research Labs,   Warren MI  48090
uunet!edsews!rphroy!megatron!tkacik		Work Ph: (313)986-1442
"Csh must go.  This is non-negotiable!"

jmm@eci386.uucp (John Macdonald) (03/23/90)

Rather than strain my brain, I let the computer find a short
self-reproducing perl "program" (well, it is a program in the
sense that its purpose in life is to be submitted to a Perl
interpreter to cause it to generate output, but it has no
other use).  The shell script to generate the program, and
the program it found, are:

---- cut here for genrepro sh script ----
: genrepro - generate a self-reproducing perl program
echo junk >cur.prog
echo trash >cur.out
until cmp -s cur.prog cur.out
do
    echo === not yet ===
    cat cur.out
    mv cur.out cur.prog
    perl cur.prog 2>cur.out
done

mv cur.out cur.prog

echo === final version ===
cat cur.prog
---- end of genrepro sh script ----
---- cut here for cur.prog ----
syntax error in file cur.prog at line 1, next 2 tokens "syntax error"
Execution aborted due to compilation errors.
---- end of cur.prog ----
-- 
Algol 60 was an improvment on most           | John Macdonald
of its successors - C.A.R. Hoare             |   jmm@eci386

phillips@cs.ubc.ca (George Phillips) (03/27/90)

In article <7494@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
>
>Here is a one-liner.  There may be shorter one-liners.
>
>$_=q $_=qx;s/x/$"$_$"/;print ;s/x/$"$_$"/;print

How about:

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

The spaces are tabs, no newline at the end.  There's also its more printable
cousin:

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

But the 2 digit octal sequences seem kinda bogus.

George Phillips phillips@cs.ubc.ca {alberta,uw-beaver,uunet}!ubc-cs!phillips

jhpb@granjon.ATT.COM (Joseph H. Buehler) (03/28/90)

Here's the world's smallest.  Quoted so you can see it:

""

[granjon] $ perl junk > junk.out
[granjon] $ ls -l junk*
-rw-r--r--   1 jhpb     arch           0 Mar 27 19:01 junk
-rw-r--r--   1 jhpb     arch           0 Mar 27 19:05 junk.out

Joe Buehler

nwinton@dalriada.axion.bt.co.uk (Neil Winton) (03/28/90)

Um ... err ... I may have missed this one going by but how about

	exec 'cat', $0;

Nahhh ... too much of a cheat ... but it does fulfill Randal's requirement
that `"perl file" should behave exactly as "cat file"' :-)

Neil

E-Mail (UUCP)  NWinton@axion.bt.co.uk (...!uunet!mcsun!axion.bt.co.uk!nwinton)
Organisation   British Telecom Research Laboratories (RT3134)
Snail Mail     306 SSTF, BTRL, Martlesham Heath, IPSWICH IP5 7RE, UK
Telephone      +44 473 646079 (or +44 473 643210)
*** This line intentionally left justified ***

merlyn@iwarp.intel.com (Randal Schwartz) (03/29/90)

In article <JHPB.90Mar27190649@granjon.ATT.COM>, jhpb@granjon (Joseph H. Buehler) writes:
| 
| Here's the world's smallest.  Quoted so you can see it:
| 
| ""

Go back to my original article.  I already DQ'ed that.  Nice try. :-)

open(STDERR,">&STDOUT");warn "Just another Perl hacker,\n"
-- 
/=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: "Welcome to Portland, Oregon, home of the California Raisins!"=/