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 copy I need to
write back the info to disk whenever the info is changed in memory or periodically write them
back to disk or just write them back at the time of the script exit, which is the preferred one, because it will save lots of IO time and make the script responsive.
So just as the title, my question is does perl has some mechanism that will meet my needs?
There’s two different ways to do this, depending on what you’re looking for.
ENDblock is executed when the interpreter is shut down. See the previous answer for more details 🙂DESTROYblock/sub, that is executed when your object goes out of scope. That is, if you want to embed your logic into a module or class, then you can useDESTROY.Take a look at the following example (it’s a working example, but some details like error checking, etc.. are omitted):