Package unloading

Package unloading

Post by Derek Foun » Thu, 31 Jan 2008 23:21:17


Surely someone has been here before, but Google isn't helping...

I have a package containing a file call foo.tcl, which contains a whole
bunch of Tcl procedures I'm working on. I start a tkcon, package require
mypackage, do my thing, then go back to my editor for further work on
foo.tcl. Then I run 'make install' to get the latest foo.tcl into place.

Then, I need to exit tkcon and restart it, and package require again to
get the latest Tcl code into the interpreter. Is there a way I can
unload foo.tcl and reload it from disk without restarting the interpreter?
 
 
 

Package unloading

Post by Bryan Oakl » Thu, 31 Jan 2008 23:31:35


Have you tried using "package forget"?


--
Bryan Oakley
http://www.yqcomputer.com/

 
 
 

Package unloading

Post by Derek Foun » Fri, 01 Feb 2008 00:35:19

> Have you tried using "package forget"?

Yes. That seems to convince the interpreter that the package is no
longer loaded, but doesn't actually delete the namespaces and procedure
definitions that it created. auto_path still contains the paths to the
files. All the procs in the package are still in the interpreter so they
still run.

I was hoping there was some easy way to back out all that stuff, or to
force the next "package require" command to go and reload the tcl file
from the disk. Is there an easy way to re-source the file without having
to type its full filename (which is this case is enormous)?
 
 
 

Package unloading

Post by ZB » Fri, 01 Feb 2008 00:49:54

Dnia 30.01.2008 Derek Fountain < XXXX@XXXXX.COM > napisaa:
>> Yes. That seems to convince the interpreter that the package is no >> longer loaded, but doesn't actually delete the namespaces and procedure >> definitions that it created.

It seems, it's working "pack -forget"-like - while you need something a'la
"destroy package"...
--
ZB
 
 
 

Package unloading

Post by suchenw » Fri, 01 Feb 2008 00:52:07


The simplest solution would be to just re-source foo.tcl after you've
edited it. If it just contains procs, I expect no problems - except
possibly if you delete a proc in a newer foo.tcl, it will still be
there in the interpreter.
 
 
 

Package unloading

Post by Bryan Oakl » Fri, 01 Feb 2008 00:54:52


When I find my self in this development mode (which is rare), I
typically create a "reload" command that does all the dirty work for me.
Are you aware of tkcon's "Revert State" menu item?

My normal way of work is to create a way to launch console from within
my app, rather than run my app from a console. That way, each time I run
my app I'm assured of a fresh start, but an interactive console is just
a keypress or mouse click away.

--
Bryan Oakley
http://www.yqcomputer.com/
 
 
 

Package unloading

Post by Donald G P » Fri, 01 Feb 2008 00:57:00


Your question is confusing.

What is the relationship between the file "foo.tcl" and the package
"mypackage" ?

[package forget mypackage] will reset the interp to no longer know it
contains the "mypackage" package and to also forget anything it knows
about what versions of "mypackage" are available. Then the next
[package require mypackage $requirement] will bring in the most suitable
version of mypackage available satisfying $requirement, with a fresh
call to [package unknown] to figure that out.

The exact effect of [package require]-ing a new version of "mypackage"
over the top of its [package forg<o>t]ten predecessor depends on the
details of how "mypackage" is written.

--
| Don Porter Mathematical and Computational Sciences Division |
| XXXX@XXXXX.COM Information Technology Laboratory |
| http://www.yqcomputer.com/ ~DPorter/ NIST |
|______________________________________________________________________|
 
 
 

Package unloading

Post by Derek Foun » Fri, 01 Feb 2008 01:11:06

> What is the relationship between the file "foo.tcl" and the package

foo.tcl is a file full of procs that make up the functionality of
mypackage. It's source'ed via autoload/auto_index when a proc in it is
needed.


Yes, I'm doing my package loading in what I thought was the "standard"
way, but maybe it's only standard to me because that's how I've always
done it.

I think adding something like Brian's 'reload' to my package loading is
going to be the best way.
 
 
 

Package unloading

Post by Donald G P » Fri, 01 Feb 2008 01:31:40


Ah. That's the key. Auto-loading is the feature of the problem making
this difficult.

I'd encourage you to examine just what it is you think auto-loading is
doing for you. Most of the justifications for it I can imagine have
passed from the scene. Simplest answer, at least long term, might be
"Stop Doing That".

If you do need or want to keep auto-loading, then you'll need to
actually delete commands in order to force the auto-loader to load
them over again. If your package follows recommended conventions, that
should be a simple matter of [namespace delete ::mypackage]. [*]


I would describe it as doing packages in the "Tcl 7 way" myself. It
works, but there's better choices now.


proc reload {p args} {
# auto_reset ;# Only if your edits invalidate a tclIndex file
catch {namespace delete ::$p} ;# safety if unconventional
package forget $p
package require $p {*}$args
}

[*] There is a third way. If you want to have auto-loading as a
feature, but don't want the burden of [namespace delete] during
iterative development, you can implement auto-loading for yourself with
stub-proc definitions of your commands. Not making use of [unknown] or
auto* at all.

--
| Don Porter Mathematical and Computational Sciences Division |
| XXXX@XXXXX.COM Information Technology Laboratory |
| http://www.yqcomputer.com/ ~DPorter/ NIST |
|______________________________________________________________________|
 
 
 

Package unloading

Post by Derek Foun » Fri, 01 Feb 2008 18:05:46

> I'd encourage you to examine just what it is you think auto-loading is

My dept has been working on an extended version of Tcl for several years
now. We have about 30 packages, written in a mix of Tcl and C code.
We'd need a better reason than my current reloading issue to rework the
loading mechanism of that lot!


That works, thanks. :)


Is there something I can read that tells me how to do it the modern way?
I've been out of the Tcl loop for a couple of years and have a lot of
catching up to do.