home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / tcl-faq / usage < prev   
Text File  |  1996-06-03  |  48KB  |  1,400 lines

  1. Newsgroups: comp.lang.tcl,comp.answers,news.answers
  2. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!paperboy.osf.org!bone.think.com!blanket.mitre.org!agate!howland.reston.ans.net!newsfeed.internetmci.com!sgigate.sgi.com!sdd.hp.com!hp-pcd!hp-cv!reuter.cse.ogi.edu!gasco!qiclab.scn.rain.com!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-833672178@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-faq/usage-1-830936969@morton.rain.com>
  12. Reply-To: tcl-faq@morton.rain.com (Tcl Q&A FAQ Maintainer)
  13. Organization: Morton & Associates
  14. Date: Sat, 1 Jun 1996 23:36:21 GMT
  15. Approved: news-answers-request@MIT.EDU
  16. Expires: Sat, 6 Jul 1996 23:36:18 GMT
  17. Lines: 1380
  18. Xref: senator-bedfellow.mit.edu comp.lang.tcl:48787 comp.answers:19082 news.answers:73445
  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.  
  28. INTRODUCTION
  29.  
  30.    This FAQ file is intended to address some of the most commonly asked
  31.    questions regarding the Tcl programming language. It is hoped that by
  32.    addressing these here in this document, that the postings to
  33.    comp.lang.tcl will be of a less repetitive and more informative nature.
  34.    It is also hoped that via this and other available resources, more and
  35.    more people will be able to utilize Tcl and Tcl based applications.
  36.  
  37.    This FAQ is maintained by Joe Moss (joe@morton.rain.com) and is posted
  38.    monthly to comp.lang.tcl and news.answers and can also be obtained from
  39.    the Tcl archive at ftp.neosoft.com/languages/tcl (198.64.191.10) in the
  40.    /languages/tcl/alcatel/docs directory or via the World Wide Web as
  41.    http://route.psg.com/tcl.html.
  42.  
  43.    Note that this FAQ deals with Tcl, not with Tk. For Tk Toolkit Q&As see
  44.    the FAQ posting by Thomas J. Accardo (tja@cpu.com) (see the question
  45.    below, entitled "Where can I get further info about Tcl" for pointers to
  46.    other Tcl FAQs). However, there is some inevitable overlap. Some of the
  47.    most commonly asked questions about usage of the Tk widgets are due to
  48.    misunderstandings of how the Tcl interpreter works. These issues may
  49.    still be addressed here. Also, some examples of Tcl usage may involve
  50.    the use of the Tk widgets.
  51.  
  52.    Questions and Answers about the use of non-Tk related extensions (such
  53.    as Extended Tcl and Expect, but not TkX, expectk, BLT, etc.) are also
  54.    appropriate here.
  55.  
  56.    The source of this document is maintained in HTML. It is then run
  57.    through a script (written in Tcl, of course) to automagically generate
  58.    the Index of Questions, number the questions, generate the URL list, and
  59.    add the From and Subject lines to the text version. The version
  60.    available via the WWW will be updated whenever there are changes, while
  61.    the ASCII text version will be generated and posted once a month.
  62.  
  63.    Last modified: Sat Jun 01 16:34:01 PDT 1996
  64.   ---------------------------------------------------------------------------
  65.  
  66. INDEX OF QUESTIONS:
  67.  
  68. SECTION A - GENERAL INFORMATION:
  69.      * Q.A1- Where can I get further info about Tcl?
  70.      * Q.A2- Where can I find help getting Tcl to work on my machine?
  71.      * Q.A3- How can I combine Tcl and C?
  72.      * Q.A4- How do I combine several extensions to provide all the
  73.              features I want?
  74.      * Q.A5- What about dynamic loading?
  75.      * Q.A6- How do I create a stand alone program in case Tcl isn't
  76.              installed?
  77.      * Q.A7- How do I make my script executable regardless of the location
  78.              of tclsh?
  79.      * Q.A8- Why isn't <some extension> part of the standard distribution?
  80.  
  81. SECTION B - PROGRAMMING RELATED QUESTIONS AND ANSWERS:
  82.      * Q.B1- How can I create/use association lists or property lists?
  83.      * Q.B2- How can I generate a random number in Tcl?
  84.      * Q.B3- How can I call one proc with the multi parameter value
  85.              returned by another proc?
  86.      * Q.B4- How can I pass an array into a proc?
  87.      * Q.B5- How can I run an external command and read its output via a
  88.              pipe?
  89.      * Q.B6- How can I delete a procedure from within a script?
  90.      * Q.B7- How can I get more than 7 digits of double precision?
  91.      * Q.B8- How can I grab the command line when a non-built-in call is
  92.              made?
  93.      * Q.B9- How can I get or set an environment variable?
  94.      * Q.B10- How can I use numbers with leading zeroes?
  95.      * Q.B11- How can I find the command line arguments to my application?
  96.      * Q.B12- I found a bug in the handling of comments!
  97.      * Q.B13- How can I input and output binary data?
  98.      * Q.B14- How can I trap signals, and perform other more Unix specific
  99.               functions?
  100.      * Q.B15- How can I do a double indirect? - Why doesn't $$var work?
  101.      * Q.B16- How can I get quoted strings to work the way I want?
  102.      * Q.B17- How can I share procedures between multiple tcl applications?
  103.      * Q.B18- How can I get items inserted into a list?
  104.      * Q.B19- How can I perform a non-blocking read on a file identifier?
  105.      * Q.B20- How can I read and write from a pipe without locking up?
  106.      * Q.B21- How can I find what version I am running?
  107.      * Q.B22- How can I use variables to hold array names?
  108.      * Q.B23- How can I make variables static, like in C?
  109.      * Q.B24- How can I convert between a character and its ASCII value?
  110.  
  111. SECTION C - EXPLANATION OF ERROR MESSAGES:
  112.      * Q.C1- "not found" or "Command not found"
  113.      * Q.C2- invalid command name "}"
  114.      * Q.C3- missing close-brace
  115.      * Q.C4- X server insecure (must use xauth-style authorization)
  116.      * Q.C5- expected integer but got ...
  117.      * Q.C6- "Undefined symbol: main" or similar
  118.  
  119. SECTION D - COMPILING AND OTHER PLATFORM SPECIFIC QUESTIONS:
  120.      * Q.D1- Why does "glob" cut off the first two characters of each
  121.              filename?
  122.      * Q.D2- How do I get Tcl working on MS Windows (3.1, 95, or NT)?
  123.      * Q.D3- How can I build the Tcl library as a shared libary?
  124.   ---------------------------------------------------------------------------
  125.  
  126. GENERAL INFORMATION:
  127.  
  128.  
  129. ------------------
  130.  
  131. From: Tcl Q&A
  132. Subject: Q.A1- Where can I get further info about Tcl?
  133.  
  134.    The newsgroup comp.lang.tcl is for discussion of the Tcl language and
  135.    packages based on it. There are several FAQ documents related to Tcl
  136.    available from ftp.neosoft.com in the /languages/tcl/alcatel/docs
  137.    directory that provide further information and pointers to yet more
  138.    reading material, both on-line and in print. They can also be accessed
  139.    via the World Wide Web: http://www.teraform.com/~lvirden/tcl-faq/
  140.  
  141.    Three other good starting points, if you are surfing the Web for
  142.    information, are:
  143.    http://www.sco.com/Technology/tcl/Tcl.html
  144.    http://web.cs.ualberta.ca/~wade/HyperTcl/
  145.    http://www.sunlabs.com/research/tcl/
  146.  
  147.    Of course, the source distribution itself includes a great deal of
  148.    documentation. The latest version is 7.5. It can be obtained from:
  149.    ftp://ftp.smli.com/pub/tcl/
  150.  
  151.  
  152. ------------------
  153.  
  154. From: Tcl Q&A
  155. Subject: Q.A2- Where can I find help getting Tcl to work on my machine?
  156.  
  157.    Look in the Tcl distribution for the file called "porting.notes". This
  158.    will contain a collection of notes that various people have provided
  159.    about porting Tcl to various machines and operating systems. There is
  160.    also a file called "README" which should be read *FIRST* - before doing
  161.    anything else with the code (this should always be one's first step with
  162.    any package).
  163.  
  164.    You should also check the on-line database of porting information
  165.    available via the Sun Tcl/Tk Web pages.
  166.  
  167.    There are also some questions and answers related to building the
  168.    interpreter on various platforms at the end of this document.
  169.  
  170.  
  171. ------------------
  172.  
  173. From: Tcl Q&A
  174. Subject: Q.A3- How can I combine Tcl and C?
  175.  
  176.    Tcl was intended to be used along with C code. Because of this, there
  177.    are several ways to combine the two. Here is a list of some of them
  178.    (with man page references for further info):
  179.      * Adding Commands to Tcl (Tcl_CreateCommand)
  180.      * Command Traces (Tcl_CreateTrace)
  181.      * Variable Traces (Tcl_TraceVar)
  182.      * Getting/Setting Variables (Tcl_GetVar/Tcl_SetVar)
  183.      * Linking Variables (Tcl_LinkVar)
  184.      * Asynchronous Event Handlers (Tcl_AsyncCreate)
  185.      * Adding Functions to "expr" Command (Tcl_CreateMathFunc)
  186.      * Custom "tclAppInit" Routine (Tcl_AppInit)
  187.      * Custom "main" Routine (Tcl_Main)
  188.      * Via Pipes (open)
  189.      * "exec" Command (exec)
  190.  
  191.    The last two do not provide nearly the same level of integration as
  192.    those near the top of the list, but they are necessary if you do not
  193.    have access to the source code.
  194.  
  195.    There is plenty of documentation available that explains this in further
  196.    detail. See the "TclCommandWriting" man page, included with Extended Tcl
  197.    or available via the WWW at http://psg.com/~joem/CmdWrite.html
  198.  
  199.    See also part two of Larry's Tcl FAQ and the Tcl Bibliography for other
  200.    references.
  201.  
  202.    In addition to all the ways possible with standard Tcl, several
  203.    extensions add others, notably via pseudo-ttys with expect, and via X
  204.    event, file event, timer, and idle callback handlers with Tk. Also check
  205.    out "Embedded Tk", available from ftp://ftp.vnet.net/pub/users/drh/
  206.  
  207.  
  208. ------------------
  209.  
  210. From: Tcl Q&A
  211. Subject: Q.A4- How do I combine several extensions to provide all the
  212.                features I want?
  213.  
  214.    The simple answer is to create a custom Tcl_AppInit function to call the
  215.    Init procedures for each of the desired extensions. This can be done by
  216.    copying the tclAppInit.c (tkAppInit.c for Tk apps) file from Tcl source
  217.    distribution, modifying it, and then linking your Tcl_AppInit function
  218.    with the Tcl library. For example, the standard Tcl_AppInit looks like
  219.    this:
  220.  
  221.     int
  222.     Tcl_AppInit(interp)
  223.         Tcl_Interp *interp; /* Interpreter for application. */
  224.     {
  225.         /* ... */
  226.  
  227.         if (Tcl_Init(interp) == TCL_ERROR) {
  228.             return TCL_ERROR;
  229.         }
  230.  
  231.         /* ... */
  232.     }
  233.  
  234.    In order to add the "foo" extension, you would add a call to the foo
  235.    init function, like this:
  236.  
  237.     int
  238.     Tcl_AppInit(interp)
  239.         Tcl_Interp *interp; /* Interpreter for application. */
  240.     {
  241.         /* ... */
  242.  
  243.         if (Tcl_Init(interp) == TCL_ERROR) {
  244.             return TCL_ERROR;
  245.         }
  246.  
  247.         if (Foo_Init(interp) == TCL_ERROR) {
  248.             return TCL_ERROR;
  249.         }
  250.  
  251.         /* ... */
  252.     }
  253.  
  254.    For more information, see the Tcl_AppInit man page and the tclAppInit.c
  255.    file.
  256.  
  257.    The more complete answer is that the simple answer doesn't always work.
  258.    Many extensions require more extensive modifications and the way to
  259.    combine them depends on the specific extensions involved.
  260.  
  261.    If you wish to include Extended Tcl, you should start with the
  262.    Tcl_AppInit function that comes with it. Also make sure when you link
  263.    the program that you include the Extended Tcl library *before* the
  264.    standard Tcl library, for example:
  265.  
  266.     cc -o mytclsh mytclXAppInit.c -ltclx -ltcl ...
  267.  
  268.    or for an interpreter including the Tk extensions:
  269.  
  270.     cc -o mywish mytkXAppInit.c -ltkx -ltk -ltclx -ltcl ...
  271.  
  272.    On the positive side, there are also several packages available that can
  273.    aid in combining extensions, such as Tcl-my-fancy, or extensions that
  274.    include configuration files for combining them with other popular
  275.    extensions. For example, several extensions written by Sven Delmas allow
  276.    you to specify options to configure to generate a Makefile that will
  277.    build an interpreter containing the specified extensions. See part five
  278.    of Larry Virden's FAQ for the location of these and other packages.
  279.  
  280.  
  281. ------------------
  282.  
  283. From: Tcl Q&A
  284. Subject: Q.A5- What about dynamic loading?
  285.  
  286.    The current version of Tcl supports dynamic loading on many platforms.
  287.  
  288.  
  289. ------------------
  290.  
  291. From: Tcl Q&A
  292. Subject: Q.A6- How do I create a stand alone program in case Tcl isn't
  293.                installed?
  294.  
  295.    The "Plus" patches from Jan Nijtmans (nijtmans@nici.kun.nl) include a
  296.    make target for building a standalone interpreter. See
  297.    http://www.nici.kun.nl/tkpvm/pluspatch.html
  298.  
  299.    Additionally, Earle Lowe (lowee@cpsc.ucalgary.ca) has replied that:
  300.  
  301.  
  302.       If you want to create a stand-alone program that will run anywhere
  303.       regardless of the presence/absence of TCL/Tk, than you need to go
  304.       through some more work.
  305.  
  306.       Basically, this involves converting the TCL init files to C strings,
  307.       and then rather than calling Tcl_Init() and Tk_Init(), you call
  308.       Tcl_Eval() with the converted C strings as arguments.
  309.  
  310.       I suggest getting the wish_compiler package by Alexei Rodriguez
  311.       (alexei@cis.ufl.edu) from
  312.       ftp://ftp.neosoft.com/languages/tcl/alcatel/code/wish_compiler.shar.g
  313.       z
  314.  
  315.       This package contains a tcl2c converter, and instructions as to its
  316.       use.
  317.  
  318.       With some Makefile magic, you can use using TCL/Tk as intended (as an
  319.       interpreted language using some version of wish) and when the code
  320.       works, you can create a stand-alone compiled program.
  321.  
  322.    Another option that is available is "Embedded Tk". You can get it from
  323.    ftp://ftp.vnet.net/pub/users/drh/
  324.  
  325.  
  326. ------------------
  327.  
  328. From: Tcl Q&A
  329. Subject: Q.A7- How do I make my script executable regardless of the
  330.                location of tclsh?
  331.  
  332.    There are several tricks you can use, but the most common is to make use
  333.    of the fact that, in Tcl, the backslash character works as a line
  334.    continuation indicator, even within comments. So instead of:
  335.  
  336.     #! /usr/local/bin/tclsh
  337.  
  338.     puts "Hello World"
  339.  
  340.    You could use:
  341.  
  342.     #! /bin/sh
  343.     # The next line is executed by /bin/sh, but not Tcl \
  344.       exec tclsh $0 ${1+"$@"}
  345.  
  346.     puts "Hello World"
  347.  
  348.    and the script will run regardless of where tclsh is located (as long as
  349.    it is in one of the directories in the user's PATH).
  350.  
  351.  
  352. ------------------
  353.  
  354. From: Tcl Q&A
  355. Subject: Q.A8- Why isn't <some extension> part of the standard
  356.                distribution?
  357.  
  358.    Often people ask why some language extension that they use often (in
  359.    fact, they may find it indispensible) isn't merged into the core
  360.    language.
  361.  
  362.    It must be remembered that a lot of people use Tcl on widely differing
  363.    systems and in vastly different ways. Also, Tcl was originally intended
  364.    as an embedded language that would provide the minimal programming
  365.    constructs and framework upon which a programmer could base the
  366.    scripting language for their application.
  367.  
  368.    Having said that, the core Tcl language _has_ grown over the years. Some
  369.    of the added features have come from extensions written by others. When
  370.    the capabilities provided by extensions are deemed to be of sufficient
  371.    value to all users of Tcl, John has added them to the base language.
  372.    Some examples are associative arrays, the file I/O commands (and file
  373.    handles), and the "unknown" procedure, all of which appeared in Extended
  374.    Tcl before becoming part of standard Tcl. Tk 4.0 and later contain the
  375.    equivalent functionality of the addinput and photo widget extensions
  376.    that were written by others for Tcl 7.3/Tk 3.6.
  377.   ---------------------------------------------------------------------------
  378.  
  379. PROGRAMMING RELATED QUESTIONS AND ANSWERS:
  380.  
  381.  
  382. ------------------
  383.  
  384. From: Tcl Q&A
  385. Subject: Q.B1- How can I create/use association lists or property lists?
  386.  
  387.    Use arrays or Extended Tcl keyed lists.
  388.  
  389.    For example, if you did a:
  390.  
  391.     keylset ttyFields ttyName tty1a
  392.     keylset ttyFields baudRate 57600
  393.     keylset ttyFields parity strip
  394.  
  395.    And then an "echo $ttyFields", you'd get:
  396.  
  397.     {ttyName tty1a} {baudRate 57600} {parity strip}
  398.  
  399.    Or using arrays:
  400.  
  401.     set ttyFields(ttyName)  tty1a
  402.     set ttyFields(baudRate) 57600
  403.     set ttyFields(parity)   strip
  404.  
  405.  
  406. ------------------
  407.  
  408. From: Tcl Q&A
  409. Subject: Q.B2- How can I generate a random number in Tcl?
  410.  
  411.    Extended Tcl includes a command named "random" that is an interface to
  412.    the random number generator in the system's standard C library.
  413.  
  414.    For example, to generate a random number between 0 and 9 inclusive:
  415.  
  416.     set random_number [random 10]
  417.  
  418.    It also allows you to specify a seed value.
  419.  
  420.    To get a value you can use as a seed, you might try one or a combination
  421.    of these (on Unix-like systems):
  422.  
  423.     [pid]
  424.     [file atime /dev/kmem]
  425.     [getclock]        (Extended Tcl only)
  426.  
  427.    Several pseudo-random number generator functions, written in straight
  428.    Tcl, have been posted to comp.lang.tcl, see Larry's tcl-faq/part4 for a
  429.    list.
  430.  
  431.    Here is one that is syntax compatible with the one in Extended Tcl, but
  432.    implemented in standard Tcl. The constants are from Don Libes. Be aware
  433.    that there is little checking for valid arguments.
  434.  
  435.     proc random {args} {
  436.         global RNG_seed
  437.     
  438.         set max 259200
  439.         set argcnt [llength $args]
  440.         if { $argcnt < 1 || $argcnt > 2 } {
  441.             error "wrong # args: random limit | seed ?seedval?"
  442.         }
  443.         if ![string compare [lindex $args 0] seed] {
  444.             if { $argcnt == 2 } {
  445.                 set RNG_seed [lindex $args 1]
  446.             } else {
  447.                 set RNG_seed [expr \
  448.                     ([pid]+[file atime /dev/kmem])%$max]
  449.             }
  450.             return
  451.         }
  452.         if ![info exists RNG_seed] {
  453.             set RNG_seed [expr ([pid]+[file atime /dev/kmem])%$max]
  454.         }
  455.         set RNG_seed [expr ($RNG_seed*7141+54773) % $max]
  456.         return [expr int(double($RNG_seed)*[lindex $args 0]/$max)]
  457.     }
  458.  
  459.  
  460. ------------------
  461.  
  462. From: Tcl Q&A
  463. Subject: Q.B3- How can I call one proc with the multi parameter value
  464.                returned by another proc?
  465.  
  466.    Assuming y requires multiple args and x returns multiple words, use
  467.    Tcl's eval command:
  468.  
  469.     eval y [x]
  470.  
  471.  
  472. ------------------
  473.  
  474. From: Tcl Q&A
  475. Subject: Q.B4- How can I pass an array into a proc?
  476.  
  477.    Use upvar rather than try to use global variables when possible. If the
  478.    function is event driven, you are forced to use global variables.
  479.  
  480.     # print elements of an array
  481.     proc show_array arrayName {
  482.         upvar $arrayName myArray
  483.  
  484.         foreach element [array names myArray] {
  485.            puts stdout "${arrayName}($element) =  $myArray($element)"
  486.         }
  487.     }
  488.  
  489.     set arval(0) zero
  490.     set arval(1) one
  491.     show_array arval
  492.  
  493.    To return an array from a procedures, just take the array name in as an
  494.    argument, as above. Any changes you make in the array will be made in
  495.    the parent's array as well.
  496.  
  497.    Extended Tcl introduces a concept called keyed lists which are arrays
  498.    made out of lists of key-value pairs and can be passed by value to
  499.    routines, over networks, etc.
  500.  
  501.  
  502. ------------------
  503.  
  504. From: Tcl Q&A
  505. Subject: Q.B5- How can I run an external command and read its output via a
  506.                pipe?
  507.  
  508.    For example, to grep a pattern out of a range of files, one might do:
  509.  
  510.    Karl Lehenbauer (karl@NeoSoft.com) writes:
  511.  
  512.     set files [glob /home/cole/stats/*]
  513.  
  514.     proc parseInfo { site } {
  515.        global files
  516.  
  517.     #
  518.     # site is chosen from a listbox earlier
  519.     #
  520.        set in [open [concat "|/usr/bin/grep $site $files"] r]
  521.  
  522.        while {[gets $in line]>-1} {
  523.           puts stderr $line
  524.        }
  525.        catch {close $in}
  526.     }
  527.  
  528.    One thing: the matching strings are *not* returned in directory order.
  529.  
  530.    But what if I want to check the return code AND use the output of the
  531.    command? Kevin B. Kenny (kennykb@dssv01.crd.ge.com) writes:
  532.  
  533.     if [catch {exec ls} data] {
  534.         # The exec got an error, and $errorCode has its termination status
  535.     } else {
  536.         # The exec succeeded
  537.     }
  538.     # In any case, `data' contains all the output from the child process.
  539.  
  540.    Note that Karl Lehenbauer (karl@NeoSoft.com) adds that errorCode will be
  541.    a list containing three elements, the string "CHILDSTATUS", the process
  542.    ID of the child, and the exit status of the child.
  543.  
  544.  
  545. ------------------
  546.  
  547. From: Tcl Q&A
  548. Subject: Q.B6- How can I delete a procedure from within a script?
  549.  
  550.    Rename the procedure to have no name, for example:
  551.  
  552.     rename procedureName ""
  553.  
  554.  
  555. ------------------
  556.  
  557. From: Tcl Q&A
  558. Subject: Q.B7- How can I get more than 7 digits of double precision?
  559.  
  560.    In Tcl 7.x, set the global variable `tcl_precision' to a value in the
  561.    range 1-17. For example:
  562.  
  563.     % expr 4*atan(1)
  564.     3.14159
  565.     % set tcl_precision 0
  566.     can't set "tcl_precision": improper value for precision
  567.     % set tcl_precision 3
  568.     3
  569.     % expr 4*atan(1)
  570.     3.14
  571.     % set tcl_precision 16
  572.     16
  573.     % expr 4*atan(1)
  574.     3.141592653589793
  575.     % set tcl_precision 18
  576.     can't set "tcl_precision": improper value for precision
  577.  
  578.    In Tcl 6.x, you must modify the tclExpr.c module to use "%lf" instead of
  579.    "%g".
  580.  
  581.  
  582. ------------------
  583.  
  584. From: Tcl Q&A
  585. Subject: Q.B8- How can I grab the command line when a non-built-in call is
  586.                made?
  587.  
  588.    The procedure "unknown" is called automatically with arguments
  589.    containing the command and its arguments for any command that couldn't
  590.    be found. In fact, Tcl and Extended Tcl use this feature to provide
  591.    demand loaded commands, and even entire libraries. Also, when using the
  592.    "tclsh" or "tcl" shells interactively, the "unknown" procedure enables
  593.    the shell to run external programs (i.e. without typing "exec" first).
  594.  
  595.    So by modifying the unknown procedure you can provide your own extended
  596.    functionality, or even remove the demand loading capability if you so
  597.    desire.
  598.  
  599.  
  600. ------------------
  601.  
  602. From: Tcl Q&A
  603. Subject: Q.B9- How can I get or set an environment variable?
  604.  
  605.    By using something like the following.
  606.  
  607.     set olddisplay $env(DISPLAY)
  608.     set env(DISPLAY) unix:0
  609.  
  610.    Thanks to Joel Fine (joel@cs.berkeley.edu) for the answer.
  611.  
  612.    You can also check if an environment variable exists, like this:
  613.  
  614.     if [info exists env(VARNAME)] {
  615.         # okay, it's there, use it
  616.         set value $env(VARNAME)
  617.     } else {
  618.         # the environment var isn't set, use a default
  619.         set value "the default value"
  620.     }
  621.  
  622.  
  623. ------------------
  624.  
  625. From: Tcl Q&A
  626. Subject: Q.B10- How can I use numbers with leading zeroes?
  627.  
  628.    In Tcl, numbers that start with a zero are interpreted as octal values
  629.    (or hexadecimal, if the zero is immediately followed by an 'x'). Often,
  630.    this is a useful feature, but there are some problems:
  631.      * What if the number has invalid octal (hexadecimal) digits?
  632.  
  633.        Prior to Tcl 7.4b3, the interpreter would try to interpret a number
  634.        such as 08 as a floating point value. Starting with 7.4b3, the
  635.        interpreter generates an error message when presented with such a
  636.        value.
  637.      * What if you want the value interpreted as a decimal number (as if
  638.        the leading zeros weren't there)?
  639.  
  640.        You have to strip off the zeros. Here are two possible
  641.        implementations of a function "stripzeros":
  642.  
  643.     proc stripzeros {value} {
  644.         regsub ^0+(.+) $value \\1 retval
  645.         return $retval
  646.     }
  647.  
  648.      or
  649.  
  650.     proc stripzeros {value} {
  651.         set retval [string trimleft $value 0]
  652.         if { ![string length $retval] } {
  653.             return 0
  654.         }
  655.         return $retval
  656.     }
  657.  
  658.      Then, whenever you are passing a value to a command that requires an
  659.      integer value, and there is the possibility that the value has a
  660.      leading zero, just pass it through this function first, i.e. instead
  661.      of:
  662.  
  663.     set dayofweek [expr $days%7]
  664.  
  665.      use:
  666.  
  667.     set dayofweek [expr [stripzeros $days]%7]
  668.  
  669.  
  670.  
  671. ------------------
  672.  
  673. From: Tcl Q&A
  674. Subject: Q.B11- How can I find the command line arguments to my
  675.                 application?
  676.  
  677.    The program name is assigned to the global variable "argv0" and any
  678.    arguments to it are placed in the global variable "argv" as a list. The
  679.    variable "argc" is set to the number of elements in the list "argv". As
  680.    an example:
  681.  
  682.     #! /usr/local/bin/tclsh
  683.  
  684.     if { $argc != 2 } {
  685.         puts stderr "$argv0: Usage: $argv0 <infile> <outfile>"
  686.         exit 1
  687.     }
  688.  
  689.     set infile  [lindex $argv 0]
  690.     set outfile [lindex $argv 1]
  691.  
  692.  
  693. ------------------
  694.  
  695. From: Tcl Q&A
  696. Subject: Q.B12- I found a bug in the handling of comments!
  697.  
  698.    Sometimes people find that Tcl behaves differently than they expect
  699.    (often because of the way some other language acts in a similar
  700.    situation). They then think this unexpected behavior is a bug. Probably
  701.    the most common occurance of this is in regard to comments.
  702.  
  703.    In Tcl, everything passed to the parser must have proper list structure,
  704.    even comments (and yes comments are passed to the parser, unlike in some
  705.    languages where they are stripped at an earlier stage). Generally, this
  706.    means you need to make sure your braces are evenly matched, even though
  707.    they may be on a line that is commented out.
  708.  
  709.    So, for example, you have an "if" statement that tests a certain
  710.    condition, but you want to try testing a different condition. You
  711.    comment out the old condition and type a new "if" statement. This code
  712.    will cause an error that there is a missing close-brace:
  713.  
  714.     ## WRONG
  715.     if { $newflag } {
  716.     # if { $oldflag } {
  717.         puts hello
  718.     }
  719.  
  720.    In this case, you have to balance the braces, for example:
  721.  
  722.     ## CORRECT
  723.     if { $newflag } {
  724.     # if { $oldflag } {
  725.         puts hello
  726.     # }
  727.     }
  728.  
  729.    Another interesting point about comments in Tcl is that the line
  730.    continuation mechanism still applies, so:
  731.  
  732.     # This is a comment line that ends with a backslash \
  733.       and this line is still part of the comment
  734.  
  735.  
  736. ------------------
  737.  
  738. From: Tcl Q&A
  739. Subject: Q.B13- How can I input and output binary data?
  740.  
  741.    Internally, the Tcl interpreter stores nearly everything in
  742.    null-terminated strings. This procludes the possibility of directly
  743.    storing binary data (or more specifically, anything with embedded
  744.    nulls).
  745.  
  746.    However, certain operations can be performed without the data being
  747.    stored in Tcl variables. For example, file handles can be attached
  748.    directly to external programs, like this:
  749.  
  750.     set infp [open "|compress -dc $fileName"]
  751.  
  752.     exec gzip -c $newFileName <@ $infp
  753.  
  754.    or in Extended Tcl:
  755.  
  756.     set infp [open "|compress -dc $fileName"]
  757.     set outfp [open "|gzip -c $newFileName" w]
  758.  
  759.     copyfile $infp $outfp
  760.  
  761.    In Tcl 7.5, you can copy binary data between open channels using the
  762.    command "unsupported0". However, as is indicated by the name, it is not
  763.    a method which should be relied on - it will disappear in a future
  764.    release, although proper support for handling binary data should be
  765.    added at that time.
  766.  
  767.    Alternatively, you can in some way convert the data to a form that is
  768.    usable from Tcl:
  769.      * Reading from or writing to a pipe that is connected to an external
  770.        filter, such as atob/btoa, uuencode/uudecode, or even od.
  771.      * A method of handling binary data one character at a time using
  772.        standard Tcl can be found in:
  773.        ftp.neosoft.com:/languages/tcl/alcatel/code/binary-io-hack.shar.gz
  774.      * There are also two different extensions available that enable the
  775.        handling of binary data: tclbin and BinarIO. The former is better
  776.        for handling structured data, while the latter works better for
  777.        binary data streams.
  778.  
  779.    Thanks to Wayne Throop throop@aur.alcatel.com for his contribution to
  780.    this section.
  781.  
  782.  
  783. ------------------
  784.  
  785. From: Tcl Q&A
  786. Subject: Q.B14- How can I trap signals, and perform other more Unix
  787.                 specific functions?
  788.  
  789.    Extended Tcl offers many of these types of functions. For instance,
  790.    extended Tcl has the "signal" command:
  791.  
  792.  
  793.       signal action siglist [command]
  794.  
  795.       where action is one of "default", "ignore", "error", "trap", "get",
  796.       plus the POSIX "block" and "unblock" actions (available only on POSIX
  797.       systems, of course). Siglist is a list of either the symbolic or
  798.       numeric Unix signal (the SIG prefix is optional). Command is your
  799.       error handler (or a simple {puts stdout "Don't press *that* key!"}
  800.       :-) "trap" does what you expect, and I find "error" and "get" to be
  801.       extremely useful in interactive programs which demand keyboard
  802.       traversal.
  803.  
  804.       Extended Tcl also has things like fork, etc.
  805.  
  806.    Answer by Brad Morrison (brad@NeoSoft.com).
  807.  
  808.  
  809. ------------------
  810.  
  811. From: Tcl Q&A
  812. Subject: Q.B15- How can I do a double indirect? - Why doesn't $$var work?
  813.  
  814.    There is one and only one level of substitution possible with every pass
  815.    through the interpreter. Also, when doing variable substitution, the
  816.    interpreter finds the dollar sign and then takes everything following it
  817.    up until the next invalid character (where invalid is defined as
  818.    anything other than a letter, digit, or underscore) as the name of the
  819.    variable - well, that is, unless it finds array notation or the
  820.    "${varname}" form.
  821.  
  822.    In the case of "$$var", the character after the first dollar sign is an
  823.    invalid character (another dollar sign), so there is no variable name
  824.    and variable substitution is not performed (the dollar sign is left as
  825.    is) and scanning starts again for any dollar signs and a following
  826.    variable name. It is immediately found at that second dollar sign, the
  827.    substitution is performed, and scanning for dollar signs resumes with
  828.    whatever was after the variable name. Since there isn't anything else,
  829.    substition is done for this pass through the interpreter (remember it's
  830.    only done once).
  831.  
  832.    The "eval" command runs its arguments through the interpreter, so you
  833.    could use "eval" to cause a second pass through the interpreter, and
  834.    thus, have "$$var" work:
  835.  
  836.     % set a 5
  837.     5
  838.     % set var a
  839.     a
  840.     % puts $$var              ;# This doesn't work
  841.     $a
  842.     % eval puts $$var         ;# This does  - but it's dangerous
  843.     5
  844.  
  845.    However, if the contents of "var" contain any _special_ characters (e.g.
  846.    whitespace, semicolon) you'll run into problems.
  847.  
  848.    A better method is to take advantage of the behaviour of the "set"
  849.    command when given only one argument, and combine command substitution
  850.    with variable substitution:
  851.  
  852.     % puts [set $var]         ;# This works safely
  853.     5
  854.  
  855.    or, in fact, you could use just command substitution (which is performed
  856.    once for each "[ ]" pair):
  857.  
  858.     % puts [set [set var]]    ;# as does this
  859.     5
  860.  
  861.    Similarly, to print the values of `var1', `var2', and `var3':
  862.  
  863.     set var1 3.14159
  864.     set var2 hello
  865.     set var3 13
  866.     foreach num {1 2 3} {
  867.     puts "var$num = [set var$num]"
  868.     }
  869.  
  870.    will output:
  871.  
  872.     var1 = 3.14159
  873.     var2 = hello
  874.     var3 = 13
  875.  
  876.    The "upvar" command can also be used to derefence variables.
  877.  
  878.    In addition, starting with version 7.4, the interpreter includes the
  879.    command "subst" which can be used to perform substitutions.
  880.  
  881.    Note that all of the above applies to array variables also.
  882.  
  883.  
  884. ------------------
  885.  
  886. From: Tcl Q&A
  887. Subject: Q.B16- How can I get quoted strings to work the way I want?
  888.  
  889.    A long article dealing with the issues can be found at
  890.    ftp://ftp.neosoft.com/languages/tcl/alcatel/docs/README.programmer.gz
  891.  
  892.    Here are some short answers:
  893.  
  894.    Q. I'm trying to build up a command for later execution but am having
  895.    trouble with variable values that include whitespace or special
  896.    characters.
  897.  
  898.    A. The safest way to build up commands is to use the list command so
  899.    that you can keep track of the list structure. Avoid using double quotes
  900.    because you can end up with an extra trip through the evaluator. We'll
  901.    illustrate this with a command to create a button that prints out the
  902.    label on the button when you click it.
  903.  
  904.  
  905.       Wrong answer #1:
  906.  
  907.     button $myname -text $label -command "puts stdout $label"
  908.  
  909.       Why? because if $label has whitespace then the puts command will be
  910.       passed the wrong number of arguments. If $label has $ or [ ]
  911.       characters, they will be interpreted instead of printed.
  912.  
  913.       Good answer #2:
  914.  
  915.     button $myname -text $label -command [list puts stdout $label]
  916.  
  917.       Why? because list will properly quote the value of $label
  918.  
  919.    Q. I'm trying to build up a command for later execution but am having
  920.    trouble getting some variables to evaluate now, and some to evaluate
  921.    later when the command is run.
  922.  
  923.    A. The cleanest way to do this is to define a procedure that hides the
  924.    use of the variables at run time, and then build up a call to that
  925.    procedure using the list command as described previously. (You can even
  926.    define the procedure on the fly. It will have global scope even it if is
  927.    created within another procedure.)
  928.  
  929.  
  930.       Wrong answer #1:
  931.  
  932.     button $myname -text $label -command \
  933.         [list puts stdout $ArrayOfDynamicStuff($label)]
  934.  
  935.       Why? The array value will be substituted when the button is created,
  936.       not later on when the button is clicked. Also, note that the command
  937.       is executed at the global scope, so it is not necessary to include a
  938.       "global ArrayOfDynamicStuff" in the command.
  939.  
  940.       Wrong answer #2 (backquotes and list):
  941.  
  942.     button $myname -text $label -command \
  943.         [list puts stdout \$ArrayOfDynamicStuff($label)]
  944.  
  945.       Why? Here the list command and the backquote of $ are fighting with
  946.       each other. The command ends up being something like:
  947.  
  948.     puts stdout {$ArrayOfDynamicStuff(foo)}
  949.  
  950.       which prevents the substitution of the value of the array element.
  951.  
  952.       Dubious answer #3 (backquotes and double-quotes):
  953.  
  954.     button $myname -text $label -command \
  955.         "puts stdout \$ArrayOfDynamicStuff($label)"
  956.  
  957.       Why? This only works if the value of $label has no special characters
  958.       or whitespace.
  959.  
  960.       Clean answer #4 (proc):
  961.  
  962.     proc doit { i } {
  963.         global ArrayOfDynamicStuff
  964.         puts stdout $ArrayOfDynamicStuff($i)
  965.     }
  966.     button $myname -text $label -command [list doit $label]
  967.  
  968.       Why? Using little TCL procs for your button commands is a good habit
  969.       because it eliminates most needs for fancy quoting, and it makes it
  970.       easier to tweak the button command later on.
  971.  
  972.    Q. I'm trying to pass along a variable number of args to another
  973.    procedure but I'm having trouble getting the $args to expand right.
  974.  
  975.    A. Avoid using eval and double quotes because that results in an extra
  976.    trip through the interpreter. The eval command will do a concat of its
  977.    arguments if there are more than one, so that pretty much eliminates the
  978.    need to group things with double quotes. Let's extend the button
  979.    example:
  980.  
  981.  
  982.       Wrong answer #1:
  983.  
  984.     proc mybutton { myname label args } {
  985.         button $myname -text $label -command [list puts stdout $label] $args
  986.     }
  987.  
  988.       Why? All the extra arguments to mybutton are grouped into one list
  989.       element that is but into the value of $args. However, the button
  990.       command expects to see individual arguments, not a sub-list.
  991.  
  992.       Wrong answer #2:
  993.  
  994.     proc mybutton { myname label args } {
  995.         eval "button $myname -text $label \
  996.                  -command [list puts stdout $label] $args"
  997.     }
  998.  
  999.       Why? The double quotes allow expansion of $label as well as $args, so
  1000.       if $label has any whitespace, the button command will be malformed
  1001.  
  1002.       Good answer #3:
  1003.  
  1004.     proc mybutton { myname label args } {
  1005.         set cmd {button $myname -text $label -command [list puts stdout $label]}
  1006.         eval $cmd $args
  1007.     }
  1008.  
  1009.       Why? Eval will first concatenate its two arguments and then run the
  1010.       result through the interpreter. Think of this as stripping off the
  1011.       outer curly braces from $cmd and $arg and making a single list with
  1012.       all the elements of both. $label will be evaluated exactly once, so
  1013.       the puts command will remain good, and whatever went into args will
  1014.       also be processed exactly one time.
  1015.  
  1016.    Q. Why do I get a syntax error in an if/while/for statement?
  1017.  
  1018.    A. You may have written something like
  1019.  
  1020.     wish: set foo bar
  1021.     wish: if {$foo == bar} {puts stdout bar}
  1022.     syntax error in expression "$foo == bar"
  1023.  
  1024.    in which bar is interpreted as neither a string nor a variable, since
  1025.    strings as operands in expressions MUST be surrounded by double quotes
  1026.    or braces.
  1027.  
  1028.    Change to
  1029.  
  1030.     wish: if {$foo == "bar"} {puts stdout bar}
  1031.  
  1032.    or
  1033.  
  1034.     wish: if {$foo == {bar}} {puts stdout bar}
  1035.  
  1036.    always in expressions, depending on if you want expansion performed or
  1037.    not.
  1038.  
  1039.    Contributed by Jesper Blommaskog (d9jesper@dtek.chalmers.se).
  1040.  
  1041.  
  1042. ------------------
  1043.  
  1044. From: Tcl Q&A
  1045. Subject: Q.B17- How can I share procedures between multiple tcl
  1046.                 applications?
  1047.  
  1048.    Rather than sourcing the files explicitly, build a tcl library:
  1049.  
  1050.    Step 1. Put the files in a common directory
  1051.  
  1052.    Step 2. Build the tclIndex for the "library". I use a Makefile with a
  1053.    convention like:
  1054.  
  1055.     install.index:
  1056.         (cd ${DESTDIR}/tclscripts/lib; \
  1057.         echo 'source /usr/local/lib/tcl/init.tcl;\
  1058.         auto_mkindex . *.tk' | tcl ; exit 0)
  1059.  
  1060.    Step 3. Modify your tcl scripts to reference the library:
  1061.  
  1062.    e.g.:
  1063.  
  1064.     # local additions
  1065.     lappend auto_path /usr/local/lib/tcl_local $env(RDS_TCL_SCRIPTS)/lib
  1066.  
  1067.    Now, as soon as your script tried to reference a procedure in the
  1068.    library, the "unknown" command autoloads the procedure for you.
  1069.  
  1070.    Contributed by Joe VanAndel (vanandel@ncar.ucar.edu).
  1071.  
  1072.  
  1073. ------------------
  1074.  
  1075. From: Tcl Q&A
  1076. Subject: Q.B18- How can I get items inserted into a list?
  1077.  
  1078.    A user asked why
  1079.  
  1080.     linsert $list 0 ..
  1081.  
  1082.    did not result in .. being inserted into list.
  1083.  
  1084.    Jesper Blommaskog (d9jesper@dtek.chalmers.se) replied:
  1085.  
  1086.    When doing list operations other than "lappend", you must save the
  1087.    returned value. This applies to "list", "lindex", "lrange", and
  1088.    "lreplace" at least.
  1089.  
  1090.    In this example, you would perhaps want to do something like:
  1091.  
  1092.     set list [ linsert $list 0 .. ] 
  1093.  
  1094.  
  1095. ------------------
  1096.  
  1097. From: Tcl Q&A
  1098. Subject: Q.B19- How can I perform a non-blocking read on a file identifier?
  1099.  
  1100.    From Frank Smith (frank@arraysystems.nstn.ns.ca) we are told that if you
  1101.    have Extended Tcl, you can
  1102.  
  1103.     read $fileId [fstat $fileId size]
  1104.  
  1105.    This will read only the number of bytes currently available on `fileId'
  1106.    and consequently will not block.
  1107.  
  1108.  
  1109. ------------------
  1110.  
  1111. From: Tcl Q&A
  1112. Subject: Q.B20- How can I read and write from a pipe without locking up?
  1113.  
  1114.    The stdio package has an optimization for speed that buffers characters
  1115.    so that you can avoid un-necessary system calls. This isn't very good
  1116.    for interactive use, so stdio changes its behaviour if the file is a
  1117.    terminal. When you write to a pipe you are not writing to a terminal and
  1118.    so your output is buffered, similarly when the program at the other end
  1119.    of the pipe writes its response it is also buffered. The end result is
  1120.    the programs at both end of the pipe stall waiting for input. It is
  1121.    possible to force the buffers to be written using "flush" in Tcl and
  1122.    fflush() in C, often though, you don't have control over the program at
  1123.    the other end of the pipeline and in that case your only solution is to
  1124.    use a pseudo-teletype (pty). Unfortunately this isn't all that easy.
  1125.  
  1126.    While this isn't directly supported in Tcl, the "spawn" command in
  1127.    expect opens a pty and starts a command.
  1128.  
  1129.  
  1130. ------------------
  1131.  
  1132. From: Tcl Q&A
  1133. Subject: Q.B21- How can I find what version I am running?
  1134.  
  1135.    Well, there is no one answer to this. Tcl itself, will tell you its
  1136.    version, if you type:
  1137.  
  1138.     info tclversion
  1139.  
  1140.    or with recent versions:
  1141.  
  1142.     puts $tcl_version
  1143.  
  1144.    On the other hand, to get the Tk version, you must use:
  1145.  
  1146.     puts $tk_version
  1147.  
  1148.    Other extensions use other means (for example, Extended Tcl uses "infox
  1149.    version" to return its version number).
  1150.  
  1151.    Starting with the 7.4 version, the tclsh executable is, by default,
  1152.    installed with the version number appended - making it rather obvious
  1153.    which version you are running. However, if it is not installed like that
  1154.    on your system, you can find out the version from the shell prompt with
  1155.    something like:
  1156.  
  1157.     echo 'puts $tcl_version;exit 0' | tclsh
  1158.  
  1159.    (the ";exit 0" part is only needed for wish and its derivatives, but
  1160.    doesn't hurt anything when it isn't needed)
  1161.  
  1162.  
  1163. ------------------
  1164.  
  1165. From: Tcl Q&A
  1166. Subject: Q.B22- How can I use variables to hold array names?
  1167.  
  1168.    Eric Bleeker (ericbl@paramount.nikhefk.nikhef.nl) has written:
  1169.  
  1170.  
  1171.       You may have written something like:
  1172.  
  1173.     % set foo "bar baz"
  1174.     bar baz
  1175.     % foreach aap $foo {
  1176.         set $aap(1) "something"
  1177.     }
  1178.     can't read "aap(1)": variable isn't array
  1179.  
  1180.       This means Tcl tries to substitute the array element `aap(1)' which
  1181.       doesn't exist. To fix this use:
  1182.  
  1183.     % foreach aap $foo {
  1184.         set [set aap](1) "something"
  1185.     }
  1186.  
  1187.       In this case two arrays `bar' and `baz' are created.
  1188.  
  1189.    An alternative format is:
  1190.  
  1191.     % foreach aap $foo {
  1192.         set ${aap}(1) "something"
  1193.     }
  1194.  
  1195.  
  1196. ------------------
  1197.  
  1198. From: Tcl Q&A
  1199. Subject: Q.B23- How can I make variables static, like in C?
  1200.  
  1201.    The "static" keyword in C is used for different purposes. One of the
  1202.    most common is when you wish to have a variable that retains its value
  1203.    across invocations of the same function (but without making the variable
  1204.    globally available). Karl Lehenbauer (karl@NeoSoft.com) wrote the
  1205.    following procedure to emulate this behaviour:
  1206.  
  1207.     proc static {args} {
  1208.         set procName [lindex [info level -1] 0]
  1209.         foreach varName $args {
  1210.         uplevel 1 "upvar #0 staticvars($procName:$varName) $varName"
  1211.         }
  1212.     }
  1213.  
  1214.    Another common use, is when you want a global variable that is only
  1215.    visible to functions within that particular source file. This is best
  1216.    handled by a namespace facility. Tcl does not yet have a built-in
  1217.    namespace facility, but several extensions are available the provide
  1218.    one. See the software catalog in Larry's FAQ for more info.
  1219.  
  1220.  
  1221. ------------------
  1222.  
  1223. From: Tcl Q&A
  1224. Subject: Q.B24- How can I convert between a character and its ASCII value?
  1225.  
  1226.    To get the ASCII value of a character:
  1227.  
  1228.     proc toASCII { char } {
  1229.         scan $char %c value
  1230.         return $value
  1231.     }
  1232.  
  1233.     puts "The ASCII value of 'A' is [toASCII A]"
  1234.  
  1235.    To convert from an ASCII value to a character:
  1236.  
  1237.     proc toChar { value } {
  1238.         return [format %c $value]
  1239.     }
  1240.  
  1241.     puts "The ASCII value 65 corresponds to the character '[toChar 65]'"
  1242.  
  1243.   ---------------------------------------------------------------------------
  1244.  
  1245. EXPLANATION OF ERROR MESSAGES:
  1246.  
  1247.    This section lists some error messages and some possible (and
  1248.    non-obvious) reasons why you may be getting them.
  1249.  
  1250.  
  1251. ------------------
  1252.  
  1253. From: Tcl Q&A
  1254. Subject: Q.C1- "not found" or "Command not found"
  1255.  
  1256.    This error message comes from your shell, not Tcl. The script probably
  1257.    starts with a "#!" that is followed by a path to the Tcl interpreter
  1258.    that is too long for your system. Many Unix systems are limited to 32
  1259.    characters (including the "#!") in this regard. So, something like this
  1260.    will fail:
  1261.  
  1262.     #! /usr/home/homedir/very/long/path/tclsh
  1263.  
  1264.     # rest of script
  1265.  
  1266.    You can either shorten the path by moving the tclsh executable to a
  1267.    different directory or by using symbolic links. Another option is to not
  1268.    specify the path at all. See the question "How do I make my script
  1269.    executable regardless of the location of tclsh?" for an example of how
  1270.    to do this.
  1271.  
  1272.  
  1273. ------------------
  1274.  
  1275. From: Tcl Q&A
  1276. Subject: Q.C2- invalid command name "}"
  1277.  
  1278.    You have probably commented out a line that ends with an open curly
  1279.    brace. See Question B12 for more info.
  1280.  
  1281.  
  1282. ------------------
  1283.  
  1284. From: Tcl Q&A
  1285. Subject: Q.C3- missing close-brace
  1286.  
  1287.    Your braces aren't balanced. Again, one likely, though perhaps
  1288.    non-obvious, reason is improperly commented lines. See Question B12.
  1289.  
  1290.  
  1291. ------------------
  1292.  
  1293. From: Tcl Q&A
  1294. Subject: Q.C4- X server insecure (must use xauth-style authorization)
  1295.  
  1296.    Tk requires you to have a secure X server before you can use the send
  1297.    command. See the question 2.A.7 "How can I get Tk 3.3 to even start - I
  1298.    get security error messages." in Thomas Accardo's Tk Toolkit Usage FAQ
  1299.    as well as http://ce-toolkit.crd.ge.com/tkxauth/ for instructions on how
  1300.    to make your server secure.
  1301.  
  1302.  
  1303. ------------------
  1304.  
  1305. From: Tcl Q&A
  1306. Subject: Q.C5- expected integer but got ...
  1307.  
  1308.    This error occurs when trying to perform some mathematical operation
  1309.    that requires an integer value, but given a non-integer value. While
  1310.    this is pretty obvious when the value supplied is a floating point value
  1311.    or an alphabetic string, this error can be confusing when the value is
  1312.    something that may look like a valid integer. Specifically numbers that
  1313.    have leading zeros. See the question How can I use numbers with leading
  1314.    zeros? for an explanation.
  1315.  
  1316.  
  1317. ------------------
  1318.  
  1319. From: Tcl Q&A
  1320. Subject: Q.C6- "Undefined symbol: main" or similar
  1321.  
  1322.    If when linking an application you get an error saying that the "main"
  1323.    function was not found, it is probably because the application was
  1324.    written for a version of Tcl prior to Tcl 7.4 and you are linking it
  1325.    with version 7.4 (or later).
  1326.  
  1327.    Older versions of Tcl included a "main" function in the library
  1328.    ("libtcl.a"). This caused various problems, in particular with C++
  1329.    programs, and it was removed starting with version 7.4b1. The
  1330.    application being linked with the Tcl library must now provide the
  1331.    "main" routine itself.
  1332.   ---------------------------------------------------------------------------
  1333.  
  1334. COMPILING AND OTHER PLATFORM SPECIFIC QUESTIONS:
  1335.  
  1336.    This section contains some hints on compiling the Tcl distribution on
  1337.    various platforms and other notes regarding platform specific problems.
  1338.  
  1339.    For building Tcl 7.4 and newer releases, there is now an on-line
  1340.    database, maintained by Sun, for people to query, if they have problems
  1341.    building the standard distribution. If the software did not build
  1342.    automatically for you, check at http://www.sunlabs.com/research/tcl/ for
  1343.    an entry regarding your platform. And please share your experience, if
  1344.    there is no current entry and you were able to get the distribution to
  1345.    build somehow.
  1346.  
  1347.    Notes that were previously listed here, but which apply primarily to
  1348.    older releases are now available in a separate document at:
  1349.    http://psg.com/~joem/tcl/PlatformNotes.html
  1350.  
  1351.    See also Question A2
  1352.  
  1353.  
  1354. ------------------
  1355.  
  1356. From: Tcl Q&A
  1357. Subject: Q.D1- Why does "glob" cut off the first two characters of each
  1358.                filename?
  1359.  
  1360.    This problem can occur on SVR4, Solaris 2.x, and similar systems that
  1361.    have both System V and BSD libraries. It happens when the interpreter is
  1362.    compiled with include files that don't match the libraries to which it
  1363.    is linked (specifically, readdir returns a different structure than is
  1364.    expected). You need to modify the Makefile to make sure the include
  1365.    files correspond to the libraries.
  1366.  
  1367.  
  1368. ------------------
  1369.  
  1370. From: Tcl Q&A
  1371. Subject: Q.D2- How do I get Tcl working on MS Windows (3.1, 95, or NT)?
  1372.  
  1373.    For Microsoft specific information see the FAQ maintained by Eric
  1374.    Johnson (ejohnson@camax.com). It is available at:
  1375.    http://ourworld.compuserve.com:80/homepages/efjohnson/tclwin.htm
  1376.  
  1377.  
  1378. ------------------
  1379.  
  1380. From: Tcl Q&A
  1381. Subject: Q.D3- How can I build the Tcl library as a shared libary?
  1382.  
  1383.    This is now supported for some platforms in the standard distribution.
  1384.  
  1385.    You can also get the "Plus" patches, which include support for
  1386.    additional platforms. For more information, see
  1387.    http://www.nici.kun.nl/tkpvm/pluspatch.html
  1388.  
  1389.    Additionally, Extended Tcl includes instructions for building shared
  1390.    libraries on many Unix systems. You may be able to use this information,
  1391.    if your platform is not supported by the plus patches.
  1392.   ---------------------------------------------------------------------------
  1393.    End of FAQ
  1394. ---
  1395. set Email "joe@morton.rain.com (Joe Moss)";set URL http://psg.com/~joem/
  1396.  ======================================================================
  1397. "Folk, if you are not checking the Frequently Asked Questions documents
  1398. before you ask questions, you are taking longer to resolve problems,
  1399. and annoying more folk, than necessary."   -- Larry W. Virden
  1400.