home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / tcl / tcl-faq-usage.gz / tcl-faq-usage
Text File  |  1994-10-01  |  37KB  |  1,071 lines

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