Small query related scope of the procedure
proc lappend {args} {
set a $args
lappend a testing ;# want to call the inbuilt tcl lappend command
puts "$a"
}
set list {new to tcl}
lappend $list
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you just do that, it won’t work. It’ll replace the standard
lappendand you’ll get an infinite recursion (well, you’ll hit a stack-depth check). There are several ways to get around this.Putting your code in a namespace
If your code is in a namespace, it will resolve the
lappendwithin that namespace first and only use the global namespace if that local search fails. You can use this like this:There are variations on that possible,
namespace eval myNS {source example.tcl}(with your code being almost verbatim in that sourced file) being one if the more interesting ones as it allows the code to be mostly agnostic to the namespace.Renaming the global
lappendcommandYou can also move the standard command out of the way like this:
This technique works just fine, so long as you don’t have too much code warring over who has the actual original command. It’s been used by many Tcl scripts over the years.
Real problem’s solution: execution tracing
Of course, the
lappendcommand isn’t one that you really want to replace as it is heavily used in much Tcl library code. For the problem of figuring out where a piece of code is actually callinglappendand what arguments are being passed in, it’s far better to be using an execution trace. (The link there is to the Tcl 8.6 documentation, but this API has been in place since Tcl 8.4 so you should have it available.)