Home > Archive > Tcl > December 2007 > Something confuse me. Could someone please help to interpret this
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Something confuse me. Could someone please help to interpret this
|
|
| jing_mingjun@126.com 2007-12-24, 4:26 am |
| Hi everybody,
Currently i am working with tcltest. But i can't understand this
command as below:
# Set workingDirectory to [pwd]. The default output directory for
# Tcl tests is the working directory. Whenever this value changes
# change to that directory.
variable workingDirectory
trace variable workingDirectory w \
[namespace code {cd $workingDirectory ;#}]
####################My Problem####################
What does " ;# " in the above command means ? I am sure it is not
just a commend, for when i delete the ;# it can not work then.
Could everyone help me and give me some idea?
Appreciate for your kindly reply !!
By the way my MSN id is :jimmywell@hotmai..com
| |
| billposer@alum.mit.edu 2007-12-24, 4:26 am |
| On Dec 23, 10:31 pm, jing_ming...@126.com wrote:
> Hi everybody,
>
> Currently i am working with tcltest. But i can't understand this
> command as below:
>
> # Set workingDirectory to [pwd]. The default output directory for
> # Tcl tests is the working directory. Whenever this value changes
> # change to that directory.
> variable workingDirectory
> trace variable workingDirectory w \
> [namespace code {cd $workingDirectory ;#}]
> ####################My Problem####################
> What does " ;# " in the above command means ? I am sure it is not
> just a commend, for when i delete the ;# it can not work then.
>
When a command associated with a trace is executed, the interpreter
appends
three arguments to the command: name1, name2, and op. Op is the
operation that triggered the trace, e.g. read, writer, or unset. If
the variable on which the trace is set is a scalar, name1 is the name
of the variable and name2 is an empty string. If it is an array, name1
is the name of the array and name2 is the array index.
In other words, the command actually executed when the trace is
triggered
will be:
cd $workingDirectory ;# workingDirectory "" w
which consists of the command "cd $workingDirectory" followed by a
comment.
The purpose of the ";#" above is to ignore the arguments added by the
interpreter. The "#" is the comment character. The ";" is needed to
terminate
the command since in Tcl the comment character is permitted only where
a
command is permitted.
When you omit the ";#", the command executed is:
cd $workingDirectory workingDirectory "" write
You've now got four arguments to "cd" rather than one.
What is probably a more common and nicer way to deal with this problem
is to put the code you want to execute into a proc and just name the
proc
when you assign the trace. Make the proc variadic by using "args" when
you define it, e.g.:
proc UpdateDirectory {args} {
cd [lindex $args 0]
}
trace add variable workingDirectory write UpdateDirectory
| |
| jing_mingjun@126.com 2007-12-24, 4:26 am |
| On Dec 24, 3:54=A0pm, billpo...@alum.mit.edu wrote:
> On Dec 23, 10:31 pm, jing_ming...@126.com wrote:
>
>
>
r[color=darkred]
nges[color=darkred]
is not[color=darkred]
>
> When a command associated with a trace is executed, the interpreter
> appends
> three arguments to the command: name1, name2, and op. Op is the
> operation that triggered the trace, e.g. read, writer, or unset. If
> the variable on which the trace is set is a scalar, name1 is the name
> of the variable and name2 is an empty string. If it is an array, name1
> is the name of the array and name2 is the array index.
>
> In other words, the command actually executed when the trace is
> triggered
> will be:
>
> cd $workingDirectory ;# workingDirectory "" w
>
> which consists of the command "cd $workingDirectory" followed by a
> comment.
>
> The purpose of the ";#" above is to ignore the arguments added by the
> interpreter. The "#" is the comment character. The ";" is needed to
> terminate
> the command since in Tcl the comment character is permitted only where
> a
> command is permitted.
>
> When you omit the ";#", the command executed is:
>
> cd $workingDirectory workingDirectory "" write
>
> You've now got four arguments to "cd" rather than one.
>
> What is probably a more common and nicer way to deal with this problem
> is to put the code you want to execute into a proc and just name the
> proc
> when you assign the trace. Make the proc variadic by using "args" when
> you define it, e.g.:
>
> proc UpdateDirectory {args} {
> =A0 =A0 =A0cd [lindex $args 0]
>
> }
>
> trace add variable workingDirectory write UpdateDirectory
Many Thanks.
I just know what it means.
But why the ;# does not affect "]}" ?
| |
| billposer@alum.mit.edu 2007-12-24, 4:26 am |
| On Dec 24, 12:10 am, jing_ming...@126.com wrote:
> But why the ;# does not affect "]}" ?
The key is that comments in Tcl do not work the way they do in most
other languages. In most languages, comments are processed first, then
the remainder is parsed. If Tcl worked that way, the "}]" would indeed
be commented out. However, in Tcl comments are parsed like everything
else. Eventually, if the interpreter finds a "#" as the first
character of the first word of a command, it interprets it as a
comment. The key here is that braces delimit words, so the entire
string "cd $workingDirectory ;#" is treated as a single word. Within
this word, the cross-hatch is not in initial position and so is not
treated as a comment character in the initial parse.
| |
|
|
| jing_mingjun@126.com 2007-12-24, 4:26 am |
| On Dec 24, 4:33=A0pm, billpo...@alum.mit.edu wrote:
> This is a good summary of the parsing process:http://tcl.tk:8000/man/tcl8.=
4/TclCmd/Tcl.htm#M9
Thanks again.
I can see it that way.
|
|
|
|
|