What means this error?
command already exists in namespace "::"
Can you provide simple case when this error raises?
That's (probably) an error thrown by the [incr Tcl] core when you try to do something like defining an itcl class with the same name as an existing command that isn't already a class; the part you left out (in quotes, between “command” and “already”) was the name of the command that you were trying to override. For example (the subst is fairly arbitrary here):
% package require itcl
4.0b7
% itcl::class subst {}
command "subst" already exists in namespace "::"
No command that is a “standard” part of Tcl itself (and not an extension package or user code) generates an error that's remotely close.
As for how to fix… don't use the same name as an existing non-class. What this means in the context of your code though, I don't know. You don't reveal enough information for me to work out the deeper reason why you might be having this problem in the first place.
Related
I'm having trouble using any predefined environmental variable in my module files. For example a line like this setenv TEST ${HOSTNAME} throws an error, where ${HOSTNAME} is defined by the system as a global environmental variable. The error looks like: ERROR:102: Tcl command execution failed: set TEST ${HOSTNAME}. I have set environmental variables in the module file itself and tried using those and get the same kind of error. For example,
This does not work:
setenv DUMMY /location/to/some/file
setenv TEST ${DUMMY}
I get a similar error as above: ERROR:102: Tcl command execution failed: set TEST ${DUMMY}. However,
This works:
set DUMMY /location/to/some/file
setenv TEST ${DUMMY}
There are certain lines that I needed to use predefined global environmental variables, so the above command cannot be used.
How can one use a predefined environmental variable in module files?
The set command sets a Tcl variable. These are local to the module processing: they are not exported anywhere else. There are very few restrictions on what characters you can use in a Tcl variable name; about the only ones to really beware of are : and (, though there are a few others that can make things really awkward so it's still best to stick to alphanumerics (and _).
The setenv command (which is not a standard Tcl command, but rather something the module system adds) sets environment variables, and these are exported to subprocesses. They are also mapped as Tcl variables as elements of the global associative array, env, so you can do this
setenv DUMMY /location/to/some/file
setenv TEST $env(DUMMY)
(The restriction on ( that I mentioned up above is because this gets tangled up with the syntax for associative arrays. Element names can use any character at all. Environment variables probably ought to avoid ASCII NUL and = in their names…)
The implementation of setenv is probably something like this:
proc setenv {variable value} {
global env
set env($variable) $value
}
The mapping to env is bi-directional.
So I have been trying to find an answer for this for a bit and could not find the answer on the internet. I need to check to see if an environment variable exists. I thought I had the right code but it keeps returning false.
if { [info exists ::env(USER)] } {
RAT::LogMsg INFO "Found USER"
} else {
RAT::LogMsg INFO "Nope!"
}
Any ideas?
You might want to check what environment variables are actually set; I don't think that USER is one of the guaranteed ones.
RAT::LogMsg INFO "Got these env-vars: [lsort [array names ::env]]"
If puts stdout works in your environment, you can try doing:
parray ::env
(The parray command is a procedure that pretty-prints an array.)
To get the current username reliably, check out the tcl_platform array's user element. That array is generated internally by Tcl (well, with probes to relevant basic OS APIs) instead of by looking at environment variables, and that particular element is always present back at least as far as Tcl 8.4.
RAT::LogMsg INFO "Username is $::tcl_platform(user)"
I've just noticed that the documentation is wrong: it says that the user element comes from USER and/or LOGNAME environment variables. It doesn't, and doesn't in at least 8.5 and 8.6. (And it's definitely my mistake. I forgot to update the code when I fixed this. Ooops!)
You have the right code, test in tclsh:
% if {[info exists ::env(USER)]} {puts "found $::env(USER)"}
found strobel
%
The problem must be in your environment.
I am new for Tcl/Tk. I am using vtk with Tk command window for running vtk tcl/tk examples. Here is a code which include Tk expression as condition of if and I am not getting it.
if { [info command rtExMath] != "" } {
##Do something related VTK
}
I have explored info of Tk but there is a combination with keyword command and also no any good explanation I found for rtExMath.
Please explain me above.
The info commands command (info command is just an unambiguous prefix) returns a list of all commands, or a list of all commands that match the given glob pattern. In the case you're looking at, the glob pattern is actually going to be a string-equality check (and is even optimised to such internally); there's no glob metacharacters in it. The result of that is that [info command rtExMath] != "" is a condition that is true exactly when the command rtExMath exists.
Tcl itself does not define any command called rtExMath; I conclude that it must be part of some specialist extension or application. However, Googling makes me suspect that it is actually a somewhat-standard name for an instance of the vtkMath class in Vtk, but I don't really know for sure. (I'm guessing that the binding of that class to Tcl was done by SWIG…)
i want to write little helper functions that stores and loads the octave session.
function restoreSession(filename)
history -r strcat('./states/',filename,'.history');
load("-binary", strcat('./states/',filename,'.data'))
endfunction
function saveSession(filename)
history -w strcat('./states/',filename,'.history');
save("-binary", strcat('./states/',filename,'.data'))
endfunction
The save/load command works well.
My Problem is that the history command seems not to evaulate the argument.
it prodces the following error:
syntax error
>>> history -r strcat('./states/',filename,'.history');
^
I already tried to use a temporary var for the path but in this case it only interprets the variable name as filename and complains about the missing file.
Does anybody has an idea how to solve this?
Use history with the function syntax instead of a command.
history ("-r", strcat ("./states/", filename, ".history"));
All commands are actually functions. The command syntax (when you don't use parentheses) is available to all functions, it just happens that for some it looks more natural. When you omit the parentheses, all the arguments are interpreted as strings, even variable names. If you want to do something fancier, call them as functions.
Is there a convenient way to specify in a Tcl script to immediately exit in case any error happens? Anything similar to set -e in bash?
EDIT I'm using a software that implements Tcl as its scripting language. If for example I run the package parseSomeFile fname, if the file fname does't exist, it reports it but the script execution continues. Is there a way that I stop the script there?
It's usually not needed; a command fails by throwing an error which makes the script exit with an informative message if not caught (well, depending on the host program: that's tclsh's behavior). Still, if you need to really exit immediately, you can hurry things along by putting a trace on the global variable that collects error traces:
trace add variable ::errorInfo write {puts stderr $::errorInfo;exit 1;list}
(The list at the end just traps the trace arguments so that they get ignored.)
Doing this is not recommended. Existing Tcl code, including all packages you might be using, assumes that it can catch errors and do something to handle them.
In Tcl, if you run into an error, the script will exit immediately unless you catch it. That means you don't need to specify the like of set -e.
Update
Ideally, parseSomeFile should have return an error, but looks like it does not. If you have control over it, fix it to return an error:
proc parseSomeFile {filename} {
if {![file exists $filename]} {
return -code error "ERROR: $filename does not exists"
}
# Do the parsing
return 1
}
# Demo 1: parse existing file
parseSomeFile foo
# Demo 2: parse non-existing file
parseSomeFile bar
The second option is to check for file existence before calling parseSomeFile.