Code that will only execute once

  • Goal

    The goal of this challenge is to write code that will execute once and only once. This means basically that it damages the program, script, or environment in some way. If rebooting the system allows the code to run again that is permitted.


    Number of votes. All assumptions must be clearly listed. Any answers just initiating a reboot or halt will be disqualified.

    Additional Rules because Greg Hewgill is a demi-god

    No root access is permitted.

    End Date

    The contest will close on May 31, 2014.


    This contest has been changed to popularity contest.

    @PeterTaylor a possible Solution would be the a self delete but as the result are showing it isn't the only one.

    Is it ok, if the programm shuts the PC down?

    No restarting the computer is equivalent to halt.

    I feel that this would be much more interesting as a `popularity-contest` type question. Perhaps ask it again, or a similar one. Too many simple low character count winners that are already answered in the program that deletes itself question.

    this is now a popularity contest.

    To a lot of people calling `vi` in a single terminal environment has this effect, no escape from it unless you reboot :) Just a joke here.

    I feel like this would be much more interesting if the original script had to be able to do something useful. Using the shebang line to mangle or remove the file is a funny interpretation of the original question, but doesn't really allow executing a "script" in the popular meaning of some Turing-complete code.

    The site is free for you to propose such a question.

    I suspect it will be closed as a duplicate.

    Trapdoors! Don't you all remember Jurassic Park (book, not movie)?

    `echo "If you try to execute me again, it means you are an idiot.";` <-- Nobody will execute more than once :P

    @orion You misspelled "emacs".

    Would missile-related software have qualified? ;)

    fork bomb? `:(){ :|: & };:`

    `echo "This program prints gullible if it is executed again."`

  • Martin Ender

    Martin Ender Correct answer

    7 years ago


    Finally a usecase for Vigil!

    def main():
    raise Exception()

    Excerpt from the "language specification":

    It goes without saying that any function that throws an exception which isn't caught is wrong and must be punished.


    If an oath is broken, the offending function [...] will be duly punished.


    Simple: it will be deleted from your source code.

    The only way to ensure your program meets its requirements to absolutely forbid code that fails to do so. With Vigil, it will do this for you automatically.

    There are other ways to do this, because Vigil provides the keywords implore and swear which are basically oaths to adhere to certain pre- and post-conditions:

    def main():
    swear 0 > 1

    No doubt, this _deserves_ to win.

    Finally, a sufficiently vigilant language for the modern age! I'm going to start using it in production. All bugs must be purged.

    To me it seems that it is not necessarily the fault of the function that throws an uncaught exception, it could also be that the functions on the call stack are negligent for failing to catch it. It could even suggest the architecture of the whole application is flawed (Because perhaps there is no way to properly handle that exception without restructuring the code?) In short, the whole program should be deleted.

    Additional points for swearing. A program making a confident false statement no doubt needs to be eliminated.

    Congratulation for winning and thank you for participating.

    Omg this language is awesome!

    Granted, it's not `code-golf`, but how about a simple `def main():x` which will `NameError`?

    @NickT ah, good point. This answer was never competitive for code golf with all those 4 to 6 character answers, so I went for something simple and neat, but didn't really care about properly golfing it down. (You can see in the edit history that there was a stage where I actually said "heck, this isn't winning anyway, but I'm posting it for the fun of it" back when the challenge actually *was* code golf.)

    @JonathanPullano Unless that function is `main`.

    Would `main=lambda:0/0` work?

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM