home *** CD-ROM | disk | FTP | other *** search
/ Danny Amor's Online Library / Danny Amor's Online Library - Volume 1.iso / html / faqs / faq / tcl-faq / usage < prev   
Encoding:
Text File  |  1995-07-25  |  36.0 KB  |  1,083 lines

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