if { [ check_for_dbl_click $click_line $click_text ] } {
set fnd_dbl_clk 1
[ transform_line $click_line ]
}
causes Tcl to complain : invalid command name "". Those are empty
quotes. What does this mean? Before I put in the set line, I could
write the test and command on one line and the Tcl parser would be
happy. I've tried rewriting this line a few times to try to remove any
extraneous spaces, but I can't seem to satisfy it. It complains
spefically about the [ transform_line $click_line ] statement. What am
I missing?
Thanks,
Jason Mazzotta
Thanks,
Jason Mazzotta
You'd typically use square brackets when you'd want to capture the
result of a command and do something with it later on or in and if
statement (like you see in your example) to test the results of a
command.
set result [transform_line $click_line]
if { $result != "" } {
puts "I didn't expect anything but instead I got \"$result\""
}
The Tcl man page has a decent description of this.
http://www.tcl.tk/man/tcl8.4/TclCmd/Tcl.htm#M6
http://www.tcl.tk/man/tcl8.4/TclCmd/Tcl.htm#M10
-Ahran
If you're familiar with C, imagine that you have two functions, "double"
and "triple". Each takes an integer and returns an integer. Assuming
you had a variable "num" of type integer, you could do:
num = double(triple(6));
In Tcl, this would be:
set num [double [triple 6]]
(Note that "set", like "double" and "triple" is a command--but it's not
in brackets because it's the outermost command, i.e. not nested.)
When the parser hits an expression in square brackets, it evaluates
what's inside the brackets and substitutes the result into the script as
a single "word" or argument. In the case of your code, the result was
an empty string, which Tcl tried to evaluate as a command because it was
the first non-whitespace word on the line.
Here are some more examples of appropriate use of square brackets:
# example 1
set answer [expr {5 + 5}]
# example 2
proc num {} {
return 5
}
set answer [expr {[num] + 5}]
# example 3
set words [list some words to be sorted]
set first_word [lindex [lsort $words] 0]
It looks like Ahran has pointed you to the "endekalogue"--the Tcl man
page. Depending on whether you're using version 8.5 of Tcl or a
previous version, Tcl has only 12 or 11 syntax rules, of which command
substitution is one. Since there are so few rules and since each one is
quite important, it's a good idea to familiarize yourself with them.
Wrapping your mind around the simplicity (and rigidity) of Tcl's syntax
can be tricky if you're used to languages like C, where the parser does
a lot more work. Once you're used to it, though, you will love being
able to look at a program and know exactly how the interpreter is going
to parse it.
Regards,
Aric
By the way, it is possible to define a Tcl procedure whose name is the
empty string. Probably not a good idea, but if you had had one in your
program, Tcl would have executed it as the result of [ transform_line
$click_line ].
proc "" {} {
puts "this is the empty string proc"
}
From an interactive Tcl shell:
% ""
this is the empty string proc
% {}
this is the empty string proc
My guess is that transform_line returns an empty string instead of a valid
command name.
In Tcl the first word on a "line" is the command, in this case the first
word is the value returned by transform_line (square brackets roughly mean
evaluate what is in side of the square brackets as a command and its
arguments and substitute the return value of the command in place of the
square brackets and its contents).
Thus in the above if the value of click_line is foo and the "line" is: [
transform_line $click_line ]
The parser sees the square brackets, pauses execution and evaluates
transform_line $click_line
it sees the the $click_line, substitiutes its value, thus giving
transform_line foo
it then call transform_line with one argument, foo. Let us say it returns
the null string. The parser will then resume the suspended execution os
the original line, substituing in "" for square brackets and its contents.
Thus it attempts to evaluate:
""
From the error message, it would appear you do not have a procedure whose
name is the null string.
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester | "The man who fights for his ideals is |
| Gerald...@cox.net | the man who is alive." -- Cervantes |
+--------------------------------+---------------------------------------+
proc testproc {} {
return "This is a test"
}
proc squareproc {} {
return "testproc"
}
[ squareproc ]
The function squareproc in my example just return the string testproc.
Note that it is just a string and squareproc does nothing special.
The interpreter will execute the first word of a line. Since squareproc
returns the word testproc then testproc becomes the first word on the
line.
> By the way, it is possible to define a Tcl procedure whose name is the empty
> string. Probably not a good idea,
I use this idiom with tdom fairly regularly. It allows me to write
something like:
[[$node selectNodes $xpath] someMethod]
where $xpath is an xpath expression that might return 0 or 1 nodes. To
avoid having to explicitly deal with the case where no nodes would be
returned I define:
proc "" args {}
Michael
Jason Mazzotta