Re: After I overload "switch" command, how can I get back the original "switch" function back?
- From: "Donal K. Fellows" <donal.k.fellows@xxxxxxxxxxxxxxxx>
- Date: Tue, 26 Sep 2006 10:31:22 +0100
pheobe wrote:
I am planning to overlad "switch" command in my application, and use
it do something, yet after that, how can I get back the original
function of "switch" ? I don't want to use the overlad "switch" all
through my appliction.
You have to take action *before* you overload the [switch] command. To
do this, you use the [rename] command to change the original [switch] to
something else, before creating the replacement procedure. Once you've
finished with the overload, [rename] the saved real [switch] command
back to its normal name. Like this:
rename switch switch_savedForLaterByPheobe
proc switch args {
...
}
# do stuff with the custom [switch] here before restoring...
rename switch_savedForLaterByPheobe switch
If the code that is using your modified [switch] is in a namespace other
than the default global one, another technique is possible. Simply
create the modified [switch] inside that other namespace; it will then
overload inside the procedures in that namespace automatically without
affecting any commands elsewhere.
namespace eval someNs {
proc yourProcedure ... ...
proc switch args {
...
}
}
# Invoke command that uses the custom [switch]
someNs::yourProcedure ...
Note that you can cause the code in the namespace to use the standard
[switch] by just deleting or renaming the namespace's local switch
command, like this:
# To delete, do this:
rename someNs::switch {}
# To move out of the way, do this:
rename someNs::switch someNs::switch_outOfService
(Tcl 8.5 has some extra techniques that are more-powerful variants on
this theme, but the schemes already described will continue to work for
at least the foreseeable future.)
Donal.
.
- References:
- Prev by Date: Re: is tcl/tk dying out?!
- Next by Date: Re: is tcl/tk dying out?!
- Previous by thread: Re: After I overload "switch" command, how can I get back the original "switch" function back?
- Next by thread: Re: After I overload "switch" command, how can I get back the original "switch" function back?
- Index(es):
Relevant Pages
|