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
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.
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
Reverse the direction of causality.
--
| Don Porter Mathematical and Computational Sciences Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|
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|
+------------------------------------------------------------------------+
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
But Egon says that crossing the streams is Bad.
Donal.
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.
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.
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
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.
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.
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.
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.
Mine came in the mail from amazon today, looks good.
marc
--
ms4...@sdf.lonestar.org
SDF Public Access UNIX System - http://sdf.lonestar.org