Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a way to un-source Tcl source files?

1,105 views
Skip to first unread message

Bibi

unread,
Jul 12, 2013, 12:44:22 PM7/12/13
to
Experts,

When a Tcl source file gets sourced the script is getting passed to the Tcl-interpreter.
As of my understanding the interpreter needs to have the code somewhere in memory and perform look-ups on procedure calls.

My application has many Tcl source files.
To save memory, and time for lookup of procedure code in the Tcl-interp
I'd like to un-source source files when they are not needed to be present at a given time.

Is there a way to tell Tcl to forget about the source code in a specific file?

source foo.tcl
unsource foo.tcl

Thanks
Bibi

Robert Heller

unread,
Jul 12, 2013, 1:15:29 PM7/12/13
to
Not really, but you could use namespaces. If the file foo.tcl wraps all of its
code in a namespace eval foo block, you can then later later delete the
namespace with namespace delete foo. That is:

foo.tcl would look like:

namespace eval foo {

proc ....
variable ...

namespace eval bar {
}

}


Alternitively, you can redefine proc, global, and set to record somewhere the
file used to when they were eval'd and then define a proc to go through all of
the things defined in a given file and doing rename <proc> {} and unset
<variable>, etc. Somewhat combersome.

>
> Thanks
> Bibi
>

--
Robert Heller -- 978-544-6933 / hel...@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments



Les Cargill

unread,
Jul 12, 2013, 1:31:18 PM7/12/13
to
If you have a complete list of all the procs and variables created by
the "source" command, then you can do ... something with that. Use of
namespaces may also help.

See "info commands" and "info globals", plus whatever yuou have to do to
do things like that in namespaces.

Assume we have a command we created called "listDiff" which returns the
"xor" of two lists - all elements that are in one or the other but not
both.

Then:

set Cmds [ info commands ]
set Glob [ info globals ]

source "somefile.tcl"

set dCmds [ listDiff [ info commands ] $Cmds ]
set dGlob [ listDiff [ info commands ] $Glob ]

Now you have two lists - one of globals to be deleted,
one of procs to be renamed to ""

You'd have to do something about renamed proc-s. And any state
of anything else changed by the source command would still
be changed.

--
Les Cargill

mango

unread,
Jul 12, 2013, 1:52:56 PM7/12/13
to
Perhaps you could tell us a bit more about what problem you are trying to solve. Proc look up is done, IIRC, by hashing so it is approximately constant in time. Memory can be saved, but it would seem better not to "source" code that isn't needed in the first place. Organizing programs into packages and using the "package require" command can minimize sourcing unnecessary code. I guess I don't understand what your concerns are in this case. Are you having specific performance problems?

Andrew Mangogna

David Gravereaux

unread,
Jul 13, 2013, 12:10:17 PM7/13/13
to
On 07/12/2013 09:44 AM, Bibi wrote:
> Is there a way to tell Tcl to forget about the source code in a specific file?


interp create container
interp eval container {
source myfoo.tcl
}
interp delete container


--


signature.asc
0 new messages