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

Ordering in script

7 views
Skip to first unread message

jh

unread,
Aug 17, 2009, 4:42:54 PM8/17/09
to

Trying to put together some simple scripts.

If I put procs at end I get a complaint that it is an invalid command.

I I put procs at the top it doesn't "see" the variable define later in
the script below.

What's the solution?

Thanks,

jh

dkf

unread,
Aug 17, 2009, 5:02:26 PM8/17/09
to
On Aug 17, 9:42 pm, jh <jvh75...@gmail.com> wrote:
> If I put procs at end I get a complaint that it is an invalid command.
>
> I I put procs at the top it doesn't "see" the variable define later in
> the script below.

It sounds to me like you're very confused. (I'm certainly confused by
your descriptions!)

If you put your procedures - just your procedures mind you! - in a
separate file (or two) and load it with the [source] command before
you use any of them, you'll have much more understandable code. If
you're getting complaints about not seeing the variable then, either
you've forgotten to put in [global] definitions into your procedures,
or you're using "these" quotes to create a procedure and not {these};
don't do that (well, unless you're absolutely certain what you're
doing with on-the-fly code generation) as it will make your head ache.
Braced procedure definitions are much simpler to get right.

If these comments don't answer your question somehow, we'll need more
info to help...

Donal.

Hai Vu

unread,
Aug 17, 2009, 5:18:52 PM8/17/09
to
Please read Donal's comment and if you are still confused: by default,
procs don't see global variables at all. You have to explicity declare
them as global variable, or address as such:

proc doSomething {} {
# Method 1: declare global variable
global myGlobalVar1
puts "myGlobalVar1 = $myGlobalVar1"

# Method 2: address it using "::"
puts "myGlobalVar1 = $::myGlobalVar1"
}

As for order, you will need to declare your procedures before you use
them. That often means putting the procedures on top.
Hai

Don Porter

unread,
Aug 17, 2009, 5:29:28 PM8/17/09
to

Reverse the direction of causality.

--
| Don Porter Mathematical and Computational Sciences Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

Gerald W. Lester

unread,
Aug 17, 2009, 5:47:15 PM8/17/09
to
If dkf's explanation does not lead to your problem being solved,

Please post a small example with a single two or three line procedure and a
single variable "defined" later along with the exact error message you are
getting.


--
+------------------------------------------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

Jeff Godfrey

unread,
Aug 17, 2009, 6:12:01 PM8/17/09
to

In addition to the other responses, I'd add the following:

It's generally advisable to have the vast majority (if not *all*) of
your code in individual procs. There are a number of reasons for that,
including code reuse and run-time speed.

With that in mind, I (more or less) structure all of my Tcl code like this:

proc main {} {
}

proc otherProcA {} {
}

proc otherProcB {} {
}

main

So, in the above there's only *1* line that's not in a proc - the call
to "main" that's the very last line. By the time "main" is called, all
procs have already been "found" by the interpreter (as they are above
the call), so they can be used as needed.

Then, from within "main", I do whatever is necessary to initialize and
start my application.

In addition (as others have already mentioned), global variables are not
recognized within procs by default. They need to be declared as global
in the local proc (either by using "global" or explicitly referencing
them in the global namespace).

Jeff

dkf

unread,
Aug 17, 2009, 6:16:46 PM8/17/09
to
On 17 Aug, 22:29, Don Porter <d...@nist.gov> wrote:

> jh wrote:
> > What's the solution?
>
> Reverse the direction of causality.

But Egon says that crossing the streams is Bad.

Donal.

jh

unread,
Aug 18, 2009, 8:56:31 AM8/18/09
to

We11 sh:t. The previous sentence exemplifies my problem. What is the
third character of both words? In the first word it is the number 1,
in the 2nd it is a colon. Looks like it's egg on me for poor typing
AND not correctly using global. Variable declarations and data types -
one man's bliss, another's curse.

Confused, somewhat, sometimes, and not just about Tcl, head injuries
will do that for you. Didn't know one could confuse "The Donal" :)

Thanks.

jh

unread,
Aug 18, 2009, 9:02:56 AM8/18/09
to

Thanks, Jeff, a most excellent reply.

Read Ousterhout and Welch but somehow didn't pick up on this concept.

Cameron Laird

unread,
Aug 18, 2009, 10:56:24 AM8/18/09
to
In article <0c7e8e37-6a35-4585...@o6g2000yqj.googlegroups.com>,
jh <jvh7...@gmail.com> wrote:
.
.

