Version 11 of gotcha

Updated 2012-12-22 20:29:12 by pooryorick

Summary

A gotcha is an unexpected side effect, behavior, consequence, requirement, etc.

Description

As with any language, the syntax and semantics of Tcl can catch the programmer off guard. This page is an attempt to enumerate constructs and behaviour's that can be construed as gotchas.

The Octabug

See Tcl and octal numbers

Working Lists

Another Tcl gotcha is to hand arbitrary strings, read from the user or a file/command, directly into a list operation without first ensuring that the contents is, in actuality, a list.

Switch its Arguments

RS One possible gotcha is switch -- always use "--" before the switch variable, since if the value of the switch variable starts with "-", you'll get a syntax error.

Delimiting Options from Argumennts

KPV Also, comments w/i switch, while possible, are tricky.

RS 2010-05-10 A similar gotcha is in the text search subcommand - although the misunderstanding could be avoided by counting non-optional arguments from the end,

set whatever -this
$t search $whatever 1.0

raises an error that "-this" is an undefined switch. For robustness, use

$t search -- $whatever 1.0

if the slightest possibility exists that $whatever might start with a dash.

Intepretations by expr

RS 2010-02-24: Yet another gotcha we ran into last night: Consider a function

proc f x {
    if {$x == 00000000} {
        puts "$x is NULL"
    }
}

which reported:

0E123456 is NULL

How so? Bug? No -- feature. With the == comparison operator, the operands are tried to match as integers, floats, or then as strings. And the $x in this case, though meant as a pointer in hex, could be parsed as float - with the numeric value of 0, which is numerically equivalent to 00000000. The solution of course was to use the eq operator instead.

[set] and namespaces

Twylite 2012-12-12: Setting a variable in a namespace eval can clobber a global variable. See Dangers of creative writing.

set foo 10
namespace eval bar { set foo 20 ; set bar 30 }
puts $foo ;# puts: 20
puts $bar ;# error: can't read "bar": no such variable