home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk1.iso / answers / tcl-faq / usage < prev    next >
Encoding:
Text File  |  1994-10-08  |  37.2 KB  |  1,094 lines

  1. Newsgroups: comp.lang.tcl,comp.answers,news.answers
  2. Path: bloom-beacon.mit.edu!hookup!swrinde!cs.utexas.edu!convex!news.duke.edu!MathWorks.Com!yeshua.marcam.com!charnel.ecst.csuchico.edu!psgrain!qiclab!morton!joe
  3. From: joe@morton.rain.com (Joe Moss)
  4. Subject: FAQ: comp.lang.tcl Tcl Language Usage Questions And Answers
  5. Message-ID: <tcl-faq/usage-1-781003073@morton.rain.com>
  6. Followup-To: comp.lang.tcl
  7. Summary: Answers to common questions regarding the use of the 
  8.     Tcl language and building the Tcl interpreter
  9. Keywords: FAQ Tcl TclX
  10. Sender: joe@morton.rain.com (Joe Moss)
  11. Supersedes: <tcl-usage-QnA-1-778371626@morton.rain.com>
  12. Reply-To: tcl-faq@morton.rain.com (Tcl Q&A FAQ Maintainer)
  13. Organization: Morton & Associates
  14. Date: Sat, 1 Oct 1994 05:18:02 GMT
  15. Approved: news-answers-request@MIT.EDU
  16. Expires: Sat, 5 Nov 1994 04:17:53 GMT
  17. Lines: 1074
  18. Xref: bloom-beacon.mit.edu comp.lang.tcl:18730 comp.answers:7676 news.answers:26968
  19.  
  20. Posted-By: auto-faq 3.1.1.2
  21. Archive-name: tcl-faq/usage
  22. Posting-Frequency: monthly
  23.  
  24.  
  25.                    TCL LANGUAGE USAGE QUESTIONS AND ANSWERS
  26.                                        
  27. INTRODUCTION
  28.  
  29.    This FAQ file is intended to address some of the most commonly asked
  30.    questions regarding the Tcl programming language. It is hoped that by
  31.    addressing these here in this document, that the postings to
  32.    comp.lang.tcl will be of a less repetitive and more informative
  33.    nature. It is also hoped that via this and other available resources,
  34.    more and more people will be able to utilize Tcl and Tcl based
  35.    applications.
  36.    
  37.    This FAQ is posted monthly to comp.lang.tcl and news.answers and can
  38.    also be obtained from the Tcl archive at ftp.aud.alcatel.com
  39.    (198.64.191.10) in the /tcl/docs directory or via the World Wide Web
  40.    as http://route.psg.com/tcl.html.
  41.    
  42.    For further information regarding Tcl see the FAQ maintained by Larry
  43.    Virden (lvirden@cas.org). It contains pointers to other available
  44.    documentation and to many extensions and sample code. It too is
  45.    available from alcatel in the /tcl/docs directory (as the files
  46.    tcl-faq.part0[1-5].gz) or via the URL:
  47.    http://www.cis.ohio-state.edu/hypertext/faq/usenet/tcl-faq/top.html 
  48.    
  49.    Note that this FAQ deals with Tcl, not with Tk. For Tk Toolkit Q&As
  50.    see the posting by Thomas J. Accardo (tja@cpu.com). However, there is
  51.    some inevitable overlap. Some of the most commonly asked questions
  52.    about usage of the Tk widgets are due to mis-understandings of how the
  53.    Tcl interpreter works. These issues may still be addressed here. Also,
  54.    some examples of Tcl usage may involve the use of the Tk widgets.
  55.    
  56.    Questions and Answers about the use of non-Tk related extensions (such
  57.    as Extended Tcl and Expect, but not TkX, expectk, BLT, etc.) are also
  58.    appropriate here.
  59.    
  60.    The source of this document is now maintained in HTML. It is then run
  61.    through a couple of scripts (written in Tcl, of course) to
  62.    automagically add question numbers, generate the Index of Questions,
  63.    and add the From and Subject lines to the text version. The version
  64.    available via the WWW will be updated whenever there are changes,
  65.    while the ASCII text version will be generated and posted only once a
  66.    month.
  67.      _________________________________________________________________
  68.    
  69. Index of Questions:
  70.  
  71.      * Q1- Where I can find help getting Tcl to work on my machine?
  72.      * Q2- How can I create/use association lists or property lists?
  73.      * Q3- How can I generate a random number in Tcl?
  74.      * Q4- How can I call one proc with the multi parameter value
  75.        returned by another proc?
  76.      * Q5- How can I pass an array into a proc?
  77.      * Q6- How can I run an external command and read its output via a
  78.        pipe?
  79.      * Q7- How can I merge extended Tcl with wish, expect, etc.?
  80.      * Q8- How can I delete a procedure from within a script?
  81.      * Q9- How can I get parray to recognize an array created via upvar?
  82.      * Q10- How can I get more than 7 digits of double precision ?
  83.      * Q11- How can I grab the command line when a non-built-in call is
  84.        made?
  85.      * Q12- How can I get or set an environment variable?
  86.      * Q13- How can I use numbers with leading zeroes?
  87.      * Q14- How can I find the command line arguments to my application?
  88.      * Q15- How can I put comments in my script, e.g. in a case
  89.        statement?
  90.      * Q16- How can I input and output binary data?
  91.      * Q17- How can I trap signals, and other more Unix specific
  92.        functions?
  93.      * Q18- How can I get quoted strings to work the way I want?
  94.      * Q19- How can I share procedures between multiple tcl applications?
  95.      * Q20- How can I get items inserted into a list?
  96.      * Q21- How can I perform a non-blocking read on a file identifier?
  97.      * Q22- How can I read and write from a pipe without locking up?
  98.      * Q23- How can I find what version I am running?
  99.      * Q24- How do I create a stand alone program in case Tcl isn't
  100.        installed?
  101.      * Q25- How can I use variables to hold array names?
  102.      * Q26- How can I create shared Tcl/Tk libraries on AIX
  103.      * Q27- Has anyone gotten Tcl to compile under HP-UX?
  104.      * Q28- Has anyone gotten Tcl to compile under VMS?
  105.      * Q29- What does it take to get Tcl to compile under SCO Unix?
  106.      * Q30- Why do I get format and scan errors when I run tclTest?
  107.      * Q31- Why do I get lots of errors under Irix 4.0.1 when I run
  108.        tclTest?
  109.      * Q32- Does anyone else have problems with Tcl on a Cray?
  110.      * Q33- How do I install Tcl 7.1/Tk 3.4 on NeXTSTEP 3.1?
  111.      * Q34- Why can't I print the draft of the Tcl/Tk book?
  112.      * Q35- Why am I getting errors on my SGI Indigo workstation?
  113.      * Q36- How do I build expect on Solaris 2.3?
  114.      * Q37- How do I port Tcl and Tk to a Sequent?
  115.      * Q38- How do I get around the symbol table full msg in AU/X 3.0?
  116.      * Q39- How do I compile Tcl on MS-DOS, MacOS, or other non-UNIX
  117.        system?
  118.      * Q40- How do I get Tcl to compile on my QNX system?
  119.        
  120.      _________________________________________________________________
  121.    
  122. ------------------
  123.  
  124. From: TCL Q&A
  125. Subject: Q1- WHERE I CAN FIND HELP GETTING TCL TO WORK ON MY MACHINE?
  126.   
  127.    Look in the Tcl distribution for the file called porting.notes. This
  128.    will contain a collection of notes that various people have provided
  129.    about porting Tcl to various machines and operating systems. There is
  130.    also a file called README which should be read FIRST - before doing
  131.    anything else with the code (this should always be one's first step
  132.    with any package).
  133.    
  134.    There are also several questions and answers related to building the
  135.    interpreter on various platforms at the end of this document. 
  136.    
  137. ------------------
  138.  
  139. From: TCL Q&A
  140. Subject: Q2- HOW CAN I CREATE/USE ASSOCIATION LISTS OR PROPERTY LISTS?
  141.   
  142.    Use Extended Tcl arrays or keyed lists.
  143.    
  144.    For example, if you did a:
  145.  
  146.     keylset ttyFields ttyName tty1a
  147.     keylset ttyFields baudRate 57600
  148.     keylset ttyFields parity strip
  149.  
  150.    And then an echo $ttyFields, you'd get:
  151.  
  152.     {ttyName tty1a} {baudRate 57600} {parity strip}
  153.  
  154. ------------------
  155.  
  156. From: TCL Q&A
  157. Subject: Q3- HOW CAN I GENERATE A RANDOM NUMBER IN TCL?
  158.   
  159.    Extended Tcl includes a command named random to do this. For example,
  160.    to generate a random number between 0 and 9 inclusive:
  161.  
  162.     set random_number [random 10]
  163.  
  164.    It also has a srandom command to seed the random number generator.
  165.    
  166.    Several pseudo-random number generator functions, written in straight
  167.    Tcl, have been posted to comp.lang.tcl and are available from alcatel.
  168.    
  169.    To get a value you can use as a seed, you might try one (or a
  170.    combination) of these:
  171.  
  172.         [pid]
  173.         [file atime /dev/kmem]
  174.         [getclock]              (Extended Tcl only)
  175.  
  176. ------------------
  177.  
  178. From: TCL Q&A
  179. Subject: Q4- HOW CAN I CALL ONE PROC WITH THE MULTI PARAMETER VALUE RETURNED
  180.   BY ANOTHER PROC?
  181.   
  182.    Assuming y requires multiple args and x returns multiple words, use
  183.    Tcl's eval command :
  184.  
  185.     eval y [x]
  186.  
  187. ------------------
  188.  
  189. From: TCL Q&A
  190. Subject: Q5- HOW CAN I PASS AN ARRAY INTO A PROC?
  191.   
  192.    Use upvar rather than try to use global variables when possible. If
  193.    the function is event driven, you are forced to use global variables.
  194.  
  195.     # print elements of an array
  196.     proc show_array arrayName {
  197.         upvar $arrayName myArray
  198.  
  199.         foreach element [array names myArray] {
  200.            puts stdout "${arrayName}($element) =  $myArray($element)"
  201.         }
  202.     }
  203.  
  204.     set arval(0) zero
  205.     set arval(1) one
  206.     show_array arval
  207.  
  208.    To return an array from a procedures, just take the array name in as
  209.    an argument, as above. Any changes you make in the array will be made
  210.    in the parent's array as well.
  211.    
  212.    Extended Tcl introduces a concept called keyed lists which are arrays
  213.    made out of lists of key-value pairs and can be passed by value to
  214.    routines, over networks, etc. 
  215.    
  216. ------------------
  217.  
  218. From: TCL Q&A
  219. Subject: Q6- HOW CAN I RUN AN EXTERNAL COMMAND AND READ ITS OUTPUT VIA A
  220.   PIPE?
  221.   
  222.    For example, to grep a pattern out of a range of files, one might do:
  223.    
  224.    Karl Lehenbauer (karl@NeoSoft.com) writes:
  225.  
  226.     set files [glob /home/cole/stats/*]
  227.  
  228.     proc parseInfo { site } {
  229.        global files
  230.  
  231.     #
  232.     # site is chosen from a listbox earlier
  233.     #
  234.        set in [open [concat "|/usr/bin/grep $site $files"] r]
  235.  
  236.        while {[gets $in line]>-1} {
  237.           puts stderr $line
  238.        }
  239.        catch {close $in}
  240.     }
  241.  
  242.    One thing: the matching strings are _not_ returned in directory order.
  243.    
  244.    But what if I want to check the return code AND use the output of the
  245.    command? Kevin B. Kenny (kennykb@dssv01.crd.ge.com) writes:
  246.  
  247.     if [catch {exec ls} data] {
  248.         # The exec got an error, and $errorCode has its termination status
  249.     } else {
  250.         # The exec succeeded
  251.     }
  252.     # In any case, `data' contains all the output from the child process.
  253.  
  254.    Note that Karl Lehenbauer (karl@NeoSoft.com) adds that errorCode will
  255.    be a list containing three elements, the string "CHILDSTATUS", the
  256.    process ID of the child, and the exit status of the child. 
  257.    
  258. ------------------
  259.  
  260. From: TCL Q&A
  261. Subject: Q7- HOW CAN I MERGE EXTENDED TCL WITH WISH, EXPECT, ETC.?
  262.   
  263.    Extended Tcl, as of version 6.7c, was enhanced to make it easier to
  264.    incorporate into applications. 
  265.    
  266. ------------------
  267.  
  268. From: TCL Q&A
  269. Subject: Q8- HOW CAN I DELETE A PROCEDURE FROM WITHIN A SCRIPT?
  270.   
  271.    Rename the procedure to have no name, for example:
  272.  
  273.     rename procedureName ""
  274.  
  275. ------------------
  276.  
  277. From: TCL Q&A
  278. Subject: Q9- HOW CAN I GET PARRAY TO RECOGNIZE AN ARRAY CREATED VIA UPVAR?
  279.   
  280.    Right now (June, 1992) upvar doesn't allow you to attach to an
  281.    individual element of an array. This is considered a bug by Mr.
  282.    Ousterhout and has been place on a bug list. 
  283.    
  284. ------------------
  285.  
  286. From: TCL Q&A
  287. Subject: Q10- HOW CAN I GET MORE THAN 7 DIGITS OF DOUBLE PRECISION ?
  288.   
  289.    In Tcl 6.x, modify the tclExpr.c module to use %lf instead of %g. In
  290.    Tcl 7.x, set the global variable tcl_precision instead. 
  291.    
  292. ------------------
  293.  
  294. From: TCL Q&A
  295. Subject: Q11- HOW CAN I GRAB THE COMMAND LINE WHEN A NON-BUILT-IN CALL IS
  296.   MADE?
  297.   
  298.    The procedure unknown is called automatically with arguments
  299.    containing the command and its arguments for any command that couldn't
  300.    be found. In fact, Tcl and Extended Tcl use this feature to provide
  301.    demand loaded commands, and even entire libraries.
  302.    
  303.    So by modifying the unknown procedure you can provide your own
  304.    extended functionality, or even remove the demand loading capability
  305.    if you so desire. 
  306.    
  307. ------------------
  308.  
  309. From: TCL Q&A
  310. Subject: Q12- HOW CAN I GET OR SET AN ENVIRONMENT VARIABLE?
  311.   
  312.    By using something like the following.
  313.  
  314.     set olddisplay $env(DISPLAY)
  315.     set env(DISPLAY) unix:0
  316.  
  317.    Thanks to Joel Fine (joel@cs.berkeley.edu) for the answer.
  318.    
  319.    You can also check if an environment variable exists, like this:
  320.  
  321.     if [info exists $env(VARNAME)] {
  322.             # okay, it's there use it
  323.             set value $end(VARNAME)
  324.     } else {
  325.             # the environment var isn't set, use a default
  326.             set value "the default value"
  327.     }
  328.  
  329. ------------------
  330.  
  331. From: TCL Q&A
  332. Subject: Q13- HOW CAN I USE NUMBERS WITH LEADING ZEROES?
  333.   
  334.    Dave Morriss was recently having problems because he was trying to do
  335.    something like:
  336.  
  337.     set index [expr [exec date +%W]%[llength $pop_server_list]]
  338.  
  339.    but during the 8th and 9th week of the year, he got errors - 08 and 09
  340.    are not valid octal numbers in Tcl.
  341.    
  342.    Some of the solutions provided were:
  343.    
  344.    From George A. Howlett (george.howlett@att.com), we got:
  345.  
  346.     set wknum [format "%g" [exec date +%W]]
  347.     set index [expr [exec $wknum%[llength $pop_server_list]]
  348.  
  349.    From Fred Feirtag (feirtag@wave.nrl.navy.mil):
  350.  
  351.     set index [expr (1[exec date +%W]-100)%[llength $pop_server_list]]
  352.  
  353.    From Dan R. Schenck (schendr@Texaco.com):
  354.  
  355.     set index [expr [string trimleft [exec date +%W] 0]%[llength \
  356.                      $pop_server_list]]
  357.  
  358.    Jay Goldberg jayg@accessware.com points out that while all of the
  359.    above work in this case (dealing with a date) they don't work in the
  360.    general case.
  361.    
  362.    Instead you can use the statement:
  363.  
  364.     regsub 0+(.+) orig_string \\1 new_string
  365.  
  366.    to strip off leading zeros. So, applying this to the example above:
  367.  
  368.     regsub 0+(.+) [exec date +%W] \\1 weeknum
  369.     set index [expr $weeknum%[llength $pop_server_list]]
  370.  
  371. ------------------
  372.  
  373. From: TCL Q&A
  374. Subject: Q14- HOW CAN I FIND THE COMMAND LINE ARGUMENTS TO MY APPLICATION?
  375.   
  376.    If you are using extended Tcl or Expect, you will find the parameters
  377.    in the Tcl variable argv as a list. Note that in extended Tcl, the
  378.    name of the program is in the Tcl variable programName and NOT in
  379.    argv[0]. Thanks to Brad Morrison (brad@NeoSoft.com) and Bud Bach
  380.    (bachww@rtsg.mot.com) for this answer. 
  381.    
  382. ------------------
  383.  
  384. From: TCL Q&A
  385. Subject: Q15- HOW CAN I PUT COMMENTS IN MY SCRIPT, E.G. IN A CASE STATEMENT?
  386.   
  387.    You can't have comments where you have them. Move the comments inside
  388.    of the { for the case that you want. Your code should read:
  389.  
  390.     case 1 {
  391.       -1    {
  392.             #
  393.             # Cannot find information sought
  394.             #
  395.             exit 2
  396.             }
  397.        0    {
  398.             #
  399.             # Error in arguments
  400.             #
  401.             exit 1
  402.             }
  403.        default    {
  404.             #
  405.             # Desired information found
  406.             #
  407.             exit 0
  408.             }
  409.     }
  410.  
  411.    Thanks to Gerald W. Lester (gwlester@cpu.com). 
  412.    
  413. ------------------
  414.  
  415. From: TCL Q&A
  416. Subject: Q16- HOW CAN I INPUT AND OUTPUT BINARY DATA?
  417.   
  418.    With Extended Tcl you can safely do stuff like:
  419.  
  420.     set infp [open "|compress -dc $fileName"]
  421.     set outfp [open "|gzip -c $newFileName" w]
  422.  
  423.     copyfile $infp $outfp
  424.  
  425.    Thanks to Karl Lehenbauer (karl@NeoSoft.com) for the code example.
  426.    
  427.    A method of handling binary data one character at a time using
  428.    standard Tcl can be found in:
  429.    ftp.aud.alcatel.com:/tcl/code/binary-io-hack.shar.gz 
  430.    
  431. ------------------
  432.  
  433. From: TCL Q&A
  434. Subject: Q17- HOW CAN I TRAP SIGNALS, AND OTHER MORE UNIX SPECIFIC
  435.   FUNCTIONS?
  436.   
  437.    Extended Tcl offers many of these types of functions. For instance,
  438.    extended Tcl has the signal command:
  439.    
  440.      signal action siglist [command]
  441.      
  442.      where action is one of "default", "ignore", "error", "trap", "get",
  443.      plus the POSIX "block" and "unblock" actions (available only on
  444.      POSIX systems, of course). Siglist is a list of either the symbolic
  445.      or numeric Unix signal (the SIG prefix is optional). Command is your
  446.      error handler (or a simple {puts stdout "Don't press *that* key!"}
  447.      :-)
  448.      
  449.    trap does what you expect, and I find error and get to be extremely
  450.    useful in interactive programs which demand keyboard traversal.
  451.    
  452.    Extended Tcl also has things like fork, etc.
  453.    
  454.    Answer by Brad Morrison (brad@NeoSoft.com). 
  455.    
  456. ------------------
  457.  
  458. From: TCL Q&A
  459. Subject: Q18- HOW CAN I GET QUOTED STRINGS TO WORK THE WAY I WANT?
  460.   
  461.    A long article dealing with the issues can be found at
  462.    ftp://ftp.aud.alcatel.com/tcl/docs/README.programmer.gz
  463.    
  464.    Here are some short answers:
  465.    
  466.    Q. I'm trying to build up a command for later execution but am having
  467.    trouble with variable values that include whitespace or special
  468.    characters.
  469.    
  470.    A. The safest way to build up commands is to use the list command so
  471.    that you can keep track of the list structure. Avoid using double
  472.    quotes because you can end up with an extra trip through the
  473.    evaluator. We'll illustrate this with a command to create a button
  474.    that prints out the label on the button when you click it.
  475.    
  476.      Wrong answer #1:
  477.  
  478.     button $myname -text $label -command "puts stdout $label"
  479.  
  480.      Why? because if $label has whitespace then the puts command will be
  481.      passed the wrong number of arguments. If $label has $ or [ ]
  482.      characters, they will be interpreted instead of printed.
  483.      Good answer #2:
  484.  
  485.     button $myname -text $label -command [list puts stdout $label]
  486.  
  487.      Why? because list will properly quote the value of $label
  488.      
  489.    Q. I'm trying to build up a command for later execution but am having
  490.    trouble getting some variables to evaluate now, and some to evaluate
  491.    later when the command is run.
  492.    
  493.    A. The cleanest way to do this is to define a procedure that hides the
  494.    use of the variables at run time, and then build up a call to that
  495.    procedure using the list command as described previously. (You can
  496.    even define the procedure on the fly. It will have global scope even
  497.    it if is created within another procedure.)
  498.    
  499.      Wrong answer #1:
  500.  
  501.     button $myname -text $label -command \
  502.         [list puts stdout $ArrayOfDynamicStuff($label)]
  503.  
  504.      Why? The array value will be substituted when the button is created,
  505.      not later on when the button is clicked. Also, note that the command
  506.      is executed at the global scope, so it is not necessary to include a
  507.      "global ArrayOfDynamicStuff" in the command.
  508.      Wrong answer #2 (backquotes and list):
  509.  
  510.     button $myname -text $label -command \
  511.         [list puts stdout \$ArrayOfDynamicStuff($label)]
  512.  
  513.      Why? Here the list command and the backquote of $ are fighting with
  514.      each other. The command ends up being something like:
  515.  
  516.     puts stdout {$ArrayOfDynamicStuff(foo)}
  517.  
  518.      which prevents the substitution of the value of the array element.
  519.      Dubious answer #3 (backquotes and double-quotes):
  520.  
  521.     button $myname -text $label -command \
  522.         "puts stdout \$ArrayOfDynamicStuff($label)"
  523.  
  524.      Why? This only works if the value of $label has no special
  525.      characters or whitespace.
  526.      Clean answer #4 (proc):
  527.  
  528.     proc doit { i } {
  529.         global ArrayOfDynamicStuff
  530.         puts stdout $ArrayOfDynamicStuff($i)
  531.     }
  532.     button $myname -text $label -command [list doit $label]
  533.  
  534.      Why? Using little TCL procs for your button commands is a good habit
  535.      because it eliminates most needs for fancy quoting, and it makes it
  536.      easier to tweak the button command later on.
  537.      
  538.    Q. I'm trying to pass along a variable number of args to another
  539.    procedure but I'm having trouble getting the $args to expand right.
  540.    
  541.    A. Avoid using eval and double quotes because that results in an extra
  542.    trip through the interpreter. The eval command will do a concat of its
  543.    arguments if there are more than one, so that pretty much eliminates
  544.    the need to group things with double quotes. Let's extend the button
  545.    example:
  546.    
  547.      Wrong answer #1:
  548.  
  549.     proc mybutton { myname label args } {
  550.         button $myname -text $label -command [list puts stdout $label] $args
  551.     }
  552.  
  553.      Why? All the extra arguments to mybutton are grouped into one list
  554.      element that is but into the value of $args. However, the button
  555.      command expects to see individual arguments, not a sub-list.
  556.      Wrong answer #2:
  557.  
  558.     proc mybutton { myname label args } {
  559.         eval "button $myname -text $label -command [list puts stdout $label] $a
  560. rgs"
  561.     }
  562.  
  563.      Why? The double quotes allow expansion of $label as well as $args,
  564.      so if $label has any whitespace, the button command will be
  565.      malformed
  566.      Good answer #3:
  567.  
  568.     proc mybutton { myname label args } {
  569.         set cmd {button $myname -text $label -command [list puts stdout $label]
  570. }
  571.         eval $cmd $args
  572.     }
  573.  
  574.      Why? Eval will first concatenate its two arguments and then run the
  575.      result through the interpreter. Think of this as stripping off the
  576.      outer curly braces from $cmd and $arg and making a single list with
  577.      all the elements of both. $label will be evaluated exactly once, so
  578.      the puts command will remain good, and whatever went into args will
  579.      also be processed exactly one time.
  580.      
  581.    Q. Why do I get a syntax error in an if/while/for statement?
  582.    
  583.    A. You may have written something like
  584.  
  585.     wish: set foo bar
  586.     wish: if {$foo == bar} {puts stdout bar}
  587.     syntax error in expression "$foo == bar"
  588.  
  589.    in which bar is interpreted as neither a string nor a variable, since
  590.    strings as operands in expressions MUST be surrounded by double quotes
  591.    or braces.
  592.    
  593.    Change to
  594.  
  595.     wish: if {$foo == "bar"} {puts stdout bar}
  596.  
  597.    or
  598.  
  599.     wish: if {$foo == {bar}} {puts stdout bar}
  600.  
  601.    always in expressions, depending on if you want expansion performed or
  602.    not.
  603.    
  604.    Contributed by Jesper Blommaskog (d9jesper@dtek.chalmers.se). 
  605.    
  606. ------------------
  607.  
  608. From: TCL Q&A
  609. Subject: Q19- HOW CAN I SHARE PROCEDURES BETWEEN MULTIPLE TCL APPLICATIONS?
  610.   
  611.    Rather than sourcing the files explicitly, build a tcl library:
  612.    
  613.    Step 1. Put the files in a common directory
  614.    
  615.    Step 2. Build the tclIndex for the "library". I use a Makefile with a
  616.    convention like:
  617.  
  618.     install.index:
  619.         (cd ${DESTDIR}/tclscripts/lib; \
  620.         echo 'source /usr/local/lib/tcl/init.tcl;\
  621.         auto_mkindex . *.tk' | tcl ; exit 0)
  622.  
  623.    Step 3. Modify your tcl scripts to reference the library:
  624.    
  625.    e.g.:
  626.  
  627.     # local additions
  628.     lappend auto_path /usr/local/lib/tcl_local $env(RDS_TCL_SCRIPTS)/lib
  629.  
  630.    Now, as soon as your script tried to reference a procedure in the
  631.    library, the "unknown" command autoloads the procedure for you.
  632.    
  633.    Contributed by Joe VanAndel (vanandel@ncar.ucar.edu). 
  634.    
  635. ------------------
  636.  
  637. From: TCL Q&A
  638. Subject: Q20- HOW CAN I GET ITEMS INSERTED INTO A LIST?
  639.   
  640.    A user asked why
  641.  
  642.     linsert $list 0 ..
  643.  
  644.    did not result in .. being inserted into list.
  645.    
  646.    Jesper Blommaskog (d9jesper@dtek.chalmers.se) replied:
  647.    
  648.    When doing list operations other than lappend, you must save the
  649.    returned value. This applies to list, lindex, lrange, and lreplace at
  650.    least.
  651.    
  652.    In this example, you would perhaps want to do something like:
  653.  
  654.     set list [ linsert $list 0 .. ]
  655.  
  656. ------------------
  657.  
  658. From: TCL Q&A
  659. Subject: Q21- HOW CAN I PERFORM A NON-BLOCKING READ ON A FILE IDENTIFIER?
  660.   
  661.    From Frank Smith (frank@arraysystems.nstn.ns.ca) we are told that if
  662.    you have Extended Tcl, you can
  663.  
  664.     read $fileId [fstat $fileId size]
  665.  
  666.    This will read only the number of bytes currently available on fileId
  667.    and consequently will not block. 
  668.    
  669. ------------------
  670.  
  671. From: TCL Q&A
  672. Subject: Q22- HOW CAN I READ AND WRITE FROM A PIPE WITHOUT LOCKING UP?
  673.   
  674.    The stdio package has an optimization for speed that buffers
  675.    characters so that you can avoid un-necessary system calls. This isn't
  676.    very good for interactive use so stdio changes it's behaviour if the
  677.    file is a terminal. When you write to a pipe you are not writing to a
  678.    terminal and so your output is buffered, similarly when the program at
  679.    the other end of the pipe writes its response it is also buffered. The
  680.    end result is the programs at both end of the pipe stall waiting for
  681.    input. It is possible to force the buffers to be written using flush
  682.    in Tcl and fflush() in C, often though, you don't have control over
  683.    the program at the other end of the pipeline and in that case your
  684.    only solution is to use a pseudo-teletype (pty). Unfortunately this
  685.    isn't all that easy.
  686.    
  687.    While this isn't directly supported in Tcl the spawn command in expect
  688.    opens a pty and starts a command. 
  689.    
  690. ------------------
  691.  
  692. From: TCL Q&A
  693. Subject: Q23- HOW CAN I FIND WHAT VERSION I AM RUNNING?
  694.   
  695.    Well, there is no one answer to this. Tcl itself will tell you it's
  696.    version if you type:
  697.  
  698.     info tclversion
  699.  
  700.    On the other hand, to get the Tk version, you must use:
  701.  
  702.     puts $tk_version
  703.  
  704.    Other extensions use other means. 
  705.    
  706. ------------------
  707.  
  708. From: TCL Q&A
  709. Subject: Q24- HOW DO I CREATE A STAND ALONE PROGRAM IN CASE TCL ISN'T
  710.   INSTALLED?
  711.   
  712.    Earle Lowe (lowee@cpsc.ucalgary.ca) has replied that:
  713.    
  714.      If you want to create a stand-alone program that will run anywhere
  715.      regardless of the presence/absence of TCL/Tk, than you need to go
  716.      through some more work. Basically, this involves converting the TCL
  717.      init files to C strings, and then rather than calling Tcl_Init() and
  718.      Tk_Init(), you call Tcl_Eval() with the converted C strings as
  719.      arguments. I suggest getting the wish_compiler package by Alexei
  720.      Rodriguez (alexei@cis.ufl.edu) from
  721.      ftp://ftp.aud.alcatel.com/tcl/code/wish_compiler.shar.gz This
  722.      package contains a tcl2c converter, and instructions as to its use.
  723.      With some Makefile magic, you can use using TCL/Tk as intended (as
  724.      an interpreted language using some version of wish) and when the
  725.      code works, you can create a stand-alone compiled program.
  726.      
  727. ------------------
  728.  
  729. From: TCL Q&A
  730. Subject: Q25- HOW CAN I USE VARIABLES TO HOLD ARRAY NAMES?
  731.   
  732.    Eric Bleeker (ericbl@paramount.nikhefk.nikhef.nl) has written:
  733.    
  734.      You may have written something like:
  735.  
  736.     % set foo "bar baz"
  737.     bar baz
  738.     % foreach aap $foo {
  739.         set $aap(1) "something"
  740.     }
  741.     can't read "aap(1)": variable isn't array
  742.  
  743.      This means Tcl tries to substitute the array element aap(1) which
  744.      doesn't exist. To fix this use:
  745.  
  746.     % foreach aap $foo {
  747.         set [set aap](1) "something"
  748.     }
  749.  
  750.      In this case two arrays bar and baz are created.
  751.      
  752. ------------------
  753.  
  754. From: TCL Q&A
  755. Subject: Q26- HOW CAN I CREATE SHARED TCL/TK LIBRARIES ON AIX
  756.   
  757.    Dov Grobgeld (dov@menora.weizmann.ac.il) provided info on creating Tcl
  758.    and Tk shared libraries under AIX 3.1.5:
  759.    
  760.      For Tcl:
  761.  
  762.     cc -o tkshar.o *.o -bE:tclshar.exp -bM:SRE -berok -lX11 -lm
  763.     ar r libtclshr tclshar.o
  764.  
  765.      For Tk:
  766.  
  767.     cc -o tkshar.o *.o -bE:tkshar.exp -bM:SRE -berok -Ltcl -lX11 -lm -ltclshr
  768.     ar r libtkshr tkshar.o
  769.  
  770.      where tckshar.exp and tkshar.exp had lists of the external
  771.      functions.
  772.      
  773.      There are also problems with fonts on AIX and the IBM. A patch is
  774.      needed from IBM to fix the X server so that fonts are working
  775.      
  776. ------------------
  777.  
  778. From: TCL Q&A
  779. Subject: Q27- HAS ANYONE GOTTEN TCL TO COMPILE UNDER HP-UX?
  780.   
  781.    See the porting notes. Also, be aware that there have been some
  782.    reports of Configure assuming that you should use the compatibility
  783.    version of opendir() and that you have no dirent.h . This causes
  784.    glob-ing to fail, as well as TclX's readdir, etc.
  785.    
  786.    Changing tcl7.3 to remove the opendir compatibility file and removing
  787.    the -DNO_DIRENT, -DUSE_DIRENT2 fixed it all up. 
  788.    
  789. ------------------
  790.  
  791. From: TCL Q&A
  792. Subject: Q28- HAS ANYONE GOTTEN TCL TO COMPILE UNDER VMS?
  793.   
  794.    Information from John Kimball (jkimball@src.honeywell.com) on May 4,
  795.    1993 was that he had gotten Tcl 6.7 and Tk 3.2 ported to VMS 5.5. See
  796.    the catalog for the file information. 
  797.    
  798. ------------------
  799.  
  800. From: TCL Q&A
  801. Subject: Q29- WHAT DOES IT TAKE TO GET TCL TO COMPILE UNDER SCO UNIX?
  802.   
  803.    Add a "#undef select" to tkEvent.c, and remove the reference to
  804.    TK_EXCEPTION around line 460 of main.c.
  805.    
  806.    Tk uses its own scheme for allocating the border colors for its 3D
  807.    widgets, which causes problems when running TK on a system with
  808.    "PseudoColor" display class, and a 16-cell colormap.
  809.    
  810.    If you can't go to eight bitplanes, you can instead start the server
  811.    with a "-static" (Xsco) or "-analog" (Xsight) option, making the
  812.    display class become "StaticColor". This makes the entire colormap
  813.    read-only, and it will return the color that most closely maps to the
  814.    desired color as possible.
  815.    
  816.    This information is from Keith Amann (Keith_Amann@stortek.com). 
  817.    
  818. ------------------
  819.  
  820. From: TCL Q&A
  821. Subject: Q30- WHY DO I GET FORMAT AND SCAN ERRORS WHEN I RUN TCLTEST?
  822.   
  823.    That's a problem (scanf/printf) many systems seem to have. Don't worry
  824.    too much about it - just don't use these 'advanced' features. If
  825.    you're hacking C, you'll have the same problems.
  826.    
  827.    For instance, printf("%Ng, double_value) and strtod("+",&terminal)
  828.    produce incorrect results. 
  829.    
  830. ------------------
  831.  
  832. From: TCL Q&A
  833. Subject: Q31- WHY DO I GET LOTS OF ERRORS UNDER IRIX 4.0.1 WHEN I RUN
  834.   TCLTEST?
  835.   
  836.    There's a bug in the 4.0.1 optimizer that's fixed in 4.0.2. Compile
  837.    tclVar.c using -O0 (no optimization). 
  838.    
  839. ------------------
  840.  
  841. From: TCL Q&A
  842. Subject: Q32- DOES ANYONE ELSE HAVE PROBLEMS WITH TCL ON A CRAY?
  843.   
  844.    See the porting notes for a set of changes mentioned. Also, Booker C.
  845.    Bense (benseb@grumpy.sdsc.edu) reports that version 3.0.1.6 has some
  846.    real problems with char pointers, causing Tcl to crash. Using version
  847.    3.0.2.1, things are much better, except for a minor formatting problem
  848.    and serious problems with scan. 
  849.    
  850. ------------------
  851.  
  852. From: TCL Q&A
  853. Subject: Q33- HOW DO I INSTALL TCL 7.1/TK 3.4 ON NEXTSTEP 3.1?
  854.   
  855.    I have indicated in the past that this particular question seems to
  856.    generate controversy - I have replaced previous explanations with the
  857.    latest msg from someone who sounds authoritative...
  858.    
  859.    From: Robert Nicholson (robert@steffi.demon.co.uk)
  860.    
  861.      This is for TCL7.1 and TK3.4
  862.      
  863.      This is a variation on Thomas Funke's entry.
  864.      
  865.     Installing TCL7.1 on NeXT
  866.     
  867.      To install tcl7.1 on NeXTSTEP 3.1 you must:
  868.      * run configure with predefined CPP:
  869.        type "sh" to run a Bourne shell. then type
  870.  
  871.     CPP='cc -E' ./configure
  872.      * edit Makefile:
  873.        add tmpnam.o to COMPAT_OBJS:
  874.  
  875.   COMPAT_OBJS =      getcwd.o waitpid.o strtod.o tmpnam.o
  876.  
  877.      At this point you probably want to rename the all calls to strtod
  878.      and tmpnam to something else in order to distinguish them from those
  879.      that are supplied in the NeXT libraries libsys_a.a. You can do this
  880.      by adding the following line to AC_FLAGS
  881.  
  882.     -Dstrtod=newstrtod -Dtmpnam=newtmpnam
  883.  
  884.      and renaming the names of the definitions in the appropriate
  885.      compat/*.c files.
  886.      
  887.      To check everything is working correctly open a tclsh
  888.  
  889.     % expr {"0" == "+"}
  890.     0
  891.  
  892.      Using the wrong strtod function will cause this test to return 1
  893.      which is a serious error. Ignore the precision errors.
  894.      
  895.     Installing TK3.4 on NEXTSTEP 3.1
  896.     
  897.      There is a call to strtod in TK3.4 so you should link against
  898.      ../tcl-7.1/compat/strtod.o and apply the -Dstrtod=newstrtod to
  899.      AC_FLAGS in Tk also.
  900.      
  901.      Note:Tk's raise test will fail when running the tvtwm window
  902.      manager. Changing to either twm or even better fvwm ensures that
  903.      this test will succeed.
  904.      
  905. ------------------
  906.  
  907. From: TCL Q&A
  908. Subject: Q34- WHY CAN'T I PRINT THE DRAFT OF THE TCL/TK BOOK?
  909.   
  910.    From John Ousterhout (ouster@cs.Berkeley.edu):
  911.    
  912.    Here's my form letter that seems to explain most of the problems
  913.    people have had printing parts of the book:
  914.    
  915.    Some old versions of the Transcript spooler software cannot properly
  916.    handle encapsulated Postscript files within another Postscript file
  917.    when they do page reversal. They tend to garble the page structure of
  918.    the file, causing an error at the point of the first EPS file. Part II
  919.    of the Tcl book has lots of EPS files embedded in it, one for each
  920.    screen dump. If your printer cannot print this file I suggest checking
  921.    to see if page reversal is enabled for your printer. If so, try asking
  922.    your local system wizard to disable it for you; this should allow the
  923.    file to print. Or, find some other way to dump the Postscript file
  924.    directly to the printer without going through the spooling software
  925.    (e.g. perhaps you can simply cat it to the printer's serial port).
  926.    
  927.    Others have suggested:
  928.    
  929.    The embedded pictures have CR as the line separator instead of LF and
  930.    this may be causing the problem. Try translating them to LFs ...
  931.  
  932.     tr '\015' '\012' <book.p2.ps >fixedbook.p2.ps
  933.  
  934.    and:
  935.    
  936.    This is not the original poster's problem but in countries using A4
  937.    paper and on a particular printer, the Dataproducts LZR1260E, the
  938.    frame size causes the printing to be stretched vertically. This
  939.    happens with other Framemaker generated documents by the way. A PS
  940.    interpreter bug no doubt. The fix is to edit the dimensions for A4
  941.    paper. This shell script does both fixes.
  942.  
  943.     #!/bin/sh
  944.     cat $1 | tr '\015' '\012' | sed '/FMDOCUMENT$/s/612 792/595 842/'
  945.  
  946. ------------------
  947.  
  948. From: TCL Q&A
  949. Subject: Q35- WHY AM I GETTING ERRORS ON MY SGI INDIGO WORKSTATION?
  950.   
  951.    From Gordon Lack (gml4410@ggr.co.uk) we are told that the SGI c
  952.    compiler has some bugs with variable arguments.
  953.    
  954.    tclVar.c must be compiled with -O0 at IRIX C 4.0.1 because of a
  955.    compiler bug with varargs.
  956.    
  957.    Done by placing specific rule into Makefile.
  958.  
  959.     # GGR SG needs -O0 for varargs at 4.0.1
  960.  
  961.     CC_SWITCHES0 =  -O0 -I. -I${SRC_DIR} ${AC_FLAGS} ${MATH_FLAGS} \
  962.           ${GENERIC_FLAGS} ${PROTO_FLAGS} ${MEM_DEBUG_FLAGS} \
  963.           -DTCL_LIBRARY=\"${TCL_LIBRARY}\"
  964.  
  965.     tclVar.o: tclVar.c
  966.           $(CC) -c $(CC_SWITCHES0) $
  967.  
  968. Peter Neelin (neelin@pet.mni.mcgill.ca) also noted that:
  969.  
  970. I get tclX 7.3a to compile on an SGI (irix 4.0.5) with the following
  971. changes to the Config.mk file:
  972.  
  973.     71c71
  974.     < CFLAGS=-cckr -D__GNU_LIBRARY__
  975.     ---
  976.     > #CFLAGS=
  977.     106,107c106,107
  978.     < TCL_PLUS_BUILD=TCL_PLUS
  979.     < CCPLUS=g++
  980.     ---
  981.     > #TCL_PLUS_BUILD=TCL_PLUS
  982.     > CCPLUS=CC
  983.     191,193c191
  984.     < MAN_DIR_SEPARATOR=
  985.     <
  986.     < LIBOBJS=strftime.o
  987.     ---
  988.     > #MAN_DIR_SEPARATOR=.
  989.  
  990. The -D__GNU_LIBRARY solves the srandom problem and I think that the -cckr gets
  991. around the prototype error with waitpid (it's crude, but it works).
  992. I've forgotten why I needed the strftime.
  993.  
  994. If you want the version of Config.mk that worked for me, send me mail.
  995.  
  996. ------------------
  997.  
  998. From: TCL Q&A
  999. Subject: Q36- HOW DO I BUILD EXPECT ON SOLARIS 2.3?
  1000.  
  1001. From Jeff Abramson (jra@hrcms.jazz.att.com) we are told that,
  1002.  
  1003.      using either SunPro cc 2.0.1 or gcc 2.5.8 with no problems. For
  1004.      SunPro I do:
  1005.  
  1006.     CC=cc ./configure --prefix=directory_of_your_choice
  1007.     make CC=cc
  1008.  
  1009.      For gcc I do:
  1010.  
  1011.     CC="gcc -fwritable-strings" ./configure \
  1012.         --prefix=directory_of_your_choice
  1013.     make CC="gcc -fwritable-strings"
  1014.  
  1015. ------------------
  1016.  
  1017. From: TCL Q&A
  1018. Subject: Q37- HOW DO I PORT TCL AND TK TO A SEQUENT?
  1019.  
  1020. From Andrew Swan (aswan@soda.berkeley.edu) we find:
  1021.  
  1022.      For both Tcl and Tk, I used gcc rather than the Sequent cc which is
  1023.      not ansi.
  1024.      
  1025.      For Tcl, there were a couple of problems with the math library.
  1026.      First, the Sequent math library doesn't include the 'fmod' function.
  1027.      I got the source for fmod from ftp.uu.net, put it in the compat
  1028.      subdirectory, and added it to the Makefile. fmod also wanted
  1029.      functions 'isnan' and 'finite' but I just commented those out since
  1030.      I don't believe tcl supports infinity and nan.
  1031.      
  1032.      The other problem with the math library was that it has two copies
  1033.      of the 'tanh' function in it. This is easy to fix by using "ar" to
  1034.      separate the library in to object files and then reconstruct it,
  1035.      leaving one of the copies of tanh out.
  1036.      
  1037.      With those changes, Tcl compiled cleanly and passed all the tests
  1038.      except the scanning tests. Apparently, the *scanf functions are
  1039.      broken in Dynix. The problem is with recognizing the end of octal
  1040.      numbers, and I just let this problem go. Fixing it would probably
  1041.      involve finding source to the *scanf functions and including it.
  1042.      
  1043.      To compile Tk, I had to make a new version of stddef.h including
  1044.      things like wchar_t. I just copied it in to another include
  1045.      directory and put that directory first and then tweaked the copy. Tk
  1046.      also had the math library problem (tanh). Other than those, however,
  1047.      everything compiled cleanly and passed all the tests.
  1048.      
  1049. ------------------
  1050.  
  1051. From: TCL Q&A
  1052. Subject: Q38- HOW DO I GET AROUND THE SYMBOL TABLE FULL MSG IN AU/X 3.0?
  1053.  
  1054. After getting things compiled on AU/X, you have to be sure
  1055. to do the links with the -A {factor} arguments which expand the default
  1056. table allocations.  {factor} is the amount by which the compiler multiplies
  1057. the default allocation.  Try -A 2.  Contact
  1058. Walter B. Kulecz, PhD. (wkulecz@medics.jsc.nasa.gov) for more details
  1059. of his port.
  1060.  
  1061. ------------------
  1062.  
  1063. From: TCL Q&A
  1064. Subject: Q39- HOW DO I COMPILE TCL ON MS-DOS, MACOS, OR OTHER NON-UNIX
  1065.   SYSTEM?
  1066.  
  1067. Refer to Larry Virden's FAQ (see tcl-faq/part1) or
  1068. (see tcl-faq/part4) for details concerning tools, finished
  1069. ports, or mailing lists relating to this topic.
  1070.  
  1071. ------------------
  1072.  
  1073. From: TCL Q&A
  1074. Subject: Q40- HOW DO I GET TCL TO COMPILE ON MY QNX SYSTEM?
  1075.  
  1076. Steve Furr (furr@qnx.com) indicates:
  1077.  
  1078. For the record, to get TCL to work under QNX, I had to:
  1079.  
  1080.      * Make the _POSIX_SOURCE change indicated for pathname lengths
  1081.      * Change a definition of VARARGS, so the Watcom ANSI C compiler
  1082.        would accept varargs functions used.
  1083.      * Resolve a conflict with error codes defined by QNX.
  1084.    
  1085.   __________________________________________________________________________
  1086.  
  1087. End of FAQ
  1088.  
  1089. ---
  1090. Joe V. Moss         | Hm: joe@italia.rain.com  Wk: joe@morton.rain.com
  1091. Morton & Associates |--------------------------------------------------
  1092. 7478 S.W. Coho Ct.  | label  .l -text "Insert cute quote here"
  1093. Tualatin, OR 97062  | button .b -text "OK" -command exit; pack .l .b
  1094.