Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

"There is hopeful symbolism in the fact that flags do not wave in a vacuum." -- Arthur C. Clarke


devel / comp.lang.tcl / Substituting variable

SubjectAuthor
* Substituting variableclt.to.davebr
`* Substituting variableCecil Westerhof
 +* Substituting variableet99
 |`* Substituting variablegreg
 | `- Substituting variableet99
 `* Substituting variableRolf Ade
  `- Substituting variableCecil Westerhof

1
Substituting variable

<9511698333128@dlp>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=12685&group=comp.lang.tcl#12685

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: clt.to.davebr@dfgh.net
Newsgroups: comp.lang.tcl
Subject: Substituting variable
Date: Thu, 26 Oct 23 15:12:08 GMT
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <9511698333128@dlp>
Injection-Info: dont-email.me; posting-host="53bbfbf93b57c4e779ce950a1bbac852";
logging-data="1768733"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KUslLoM5Z6SogfydnuL3+"
Cancel-Lock: sha1:6TWr3+lrFf6nQ6g1/0jPpzhGqOk=
 by: clt.to.davebr@dfgh.net - Thu, 26 Oct 2023 15:12 UTC

The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $ including the enclosing braces, and passes it to Tcl_GetVar2Ex as the variable name.

The Tcl interpreter uses Tcl_ParseVarName which checks if the character after the $ is a "{", and if it is takes everything between the matching braces as the variable name.

Dave B

Re: Substituting variable

<871qdhfiq8.fsf@munus.decebal.nl>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=12686&group=comp.lang.tcl#12686

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.tcl
Subject: Re: Substituting variable
Date: Thu, 26 Oct 2023 18:31:43 +0200
Organization: Decebal Computing
Lines: 39
Message-ID: <871qdhfiq8.fsf@munus.decebal.nl>
References: <9511698333128@dlp>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="a8a6a3348a36ae84d8a380c80d6127c3";
logging-data="1820389"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18TeiaY5VENO0LvE450U5WAU7oLQb/Gzyw="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:aHEZZrkc8e4L/4ZkyL8J9Gun7AU=
sha1:OiG8kvUpJKpylW3KVaHO1B9baxk=
 by: Cecil Westerhof - Thu, 26 Oct 2023 16:31 UTC

clt.to.davebr@dfgh.net writes:

> The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $
> including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
> variable name.
>
> The Tcl interpreter uses Tcl_ParseVarName which checks if the character
> after the $ is a "{", and if it is takes everything between the matching
> braces as the variable name.

This is concerning the thread I started?

I would find it logical if it would work this way. If at one place
$variable is the same as ${variable}, I would expect it everywhere to
be the case.
But sadly it is not.
Tho show it I use this code:
puts "Before first select"
db eval { SELECT 37 } {}
set testVal 37
puts "Before second select"
db eval { SELECT $testVal } {}
puts "Before third select"
db eval { SELECT ${testVal} } {}
puts "After selects"

When I run it I get:
Before first select
Before second select
Before third select
unrecognized token: "$"
while executing
"db eval { SELECT ${testVal} } {}"
(file "/home/cecil/bin/ytWeek.tcl" line 52)

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Substituting variable

<uhe7n5$1o9s4$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=12688&group=comp.lang.tcl#12688

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: et99@rocketship1.me (et99)
Newsgroups: comp.lang.tcl
Subject: Re: Substituting variable
Date: Thu, 26 Oct 2023 10:30:11 -0700
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <uhe7n5$1o9s4$1@dont-email.me>
References: <9511698333128@dlp> <871qdhfiq8.fsf@munus.decebal.nl>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 26 Oct 2023 17:30:13 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ea729792a4a17df4851bcd3301190d82";
logging-data="1845124"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pMFrAWKInoFPFgnE31p0D"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Cancel-Lock: sha1:jLRH1Puq3/TzqfE19YK46IQZHr4=
Content-Language: en-US
In-Reply-To: <871qdhfiq8.fsf@munus.decebal.nl>
 by: et99 - Thu, 26 Oct 2023 17:30 UTC

On 10/26/2023 9:31 AM, Cecil Westerhof wrote:
> clt.to.davebr@dfgh.net writes:
>
>> The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $
>> including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
>> variable name.
>>
>> The Tcl interpreter uses Tcl_ParseVarName which checks if the character
>> after the $ is a "{", and if it is takes everything between the matching
>> braces as the variable name.
>
> This is concerning the thread I started?
>
> I would find it logical if it would work this way. If at one place
> $variable is the same as ${variable}, I would expect it everywhere to
> be the case.
> But sadly it is not.
> Tho show it I use this code:
> puts "Before first select"
> db eval { SELECT 37 } {}
> set testVal 37
> puts "Before second select"
> db eval { SELECT $testVal } {}
> puts "Before third select"
> db eval { SELECT ${testVal} } {}
> puts "After selects"
>
> When I run it I get:
> Before first select
> Before second select
> Before third select
> unrecognized token: "$"
> while executing
> "db eval { SELECT ${testVal} } {}"
> (file "/home/cecil/bin/ytWeek.tcl" line 52)
>

If you enclose your sqlite3 statement inside of braces, then tcl doesn't parse anything inside the braces, except to keep a level count of braces so it can find the final closing brace (with \{ or \} not counted).

That's it, everything else is passed on to sqlite3 as is. See rule 6 of the 12. So, it's up to sqlite3 to deal with the $ however it chooses to. And it does not (appear) to support the ${var} format that tcl does. So, it's not tcl's fault, since sqlite3 has it's own language. It supports $, :, and @ however.

Since sqlite3 statements are generally encoded as tcl words that begin and end with {}'s, tcl $ substitution does not occur. To get ${var} handled the sqlite3 statement would need to be in double quotes (assuming it has some spaces) and then tcl would be doing the $ substitution before handing it off to sqlite3.

note for the curious:

Actually, it's Tcl_ParseVarName that handles $var, ${var}, $() or $ (as just text). The code has a comment block which explains it. It's kinda complex, since var can have namespace characters using :: and empty array names have explicit code to handle those cases.

However, the manual entry for Tcl_ParseVarName (still) doesn't mention all the possibilities, and only gives 2 examples, one of a scalar variable on one as an array. I don't know if that's a manual bug, or just a desire to not overburden the reader. The ${var} form is mentioned (only) in the tcl rule 8 of the 12 afaik.

The tcl function Tcl_GetVar2Ex is called with a pointer to the variable or array name already separated from any $.

Re: Substituting variable

<24b0c5f4-d95c-4b92-9e48-9112309e9574n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=12690&group=comp.lang.tcl#12690

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:a0c:bec6:0:b0:66c:fb8b:9ea0 with SMTP id f6-20020a0cbec6000000b0066cfb8b9ea0mr3332qvj.8.1698341989432;
Thu, 26 Oct 2023 10:39:49 -0700 (PDT)
X-Received: by 2002:a05:6870:818b:b0:1e1:3152:93fc with SMTP id
k11-20020a056870818b00b001e1315293fcmr134045oae.6.1698341989074; Thu, 26 Oct
2023 10:39:49 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Thu, 26 Oct 2023 10:39:48 -0700 (PDT)
In-Reply-To: <uhe7n5$1o9s4$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=185.250.212.59; posting-account=99ziWAoAAADLScDbI_CyfIcpa_gFZ0zh
NNTP-Posting-Host: 185.250.212.59
References: <9511698333128@dlp> <871qdhfiq8.fsf@munus.decebal.nl> <uhe7n5$1o9s4$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <24b0c5f4-d95c-4b92-9e48-9112309e9574n@googlegroups.com>
Subject: Re: Substituting variable
From: gregor.ebbing@googlemail.com (greg)
Injection-Date: Thu, 26 Oct 2023 17:39:49 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: greg - Thu, 26 Oct 2023 17:39 UTC

et99 schrieb am Donnerstag, 26. Oktober 2023 um 19:30:18 UTC+2:
> On 10/26/2023 9:31 AM, Cecil Westerhof wrote:
> > clt.to:
> >
> >> The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $
> >> including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
> >> variable name.
> >>
> >> The Tcl interpreter uses Tcl_ParseVarName which checks if the character
> >> after the $ is a "{", and if it is takes everything between the matching
> >> braces as the variable name.
> >
> > This is concerning the thread I started?
> >
> > I would find it logical if it would work this way. If at one place
> > $variable is the same as ${variable}, I would expect it everywhere to
> > be the case.
> > But sadly it is not.
> > Tho show it I use this code:
> > puts "Before first select"
> > db eval { SELECT 37 } {}
> > set testVal 37
> > puts "Before second select"
> > db eval { SELECT $testVal } {}
> > puts "Before third select"
> > db eval { SELECT ${testVal} } {}
> > puts "After selects"
> >
> > When I run it I get:
> > Before first select
> > Before second select
> > Before third select
> > unrecognized token: "$"
> > while executing
> > "db eval { SELECT ${testVal} } {}"
> > (file "/home/cecil/bin/ytWeek.tcl" line 52)
> >
> If you enclose your sqlite3 statement inside of braces, then tcl doesn't parse anything inside the braces, except to keep a level count of braces so it can find the final closing brace (with \{ or \} not counted).
>
> That's it, everything else is passed on to sqlite3 as is. See rule 6 of the 12. So, it's up to sqlite3 to deal with the $ however it chooses to. And it does not (appear) to support the ${var} format that tcl does. So, it's not tcl's fault, since sqlite3 has it's own language. It supports $, :, and @ however.
>

from
https://www.sqlite.org/draft/tokenreq.html

Variables are used as placeholders in SQL statements for constant values that are to be bound at start-time.
H40310: SQLite shall recognize as a VARIABLE token the a question-mark (u003f) followed by zero or more NUMERIC characters.
A "parameter name" is defined to be a sequence of one or more characters that consists of ALPHANUMERIC characters and/or dollar-signs (u0025) intermixed with pairs of colons (u003a) and optionally followed by any sequence of non-zero, non-WHITESPACE characters enclosed in parentheses (u0028 and u0029).

Re: Substituting variable

<uherud$1t4rq$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=12692&group=comp.lang.tcl#12692

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: et99@rocketship1.me (et99)
Newsgroups: comp.lang.tcl
Subject: Re: Substituting variable
Date: Thu, 26 Oct 2023 16:15:25 -0700
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <uherud$1t4rq$1@dont-email.me>
References: <9511698333128@dlp> <871qdhfiq8.fsf@munus.decebal.nl>
<uhe7n5$1o9s4$1@dont-email.me>
<24b0c5f4-d95c-4b92-9e48-9112309e9574n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 26 Oct 2023 23:15:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="18b4165b66d20af7c8661c29e496206e";
logging-data="2003834"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RKcI4hwiTImLSuWHb9pEh"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Cancel-Lock: sha1:VRNqr1Id8RvOpKe5PxxaCwpS6Bo=
In-Reply-To: <24b0c5f4-d95c-4b92-9e48-9112309e9574n@googlegroups.com>
Content-Language: en-US
 by: et99 - Thu, 26 Oct 2023 23:15 UTC

On 10/26/2023 10:39 AM, greg wrote:
> et99 schrieb am Donnerstag, 26. Oktober 2023 um 19:30:18 UTC+2:
>> On 10/26/2023 9:31 AM, Cecil Westerhof wrote:
>>> clt.to:
>>>
>>>> The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $
>>>> including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
>>>> variable name.
>>>>
>>>> The Tcl interpreter uses Tcl_ParseVarName which checks if the character
>>>> after the $ is a "{", and if it is takes everything between the matching
>>>> braces as the variable name.
>>>
>>> This is concerning the thread I started?
>>>
>>> I would find it logical if it would work this way. If at one place
>>> $variable is the same as ${variable}, I would expect it everywhere to
>>> be the case.
>>> But sadly it is not.
>>> Tho show it I use this code:
>>> puts "Before first select"
>>> db eval { SELECT 37 } {}
>>> set testVal 37
>>> puts "Before second select"
>>> db eval { SELECT $testVal } {}
>>> puts "Before third select"
>>> db eval { SELECT ${testVal} } {}
>>> puts "After selects"
>>>
>>> When I run it I get:
>>> Before first select
>>> Before second select
>>> Before third select
>>> unrecognized token: "$"
>>> while executing
>>> "db eval { SELECT ${testVal} } {}"
>>> (file "/home/cecil/bin/ytWeek.tcl" line 52)
>>>
>> If you enclose your sqlite3 statement inside of braces, then tcl doesn't parse anything inside the braces, except to keep a level count of braces so it can find the final closing brace (with \{ or \} not counted).
>>
>> That's it, everything else is passed on to sqlite3 as is. See rule 6 of the 12. So, it's up to sqlite3 to deal with the $ however it chooses to. And it does not (appear) to support the ${var} format that tcl does. So, it's not tcl's fault, since sqlite3 has it's own language. It supports $, :, and @ however.
>>
>
> from
> https://www.sqlite.org/draft/tokenreq.html
>
> Variables are used as placeholders in SQL statements for constant values that are to be bound at start-time.
> H40310: SQLite shall recognize as a VARIABLE token the a question-mark (u003f) followed by zero or more NUMERIC characters.
> A "parameter name" is defined to be a sequence of one or more characters that consists of ALPHANUMERIC characters and/or dollar-signs (u0025) intermixed with pairs of colons (u003a) and optionally followed by any sequence of non-zero, non-WHITESPACE characters enclosed in parentheses (u0028 and u0029).
>
>

Note that sqlite3 is not just a tcl extension. When one reads the extraordinary testing procedures used, it is clear that the developers are looking at use far beyond just tcl.

So sqlite3 doesn't support every language's idiosyncratic syntax such as tcl's ${var} form.

And this form was most likely intended (only) to extend the $ operator to variable name construction not normally found in most languages, such as punctuation characters and the invisible name ${}. It was to have $var as a shortcut for [set var].

And I am quite dismayed that there is actually special purpose code to handle invisible array variable names. And partly because of this, the jimtcl extension of $(expression) for [expr {expression}] will most likely be rejected for inclusion in tcl 9.0.

And $(expression) solves the long standing problem of bracing expressions, not to mention its more readable and writable form.

Re: Substituting variable

<878r7p7y51.fsf@pointsman.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=12693&group=comp.lang.tcl#12693

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rolf@pointsman.de (Rolf Ade)
Newsgroups: comp.lang.tcl
Subject: Re: Substituting variable
Date: Fri, 27 Oct 2023 01:38:18 +0200
Organization: Me
Lines: 65
Message-ID: <878r7p7y51.fsf@pointsman.de>
References: <9511698333128@dlp> <871qdhfiq8.fsf@munus.decebal.nl>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net tdydL/fEcOjJ0GtiJIz+NgDskbYrv0fw32BMRixBSAUUfJSmA=
Cancel-Lock: sha1:KbhL4FjWNu27qKIfEgsjAGhPGxo= sha1:sT+QrL+CuO3Ef/Kc2WSnbDUcO4s= sha256:Z9MIe4Ba1UJMZMWyZwe1rSOYW3kfKj+DPuolm2eIXuM=
User-Agent: Gnus/5.13 (Gnus v5.13)
 by: Rolf Ade - Thu, 26 Oct 2023 23:38 UTC

Cecil Westerhof <Cecil@decebal.nl> writes:
> clt.to.davebr@dfgh.net writes:
>
>> The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $
>> including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
>> variable name.
>>
>> The Tcl interpreter uses Tcl_ParseVarName which checks if the character
>> after the $ is a "{", and if it is takes everything between the matching
>> braces as the variable name.
>
> This is concerning the thread I started?

Yes. It explains why sqlite3 does not work in this respect as you
expect.

> I would find it logical if it would work this way. If at one place
> $variable is the same as ${variable}, I would expect it everywhere to
> be the case.
> But sadly it is not.

But it is.

In code evaluated by the Tcl interpreter (citing you) "$variable is the
same as ${variable}"

But the argument given to [db eval arg] isn't evaluated by the Tcl
interpreter. It is processed by the database command created by the
extension command sqlite3.

A database command created by sqlite3 expects in this case a SQL expression in
sqlite3 SQL dialect. Of course this outstandig excellent piece of
software provides a way to "inject" string literals and in a secure way.
But this commands expect the syntax $variable (or :variable) and see
${variable} as invalid syntax. That's OK. Every command is free to
interpret its arguments.

The "injection attack" - think not only about bad guys but also
about bad luck, it is a data driven bug - is a problem for every
embedded language. As Dave wrote, the Tcl API provides functions to help
extension writers so solve this (Tcl_ParseVarName, Tcl_ParseVar). I give
an example with tDOM below (the embedded query language here is XPath)
were in the embedded language $variable is the same as ${variable}.

That all said this remains to be a quirk more to your habit to prefer
${variable} over $variable.

rolf

package require tdom

set xml {
<doc>
<elem key="foo">foovalue</elem>
<elem key="bar">barvalue</elem>
</doc>
}

dom parse $xml doc
set thiskey bar
puts [$doc selectNodes {string(/doc/elem[@key=$thiskey])}]
puts [$doc selectNodes {string(/doc/elem[@key=${thiskey}])}]

Re: Substituting variable

<87ttqcdr18.fsf@munus.decebal.nl>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=12697&group=comp.lang.tcl#12697

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.tcl
Subject: Re: Substituting variable
Date: Fri, 27 Oct 2023 17:27:31 +0200
Organization: Decebal Computing
Lines: 22
Message-ID: <87ttqcdr18.fsf@munus.decebal.nl>
References: <9511698333128@dlp> <871qdhfiq8.fsf@munus.decebal.nl>
<878r7p7y51.fsf@pointsman.de>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="50f3ff635629b1f2edf99015ce38d90e";
logging-data="2464139"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FogEh2hAShhrbY6joufaLve6G34PmVXA="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:qo04gtFfcCHJnk95HUHd0lgjAkE=
sha1:8QdCpKAn9xqGKwf0MGxTlJxIEMk=
 by: Cecil Westerhof - Fri, 27 Oct 2023 15:27 UTC

Rolf Ade <rolf@pointsman.de> writes:

> Cecil Westerhof <Cecil@decebal.nl> writes:
>> clt.to.davebr@dfgh.net writes:
>>
>>> The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $
>>> including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
>>> variable name.
>>>
>>> The Tcl interpreter uses Tcl_ParseVarName which checks if the character
>>> after the $ is a "{", and if it is takes everything between the matching
>>> braces as the variable name.

English is not my first language. But the above gave me the idea that
also for SQLite $var and ${var} should work.

But I am going to use :var, so it is no biggy.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor