Re: Clean-up code for packages
- To: mathgroup at smc.vnet.net
- Subject: [mg74974] Re: Clean-up code for packages
- From: "Szabolcs" <szhorvat at gmail.com>
- Date: Fri, 13 Apr 2007 02:03:55 -0400 (EDT)
- References: <firstname.lastname@example.org><email@example.com>
On Apr 12, 11:08 am, Albert <awnl at arcor.net> wrote: > Hi, > > > Is there a way for packages to set up some clean-up code that is > > executed before Quit? (e.g. delete temporary files that may have > > been created by package functions) > > I don't know if there is a better way to do this than to manipulate > $Epilog... > > > I found $Epilog, but I cannot figure out how to prepend commands to it > > if it is already defined. E.g. the default value of $Epilog is > > $Epilog := <<end` > > and I'd like to modify it to be > > $Epilog := (someCommand;<<end`) > > without knowing the its initial value. > > > Is there a way to do this safely? Or is there any other way to run the > > clean-up code when the session is terminated? > > ... but this is one way to alter $Epilog, I hope it will be "save > enough" for your needs. You need to look up the OwnValues directly to > get ahold of the unevaluated code and then take some care to not > evaluate it too early: > > SetDelayed @@@ Cases[ > OwnValues[$Epilog], > HoldPattern[RuleDelayed[Verbatim[HoldPattern][$Epilog], origcode_]] :> > Hold[$Epilog, CompoundExpression[myCommand, origcode]] > ] > > (* test it: *) > myCommand := Print["Hello!"]; > Quit > > Of course there are other ways to extract the origcode, when testing you > might look at OwnValues[$Epilog] before and after evaluating the above > expression... > > hth, > > albert Thanks for your reply! The missing piece that I needed was OwnValues. I found DownValues, but OwnValues is missing from both the Built-in Functions section and the Master Index in the Help Browser.