Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL how to call item handlers

Tags:

tcl

I created the map from the item to item handler.

  array set handlers {
             handleItem1 handlerFunction1
             handleItem2 handlerFunction2
  }

But handlerFunctions could take parameter.

So below code is not gonna be work.

 if { [info exists handlers($item) ] } {
       eval $this $handlers($item)
 }

So could I chnage this code to be able to call the handlers which could take the parameters?

like image 627
Vardan Hovhannisyan Avatar asked May 29 '13 13:05

Vardan Hovhannisyan


People also ask

How do I return a TCL script?

Error return: same as if the error command were used to terminate the procedure, except for handling of errorInfo and errorCode variables (see below). return. The current procedure will return with a completion code of TCL_RETURN, so that the procedure that invoked it will return also. break.

What are special variables in TCL?

Refers to the current patch level of the Tcl interpreter. 11. tcl_platform. Used for representing the array of elements with objects including byteOrder, machine, osVersion, platform, and os.

What is after in TCL?

The after command returns an identifier that can be used to cancel the delayed command using after cancel. after cancel id. Cancels the execution of a delayed command that was previously scheduled. Id indicates which command should be canceled; it must have been the return value from a previous after command.


1 Answers

There are several options:

  • Command prefix If you are using Tcl 8.5 or later (8.4 is at the end of it's lifetime, upgrade if possible) then the command expansion with {*} is the best way to do that.

    proc putargs args {puts $args}
    set callback {putargs CALLBACK}
    # Invoke it
    {*}$callback param1 param2 ;# prints CALLBACK param1 param2
    
  • Script fragment Eval the callback, but add extra words at the end. (trace does that)

    proc putargs args {puts $args}
    set callback {putargs CALLBACK [clock seconds]}
    # Invoke
    eval $callback [list param1 param2] ;# Prints CALLBACK 1369834114 param1 param2
    

    Allows hacks like set callback {puts "CALLBACK" ;#} which will ignore the rest of the line, but it is slower.

  • Command Not very useful because you can not pass anything.

    proc putargs args {puts $args}
    set callback {putargs CALLBACK}
    # Invoke
    {*}$callback ;# prints CALLBACK
    
  • Script The parameter are passed as variables in the current scope, sometimes also with string substitution (Tk does that)

    proc putargs args {puts $args}
    set callback {putargs CALLBACK $param1 $param2}
    # Invoke
    set param1 FOO
    set param2 BAR
    eval $callback ;# prints CALLBACK FOO BAR
    
  • Command name Like the Command prefix, but no expansion is done. (SASL of the Tcllib does that for own mechanisms). Not recommended.

    proc putargs args {puts $args}
    set callback putargs
    # Invoke
    $callback param1 param2
    

The best solution with the best performance is the command prefix.

like image 57
Johannes Kuhn Avatar answered Sep 20 '22 01:09

Johannes Kuhn