.
>> With that in mind, I (more or less) structure all of my Tcl code like this:
>>
>> proc main {} {
>>
>> }
>>
>> proc otherProcA {} {
>>
>> }
>>
>> proc otherProcB {} {
>>
>> }
>>
>> main
>>
>> So, in the above there's only *1* line that's not in a proc - the call
>> to "main" that's the very last line. �By the time "main" is called, all
>> procs have already been "found" by the interpreter (as they are above
>> the call), so they can be used as needed.
>>
>> Then, from within "main", I do whatever is necessary to initialize and
>> start my application.
>>
>> In addition (as others have already mentioned), global variables are not
>> recognized within procs by default. �They need to be declared as global
>> in the local proc (either by using "global" or explicitly referencing
>> them in the global namespace).
>>
>> Jeff
>
>Thanks, Jeff, a most excellent reply.
>
>Read Ousterhout and Welch but somehow didn't pick up on this concept.
>

Use of [main] this way is a style that was very rare at the time
JO wrote his book. Brent and his teammates had other priorities
for their book.

Gerry Snyder

unread,
Aug 18, 2009, 11:52:40 AM8/18/09
to
jh wrote:
>
> Thanks, Jeff, a most excellent reply.
>
> Read Ousterhout....

Speaking of which, Amazon has the second edition available for
pre-order, at $42.70 including shipping, with an expected
ship date of September 14.

Ken Jones is listed as the second author. Wonder how the work was split
between them.


Gerry

dkf

unread,
Aug 19, 2009, 5:42:17 AM8/19/09
to
On 18 Aug, 16:52, Gerry Snyder <mesmerizer...@gmail.com> wrote:
> Speaking of which, Amazon has the second edition available for
> pre-order, at $42.70 including shipping, with an expected
> ship date of September 14.
>
> Ken Jones is listed as the second author. Wonder how the work was split
> between them.

Ken took the lead with the second edition; JO did not contribute any
new body text (I don't know if he wrote a foreword) but many of the
chapters are strongly based on his original text so he's still a big
contributor. His contribution just happens to be the first edition...

Donal.

Lan

unread,
Aug 20, 2009, 10:12:11 AM8/20/09
to

The best news. I must have it to sit beside my first edition (also my
first Tcl book) adding to my complete collection of Tcl/Tk literature.
Well, I _think_ it's complete.

tom.rmadilo

unread,
Aug 20, 2009, 11:10:18 PM8/20/09
to
On Aug 18, 8:52 am, Gerry Snyder <mesmerizer...@gmail.com> wrote:
> jh wrote:
>
> > Thanks, Jeff, a most excellent reply.
>
> > Read Ousterhout....
>
> Speaking of which, Amazon has the second edition available for
> pre-order, at $42.70 including shipping, with an expected
> ship date of September 14.

I was thinking that was expensive. Then I looked at my armrest/first
addition, which is about 10 years old and the price tag says $46.95.

dkf

unread,
Aug 21, 2009, 5:55:51 AM8/21/09
to
On 21 Aug, 04:10, "tom.rmadilo" <tom.rmad...@gmail.com> wrote:
> I was thinking that was expensive. Then I looked at my armrest/first
> addition, which is about 10 years old and the price tag says $46.95.

A quick, unscientific survey of my bookshelf indicates that most
computer books (that were worth getting) going back nearly 20 years
have been in the $35-$50 range, with the key determining factor
seemingly the size of the publishing run (more copies -> cheaper).
Alas, my newest ones are at home so I can't check them.

Donal.

David Bariod

unread,
Sep 14, 2009, 9:31:50 AM9/14/09
to

Trough my corporate e-library the book is now available.
I've not checked on Amazon.

David Bariod

careck

unread,
Sep 14, 2009, 3:35:29 PM9/14/09
to
On Sep 14, 11:31 pm, David Bariod <davidr...@googlemail.com> wrote:
> Trough my corporate e-library the book is now available.
> I've not checked on Amazon.
>
> David Bariod

I pre-ordered the book from Amazon and have been notified of the
shipment yesterday, alas it'll take a while until it will arrive in
Australia even with expedited post.

- Carsten.

marc spitzer

unread,
Sep 14, 2009, 4:19:42 PM9/14/09
to
On 2009-09-14, careck <car...@gmail.com> wrote:
> I pre-ordered the book from Amazon and have been notified of the
> shipment yesterday, alas it'll take a while until it will arrive in
> Australia even with expedited post.
>
> - Carsten.

Mine came in the mail from amazon today, looks good.

marc

--
ms4...@sdf.lonestar.org
SDF Public Access UNIX System - http://sdf.lonestar.org

0 new messages