Group: comp.lang.tcl


Subject: uplevel, upvar and the Tcl library
From: Robert Heller
Date: 12/8/2007 6:40:40 PM
At Sat, 8 Dec 2007 08:12:27 -0800 (PST) Alexandre Ferrieux <alexandre.ferrieux@gmail.com> wrote: > > On Dec 8, 4:33 pm, Nicolas Castagne <casta...@imag.fr> wrote: > > > > How can I make an uplevel from the handler by using the Tcl > > library ? The is a Tcl_UpVar function, but no Tcl_UpLevel... > > Are you sure you need one ? Going into your C command doesn't allocate > a new frame, contrarily to a Tcl [proc]. > So, Tcl_Eval() there does the same as [uplevel 1] in a proc. Right. And Tcl_GlobalEval() and Tcl_GlobalEvalObj() are the same as [uplevel #0] as does including TCL_EVAL_GLOBAL as one of the flags of the Tcl_Eval<mumble> functions that take a flags argument (Tcl_EvalObjEx(), Tcl_EvalObjv(), and Tcl_EvalEx()). This is in fact the direct C API to uplevel. In effect, all of the Tcl_Eval<mumble> functions provide most of the useful functionallity of a Tcl_UpLevel() function. The OP should read the man page for Tcl_Eval carefully. > > > From within my handler, calling > > Tcl_UpVar(interp, "1", ....) > > returns TCL_ERROR with an error message like : > > "bad level "1"" > > > > As well, calling > > Tcl_Eval(interp, "uplevel 1 {set test 1} ) > > returns TCL_ERROR with an error message like : > > "bad level "1"" > > > > Why ? > > Does that mean that the C handler of my command is executed at the global > > level? > > At the same level as the caller's. If you call your command from the > global level, "1" is indeed invalid. > > -Alex > -- Robert Heller -- Get the Deepwoods Software FireFox Toolbar! Deepwoods Software -- Linux Installation and Administration http://www.deepsoft.com/ -- Web Hosting, with CGI and Database heller@deepsoft.com -- Contract Programming: C/C++, Tcl/Tk