how to run piece of code just before the exit of perl script

前端 未结 3 1815
旧巷少年郎
旧巷少年郎 2021-01-02 03:20

In my script, I need to load some info from disk file and during the run of script the info might be changed.To keep the consistence of the file in disk and it\'s in memory

3条回答
  •  孤城傲影
    2021-01-02 03:57

    I think you're looking for END block:

    END {
        # cleanup
    }
    

    An END code block is executed as late as possible, that is, after perl has finished running the program and just before the interpreter is being exited, even if it is exiting as a result of a die() function. (But not if it's morphing into another program via exec, or being blown out of the water by a signal--you have to trap that yourself (if you can).) You may have multiple END blocks within a file--they will execute in reverse order of definition; that is: last in, first out (LIFO). END blocks are not executed when you run perl with the -c switch, or if compilation fails.

提交回复
热议问题