Substitution Strings on the Tcl Side

When events are handled in Tcl/Gnocl scripts can be called into which variable substitutions can take place using percent-strings. The way of doing this draws inspiritation from the use of such patterns in the C language functions printf and scanf.

In Tickle these patterns can be arbitrary, depending upon the context requiring the substitution.  Here's a sample from some expiremental coding. Proceedure myObj creates an object, and associates data with the in the form of a series of arrays
which holds data for each instance of myObj.

In the following example, any instance of myObj has one simple action,



# playing substition strings...

# call back function
proc myCallBack {cmd args} {puts $args}

# object creation counter
set Objs 0

# object builder
proc myObj {} {

    set id obj_[incr ::Objs]

    # initialize empty data
    set ::eng($id) ""
    set ::cback($id) ""
    set ::con($id) ""
    set ::src($id) ""
    set ::tgt($id) ""
   
    proc ::$id {} {
        set id [lindex [::info level 0] 0]
        subExec $id
    }
   
    return $id
}

    # create an instance, and assign some values
    set id [myObj]

    set ::eng($id) AAAAA_1
    set ::cback($id) "myCallBack add %s %t %c"
    set ::con($id) "我即以神足往告釋梵護世"
    set ::src($id) 神
    set ::tgt($id) magical

# link this to some event trigger..
proc subExec {id} {

    set subStr [list %e $::eng($id) %c $::con($id) %s $::src($id) %t $::tgt($id)]
    {*}[string map $subStr $::cback($id) ]
}

$id



Comments