Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

subst -nobackslashes

35 views
Skip to first unread message

Lawrence Woodman

unread,
May 25, 2018, 10:13:05 AM5/25/18
to
Hello,

I'm trying to understand the behaviour of subst -nobackslashes depending on
how a string is quoted. The first line below outputs "!!", while the second
returns a missing close-bracket error.

puts [subst -nobackslashes "!\[\]!"]
puts [subst -nobackslashes {B: \[\]}]

Any help to understand this would be most appreciated.

Thanks

Lawrence Woodman

Ralf Fassel

unread,
May 25, 2018, 10:30:56 AM5/25/18
to
* Lawrence Woodman <lorryw...@gmail.com>
| I'm trying to understand the behaviour of subst -nobackslashes depending on
| how a string is quoted. The first line below outputs "!!", while the second
| returns a missing close-bracket error.
>
| puts [subst -nobackslashes "!\[\]!"]

When using "" for quoting, the TCL parser "looks inside" the "" and
already does one round of substitution of whatever can be substituted.
So the \ never makes it to the 'subst' command:

puts "!\[\]!"
=> ![]!

This ![]! is what subst sees (note: no backslashes). The [] then is
replaced by subst by the empty string, since there was no command given
to call.

| puts [subst -nobackslashes {B: \[\]}]

When using {} for qouting, the enclosed text is used as-is, no
substition performed by the parser:

puts {B: \[\]}
=> B: \[\]

So subst 'sees' "B: \[\]" (with backslashes).

puts [subst -nobackslashes {B: \[\]}]
=> ERR missing close-bracket

I understand it like this:

Since subst was instructed to *not* handle backslashes, the first "\"
loses its special meaning, and the following "[" starts a command to call.

Now the parser jumps in again and handles the rest: the "\]" is handled
by the parser and since escaped, the "]" does *not* terminate the [.
But then the string ends and there is no corresponding ] found,
hence the error.

| Any help to understand this would be most appreciated.

HTH, and hopefully my interpretation is not wrong in terms of TCL
internals :-)
R'

Lawrence Woodman

unread,
May 26, 2018, 2:03:40 AM5/26/18
to
On Fri, 25 May 2018 16:30:52 +0200, Ralf Fassel wrote:

> * Lawrence Woodman <lorryw...@gmail.com>

>>
> | puts [subst -nobackslashes "!\[\]!"]
>
> When using "" for quoting, the TCL parser "looks inside" the "" and
> already does one round of substitution of whatever can be substituted.
> So the \ never makes it to the 'subst' command:
>
> puts "!\[\]!"
> => ![]!
>
> This ![]! is what subst sees (note: no backslashes). The [] then is
> replaced by subst by the empty string, since there was no command given
> to call.
>
> | puts [subst -nobackslashes {B: \[\]}]
>
> When using {} for qouting, the enclosed text is used as-is, no
> substition performed by the parser:
>
> puts {B: \[\]}
> => B: \[\]
>
> So subst 'sees' "B: \[\]" (with backslashes).
>
> puts [subst -nobackslashes {B: \[\]}]
> => ERR missing close-bracket
>
> Since subst was instructed to *not* handle backslashes, the first "\"
> loses its special meaning, and the following "[" starts a command to call.
>
> Now the parser jumps in again and handles the rest: the "\]" is handled
> by the parser and since escaped, the "]" does *not* terminate the [.
> But then the string ends and there is no corresponding ] found,
> hence the error.

Thanks Ralf that makes perfect sense now. I was looking at this and felt
that I could almost understand what was happening, but couldn't quite make
the final connection.

Best wishes


Lorry

--

http://lawrencewoodman.github.io
0 new messages