rename , a built-in routine, renames or removes a routine.


rename oldName newName


official reference


To remove a routine, rename it to the empty string:

rename foo {}

To rename a routine to the empty string instead of removing it:

rename foo ::

To rename a routine only if a routine by the new name doesn't exist:

if {[namespace which newname] eq {}} {
    rename oldname newname

When a routine is renamed into a another namespace, that namespace becomes its evaluation context:

% namespace eval one {
  variable message "I'm in one"
  proc test {} {variable message; return $message}
% namespace eval two {variable message "I'm in two"}
% rename one::test two::test
% two::test
I'm in two

aspect: Also watch out for where you're renaming routines to:

% proc foo args {}
% namespace eval ddd {namespace which -command foo}
% namespace eval ddd {rename foo foo}
% namespace eval ddd {namespace which -command foo}

HaO 2010-04-10: (discussion on CLT ): rename or delete from inside targeted proc is allowed:

proc ::runOnceAndDelete {} {
    # do something
    rename ::runOnceAndDelete ""

See Also

wrapping commands
One common use of rename is to replace a routine with another routine that calls the original routine, performing additional steps before or afterwards. Some examples of this are Overloading widgets, Text variable for text widget, and Forward compatibility.

Page Contributors

Lars H