1

I have to run TCL code written for version 8.5 with the interpreter of version 8.3.

I am having problems with eq. Seems 8.3 doesn't recognize it. A simple code:

% expr { "a" eq "b" }

returns an error message like:

syntax error in expression "a eq b"

I am trying to fix this by adding an interp alias before everything like this:

interp alias {} eq {} ==

but seems it has no effect.

Is there any way to make eq an alias for ==? If no, is the only way to get rid of this error is to replace all eq statements by == statements?

Vahagn
  • 4,670
  • 9
  • 43
  • 72

3 Answers3

4

The eq and ne operators were introduced in Tcl 8.4. See Changes in Tcl/Tk 8.4 on the Tcler's wiki. There is no way to make "eq" be an alias for "==" within an expression. Your attempt to use the alias command only resulted in creating a command named eq that does whatever the command == does (specifically, fail unless you actually have a command named ==).

If you absolutely must find a way to make this work, in your 8.3 interpreter you can rename expr to be something else (eg: _expr), then create your own expr command that does a string substitution before calling the renamed expr command. This is fraught with peril since you have to make sure to only substitute operators and not the data being compared.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    I guess the custom `expr` will not be called for `if { "a" eq "b" } { }`, right? – Vahagn Nov 01 '10 at 16:42
  • 1
    @Vahagn: correct. Even though "if" uses the same code as expr under the hood, they are separate commands. Same goes for the "for" and "while" commands, though the use of "eq" and "ne" in those is pretty rare. – Bryan Oakley Nov 01 '10 at 17:09
1

I'm not even sure this code works properly in tcl 8.5

First make sure your expr is between curly brackets:

% expr {"a" eq "b"}

The alias allows you to create an alias for a command, but eq is not a command, it´s a subcommand of the expr command, so you would really need to create your own proc expr args wrapper.

Why do you need to use tcl 8.3? it's a really old version.

Carlos Tasada
  • 4,438
  • 1
  • 23
  • 26
0

Tcl 8.3 is rather old, and the recommended path for upgrading from TclPro (as I see you're using from comments) to something less… antediluvian… is to purchase the Tcl Dev Kit from ActiveState, which is at least the spiritual successor and has current support. Nobody supports TclPro any more.


That said…

Now, with Tcl 8.3 the equivalent of the functionality of the eq operator was the string equal command (in 8.4, they actually compile to the same bytecode; one is a short-cut for the other). This means that for this:

if {"a" eq "b"} {...

would be instead written as this:

if {[string equal "a" "b"]} {...

though if you've definitely got a non-integer for one of the arguments, you can just use this:

if {"a" == "b"} {...

There is no mechanism for aliasing operators in any version of Tcl (well, certainly not up to 8.6, which is the current development/beta version).


The Gripping Hand

As Bryan says, you can write your own versions of expr (and if and for and while) that have this extended functionality, but that's a lot of work (i.e., months including all the testing, though you can probably reuse the test suite from Tcl 8.4 to help) and it's just so you can use a version of Tcl that is known to be obsolete. (I have no idea if it has security issues in it, and you can't pay me enough for me to want to try to find out.) You're better off paying for the TDK.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215