Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
curly bracers

Posted by TOM83 (TOM83), 27 June 2008
Hi

I am new to tcl and i really don't understand something

First when you use curly braces for grouping it doesn't do variable substitution : puts {hello $name} which will print
hello $name

But if a have a loop then it does variable substitution.

I don't understand this i've tried searching for an explenation for a newbie but i didn't find one.

Can someone please explain this to me?
\
Thank you

Posted by admin (Graham Ellis), 28 June 2008
Curly braces are a "deferred block" ... and what that means is that they are not interpretted straight away by the language, but are passed in to the command which may (or may not) choose to interpret their contents.

Thus in
set henry {The $man of boxing}
the set command does NOT interpret the block

but in
while {$k < 7} {incr k; puts $k}
the while command interprets the first block as if it is a parameter to the expr command, and in interprets the second block (if necessary, and as many times as it has to) as a deferred piece of code.

There's no easy way to remember which commands interpret blocks and which don't, except to say that Tcl usually does something sensible.


Posted by TOM83 (TOM83), 28 June 2008
Hello

Thank you for your reply.

But what are the steps lets say in your while example:

set k 0
while {$k < 7} {incr k; puts $k}

As i know the sintax is while test body . If test is true then it executes the body an then it re-evalutes test executes again body and it stops when test is false.

In the example above it has to evaluate the test first, so
while gets $k < 7 (it does not substitute k with 0) . So, how does it compare a string "$k" with 7 ? What is the output of this evaluation or exit status and why ? also when does it substitute k with 1 ( because you said incr k )
I mean what are the actual steps in this example in regard to substitution


Posted by admin (Graham Ellis), 28 June 2008
No, it does not evaluate either block first ... it passes both blocks in as parameters to the while command without evaluation.

Within the while command, it passes the first block (the condition) to expr which evaluates it in the scope of the calling code (using uplevel).   If the result returned is true, the second block is also evaluated in the scope of the calling code, again using uplevel ... and it then rechecks the condition, and so on until the condition evaluates as false.

Hopefully that's a bit clearer now ... it's something we go through in detail on our Tcl course if you want to get much deeper into this





This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho