Skip to content
Tcl Source Code Artifact [4bccad4d5f]
Login

Artifact [4bccad4d5f]

Artifact 4bccad4d5f5bfa3453dfba540caf98764e5a48c6b7a0b7910a76988c02becf5d:



CommandName: Tcl_Eval ManualSection: 3 Version: 8.1 TclPart: Tcl TclDescription: Tcl Library Procedures Keywords:

Name

Tcl_EvalObjEx, Tcl_EvalFile, Tcl_EvalObjv, Tcl_Eval, Tcl_EvalEx, Tcl_GlobalEval, Tcl_GlobalEvalObj, Tcl_VarEval - execute Tcl scripts

Synopsis

::: {.synopsis} ::: #include <tcl.h> [int]{.ret} [Tcl_EvalObjEx]{.lit} [interp, objPtr, flags]{.arg} [int]{.ret} [Tcl_EvalFile]{.lit} [interp, fileName]{.arg} [int]{.ret} [Tcl_EvalObjv]{.lit} [interp, objc, objv, flags]{.arg} [int]{.ret} [Tcl_Eval]{.lit} [interp, script]{.arg} [int]{.ret} [Tcl_EvalEx]{.lit} [interp, script, numBytes, flags]{.arg} [int]{.ret} [Tcl_GlobalEval]{.lit} [interp, script]{.arg} [int]{.ret} [Tcl_GlobalEvalObj]{.lit} [interp, objPtr]{.arg} [int]{.ret} [Tcl_VarEval]{.lit} [interp, part, part, ...= ยง(char *)NULL]{.arg} :::

Arguments

.AP Tcl_Interp *interp in Interpreter in which to execute the script. The interpreter's result is modified to hold the result or error message from the script. .AP Tcl_Obj *objPtr in A Tcl value containing the script to execute. .AP int flags in OR'ed combination of flag bits that specify additional options. fBTCL_EVAL_GLOBALfR and fBTCL_EVAL_DIRECTfR are currently supported. .AP "const char" *fileName in Name of a file containing a Tcl script. .AP Tcl_Size objc in The number of values in the array pointed to by fIobjvfR; this is also the number of words in the command. .AP Tcl_Obj **objv in Points to an array of pointers to values; each value holds the value of a single word in the command to execute. .AP int numBytes in The number of bytes in fIscriptfR, not including any null terminating character. If -1, then all characters up to the first null byte are used. .AP "const char" *script in Points to first byte of script to execute (null-terminated and UTF-8). .AP "const char" *part in String forming part of a Tcl script.

Description

The procedures described here are invoked to execute Tcl scripts in various forms. fBTcl_EvalObjExfR is the core procedure and is used by many of the others. It executes the commands in the script stored in fIobjPtrfR until either an error occurs or the end of the script is reached. If this is the first time fIobjPtrfR has been executed, its commands are compiled into bytecode instructions which are then executed. The bytecodes are saved in fIobjPtrfR so that the compilation step can be skipped if the value is evaluated again in the future.

The return value from fBTcl_EvalObjExfR (and all the other procedures described here) is a Tcl completion code with one of the values fBTCL_OKfR, fBTCL_ERRORfR, fBTCL_RETURNfR, fBTCL_BREAKfR, or fBTCL_CONTINUEfR, or possibly some other integer value originating in an extension. In addition, a result value or error message is left in fIinterpfR's result; it can be retrieved using fBTcl_GetObjResultfR.

Tcl_EvalFile reads the file given by fIfileNamefR and evaluates its contents as a Tcl script. It returns the same information as fBTcl_EvalObjExfR. If the file could not be read then a Tcl error is returned to describe why the file could not be read. The eofchar for files is "x1A"1 for all platforms. If you require a "^Z" in code for string comparison, you can use "x1A", which will be safely substituted by the Tcl interpreter into "^Z".

Tcl_EvalObjv executes a single preparsed command instead of a script. The fIobjcfR and fIobjvfR arguments contain the values of the words for the Tcl command, one word in each value in fIobjvfR. fBTcl_EvalObjvfR evaluates the command and returns a completion code and result just like fBTcl_EvalObjExfR. The caller of fBTcl_EvalObjvfR has to manage the reference count of the elements of fIobjvfR, insuring that the values are valid until fBTcl_EvalObjvfR returns.

Tcl_Eval is similar to fBTcl_EvalObjExfR except that the script to be executed is supplied as a string instead of a value and no compilation occurs. The string should be a proper UTF-8 string as converted by fBTcl_ExternalToUtfDStringfR or fBTcl_ExternalToUtffR when it is known to possibly contain upper ASCII characters whose possible combinations might be a UTF-8 special code. The string is parsed and executed directly (using fBTcl_EvalObjvfR) instead of compiling it and executing the bytecodes. In situations where it is known that the script will never be executed again, fBTcl_EvalfR may be faster than fBTcl_EvalObjExfR. fBTcl_EvalfR returns a completion code and result just like fBTcl_EvalObjExfR.

Tcl_EvalEx is an extended version of fBTcl_EvalfR that takes additional arguments fInumBytesfR and fIflagsfR.

Tcl_GlobalEval and fBTcl_GlobalEvalObjfR are older procedures that are now deprecated. They are similar to fBTcl_EvalExfR and fBTcl_EvalObjExfR except that the script is evaluated in the global namespace and its variable context consists of global variables only (it ignores any Tcl procedures that are active). These functions are equivalent to using the fBTCL_EVAL_GLOBALfR flag (see below).

Tcl_VarEval takes any number of string arguments of any length, concatenates them into a single string, then calls fBTcl_EvalfR to execute that string as a Tcl command. It returns the result of the command and also modifies the interpreter result in the same way as fBTcl_EvalfR. The last argument to fBTcl_VarEvalfR must be (char *)NULL to indicate the end of arguments.

Flag bits

Any OR'ed combination of the following values may be used for the fIflagsfR argument to procedures such as fBTcl_EvalObjExfR:

TCL_EVAL_DIRECT : This flag is only used by fBTcl_EvalObjExfR; it is ignored by other procedures. If this flag bit is set, the script is not compiled to bytecodes; instead it is executed directly as is done by fBTcl_EvalExfR. The fBTCL_EVAL_DIRECTfR flag is useful in situations where the contents of a value are going to change immediately, so the bytecodes will not be reused in a future execution. In this case, it is faster to execute the script directly.

TCL_EVAL_GLOBAL : If this flag is set, the script is evaluated in the global namespace instead of the current namespace and its variable context consists of global variables only (it ignores any Tcl procedures that are active).

Miscellaneous details

During the processing of a Tcl command it is legal to make nested calls to evaluate other commands (this is how procedures and some control structures are implemented). If a code other than fBTCL_OKfR is returned from a nested fBTcl_EvalObjExfR invocation, then the caller should normally return immediately, passing that same return code back to its caller, and so on until the top-level application is reached. A few commands, like fBforfR, will check for certain return codes, like fBTCL_BREAKfR and fBTCL_CONTINUEfR, and process them specially without returning.

Tcl_EvalObjEx keeps track of how many nested fBTcl_EvalObjExfR invocations are in progress for fIinterpfR. If a code of fBTCL_RETURNfR, fBTCL_BREAKfR, or fBTCL_CONTINUEfR is about to be returned from the topmost fBTcl_EvalObjExfR invocation for fIinterpfR, it converts the return code to fBTCL_ERRORfR and sets fIinterpfR's result to an error message indicating that the fBreturnfR, fBbreakfR, or fBcontinuefR command was invoked in an inappropriate place. This means that top-level applications should never see a return code from fBTcl_EvalObjExfR other than fBTCL_OKfR or fBTCL_ERRORfR.

Reference count management

Tcl_EvalObjEx and fBTcl_GlobalEvalObjfR both increment and decrement the reference count of their fIobjPtrfR argument; you must not pass them any value with a reference count of zero. They also manipulate the interpreter result; you must not count on the interpreter result to hold the reference count of any value over these calls.

Tcl_EvalObjv may increment and decrement the reference count of any value passed via its fIobjvfR argument; you must not pass any value with a reference count of zero. This function also manipulates the interpreter result; you must not count on the interpreter result to hold the reference count of any value over this call.


  1. ^ Z