Wednesday, 11 September 2013

Tcl switch statement and -regexp quoting?

Tcl switch statement and -regexp quoting?

There must be an extra level of evaluation when using the Tcl switch
command. Here's an example session at the repl to show what I mean:
$ tclsh
% regexp {^\s*foo} " foo"
1
% regexp {^\\s*foo} " foo"
0
% switch -regexp " foo" {^\\s*foo {puts "match"}}
match
% switch -regexp " foo" {{^\s*foo} {puts "match"}}
match
...There needed to be an extra backslash added inside the first "switch"
version. This is consistent between 8.5.0 and 8.6.0 on Windows. Can
someone point me to the section of the manual where this and similar
instances of extra levels of unquoting are described? My first thought was
that the curly brackets in the first "switch" version would have protected
the backslash, but "switch" itself must be applying an extra level of
backslash susbtitution to the patterns. Unless I'm misunderstanding the
nuances of something else.

No comments:

Post a Comment