For Programmers: Free Programming Magazines  


Home > Archive > Tcl > February 2005 > Re: Strange [switch] thing









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 Re: Strange [switch] thing
Andreas Leitgeb

2005-02-28, 8:58 am

At writing this comment, I've already read that Googie's problem
is already solved. Anyway, for the records:

Don Porter <dgp@email.nist.gov> wrote:
> Googie wrote:
> Look again at the 3rd argument you passed to [switch]. It's a
> brace-quoted word. Review your basic Tcl substitution rules.
> Brace-quoting inhibits all forms of substitution, including
> variable substitution.


While the basic Tcl substitution rules clearly say, that
stuff inside braces is passed unsubstituted to the command,
they say nothing about what "switch" is doing with the still
verbatim "$VAR1" it finds. It happens not to interpret it,
but that's not deducable from the basic Tcl substitution rules.


The practical "syntax" of list-body-switch is actually undefined.
There is *no* documentation that defines how a string is parsed
to a list. Someone new to Tcl might well be surprised that double
quotes are significant to list-parsing, whereas brackets are not:
switch -- $var {
"two words" {...} (happens to conform to evaluation rules)
$VAR {...} (just doesn't do what's likely expected)
[command args] {...} (really screws it up)
}


> Some working alternatives include:
> # Alternate multiple argument form of [switch]
> switch -- $val $VAR1 {
> puts VAR1
> } ...

This is the *ONLY* correct alternative

For the other two alternatives consider
this advice: Don't do this at home, kids!!!

> # Replace {} quoting with "" quoting
> switch -- $val "
> $VAR1 {
> puts VAR1
> } ...

This is generally a very *BAD* idea, since it will also
cause embedded $'s and ['s in the respective case-bodies
to be evaluated unexpectedly or at least pre-maturely.
Also it will cause problems with embedded double-quoted
strings, unless all nested double-quotes are backslash-
protected.

> # Use [subst] to force substitution
> switch -- $val [subst {
> $VAR1 {
> puts VAR1
> } ...

This is just as *BAD* as the previous version. (only the
nested-double-quoted-strings issue is "solved" here)
Even, if -nocommands option is added to [subst],
stuff like that still breaks:
switch -- $val [subst {
$VAR1 { set x "hello world"; puts $x } ... }]
because at time of subst'ing, x is not yet defined (and
even if it is, then things likely get even worse).

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com