Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

This turned out not to be the disaster I expected

by Dominus (Parson)
on Jun 06, 2007 at 19:28 UTC ( #619650=perlmeditation: print w/replies, xml ) Need Help??

I was initially thinking about a module that would generate and execute every possible piece of Perl code when it was loaded. Such a module would be very useful, because once you loaded it, you wouldn't have to actually run your program; it would already have done whatever it was that you wanted.

As a first cut, I wrote:

$s = "a"; while (length($s) < 100) { eval $s; $s++ }
realizing that this wasn't going to be what I wanted, because it doesn't generate any code with spaces or punctuation, but I thought it might be fun, at least as a joke. It looks like it is going to take a long time to execute, so I ran it with some fear that it might crash my machine or do something else bad.

But actually it executes rather quickly, and nothing bad happens. As I said, it was not the disaster I expected.

Can you predict the results?

Replies are listed 'Best First'.
Re: This turned out not to be the disaster I expected
by imp (Priest) on Jun 06, 2007 at 19:39 UTC
    It would call dump since that's the first command it would come across that would force an exit. Next up would be exec and exit.

      Actually, it looks as if exec does not do the job. I get "foo" from:

      perl -le '$x="exec";eval $x;print "foo";'

      Not so for dump and exit.

      ... and takes a breather at
      getc

      No exit with exec though. perldoc exec only mentions

      exec LIST exec PROGRAM LIST
Re: This turned out not to be the disaster I expected
by kyle (Abbot) on Jun 06, 2007 at 19:41 UTC

    Well, I suppose it hits exit pretty quick, and that's the end of it.

    Doh, I see imp beat me to it, and with a better answer to boot.

Re: This turned out not to be the disaster I expected
by shmem (Chancellor) on Jun 06, 2007 at 21:36 UTC
    I thought first that it would exit when $s is ... well, "exit". But another keyword comes first - dump, which causes perl to kill itself with SIGABRT.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: This turned out not to be the disaster I expected
by BrowserUk (Pope) on Jun 06, 2007 at 19:37 UTC
      Nope, die inside an eval won't kill the app.
Re: This turned out not to be the disaster I expected
by naikonta (Curate) on Jun 07, 2007 at 02:53 UTC
    This can be a way to find a c00l n4m3 or some magic number?
    perl -le '$s = shift; while (length($s) < 100) { print $s; eval $s; $s +++; }'
    But this one is less fun :-)
    perl -le '$s = shift; while (length($s) < 100) { print $s; eval $s; $s +++; }' 'kill 9, $$'

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: This turned out not to be the disaster I expected
by ambrus (Abbot) on Jun 10, 2007 at 00:23 UTC

    Hmm let me think. It's in eval, so simple errors like m or die won't do anything. exit will quit. But dump is before exit. Is there anything earlier?

    Let me look at the keyword list at ode getprotobyname generator...

    Hmm, maybe the disaster you have expected was a fork bomb... I think eval"redo" isn't an infinite loop, but I try it in the console to be sure. Hmm. It is an infinite loop the way your code is now, because it jumps to the beginning of the while loop and the $s++ gets skipped.

    Let me think that dump thing over. I think they're planning to remove that keyword so it can only be called as CORE::dump. I look it up in the manual... Try it in the console... So it warns only for now.

    Ok, there's nothing more I can think of, so my prediction is that it will dump core.

    Update: Ok, let me think this over again after reading the other replies.

    Firstly, I realized that fork wouldn't cause a fork bomb as I thought for some reason, only fork exactly once. Now I didn't realize getc would do something significant (as opposed to, say, warn which just prints some output) until I've read FunnyMonk's reply. So this means that if we get over dump and exit, the script would fork exactly once and then try to read the terminal from both processes. This is not the typical job control situation, because the two interpreters are in the same process group, so I have to look it up in the manual what happens exactly. *looks it up* I can't find it out from the manual. Let me try. *tries* Apparently nothing special happens, they read lines after each other in some random order.

    Secondly, I realized that I thought of redo but not last or next. last would obviously exit the loop and thus end the program. next would also cause an infinite loop the same way as redo, but this could be avoided by putting the $s++ in a continue block.

    Of course, none of these change my answer being dump.

Re: This turned out not to be the disaster I expected
by djp (Hermit) on Jun 07, 2007 at 03:03 UTC
    I'm predicting you hit '?' early in the piece. Update: didn't hide my answer as others did.

      No, because magical autoincrement on alphanumeric strings never strays outside the alphanumeric character set.

Re: This turned out not to be the disaster I expected
by gaal (Parson) on Jun 07, 2007 at 16:29 UTC
    I seem to vaguely recall someone discovering this sometime in the 90ies, maybe even on c.l.p.m.
Re: This turned out not to be the disaster I expected
by diotalevi (Canon) on Jun 09, 2007 at 00:29 UTC

    It leaks memory while it's working.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://619650]
Approved by imp
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2021-04-16 09:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?