Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Help Explain Behavior

by Joe_ (Beadle)
on Aug 18, 2012 at 21:50 UTC ( #988253=perlquestion: print w/ replies, xml ) Need Help??
Joe_ has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, I know next to nothing about perlguts so I'm having a hard time understanding the behavior of Perl in a certain scenario.
When I run the following code:

#!/usr/bin/perl unlink $0; print "Hello, afterworld!\n";
The script file is deleted but the print statement executes! Can you please demystify this for me?

Comment on Help Explain Behavior
Download Code
Re: Help Explain Behavior
by LanX (Canon) on Aug 18, 2012 at 22:22 UTC
    Your running the script after it is loaded (and compiled) into memory.

    So the code can still be executed after the original source file vanished.

    Cheers Rolf

      Yes I figured as much. But that's exactly what's been bothering me. You say the script has been already compiled! How is that? I thought Perl is interpreted.
      Please pardon my ignorance and enlighten me...

        Your source is compiled into an internal form (a parse tree) which is then optimized before being run.

        Cheers Rolf

Re: Help Explain Behavior
by chromatic (Archbishop) on Aug 18, 2012 at 22:22 UTC

    Why would the program stop running even if it no longer exists on the disk?

    By the time the unlink happens, Perl's already loaded the program off of the disk into memory, compiled it, and started to run it.

    (It would be silly—and a lot of work—to write a programming language which had to read every line off of the disk before executing it. Not only would you get tripped up by things like operating system disk buffering, but you'd have a difficult time making things like function calls work well.)

      This really makes sense. Thank you.

Re: Help Explain Behavior
by BrowserUk (Pope) on Aug 18, 2012 at 23:31 UTC

    Its a little more surprising than that even. Not only is the script still in memory and still running after you unlink it.

    It is still on the disk also ... until the script exits ... at which point it automagically vanishes:

    C:\test>copy con surprise.pl #! perl -slw use strict; unlink $0; print "still here"; print for glob $0; print "Still there also"; ^Z 1 file(s) copied. C:\test>surprise.pl still here C:\test\surprise.pl Still there also C:\test>dir surprise.pl Directory of C:\test File Not Found C:\test>

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Now that seems really strange to me. I understand that the whole script was already loaded into memory by the time the unlink happens; but shouldn't the file vanish after the unlink?

        When Perl loads the script, it retains an open filehandle to the source. The filesystem won't allow the file to be deleted until all open filehandles are closed.

        So until the script ends, the source file is in a "marked for deletion" state.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988253]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-09-01 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls