I have the following code
int redirect_test(ClientData pClientData, Tcl_Interp *pInterp, int argc, char *argv[])
{
std::string result_str = "This is the output from the method";
Tcl_SetObjResult(pInterp, Tcl_NewStringObj(result_str.c_str(), -1));
return true;
}
However when I attempt to use this method in the following way
% set m [ redirect_test]
This is the output from the method
% puts $m
can't read "m": no such variable
How can I return values from TCL functions?
The problem is your:
return true;
Tcl command implementations indicate success by returning the C constant TCL_OK
(= 0) and indicate errors with TCL_ERROR
(= 1). (There are other other defined result codes, but you're advised to not use them if you're uncertain what they mean.) The true
gets converted to 1
by the C++ bool
→int
cast operator, which is TCL_ERROR
, making the command fail (and your result string is then the error message).
The fix is trivial. Use this instead:
return TCL_OK;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With