Version 9 of rename

Updated 2007-02-06 15:46:04

The documentation for the [rename] command, used to rename Tcl procs, is available at http://purl.org/tcl/home/man/tcl8.5/TclCmd/rename.htm .

The [rename] command is most often used for wrapping commands. In other words, the programmer uses [rename] to hide another command -- so that a wrapper can be placed around the hidden command to change its behavior. Examples of this important concept abound on the Wiki; among the clearest are: DGP: Be aware that if you rename a command into another

[Discuss examples. Explain errorInfo subtleties (refer to live examples). Explain "return [eval ::_$original_command \$first_argument $args]" idiom.]

RS Another frequent application for rename is to remove commands, by renaming them to the empty string:

 rename foo {}

DGP Be aware that if you [rename] a command into another namespace, its evaluation context will change to that namespace. HaO 2010-04-10: (discussion on CLT ): rename or delete from inside targeted proc is allowed:

  % 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

GDE What's the recommended way to test for the pre-existence of the "destination" argument proc to avoid an error at runtime? The best I could come up with is:

  if {![llength [info commands new_proc_name]]} {
    rename old_proc_name new_proc_name
  }

MJ - The way to avoid the error is to use [catch], this is in line with the view that you shouldn't check if you can do something, but just do it (and handle errors):

 if {[catch {rename old new}]} {
    # handle error because new already exists
 }   

Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command