modify a proc's behavior with a shim

JBR 2010-07-12

I have a lot of code that writes code. Sometimes the "standard" code that gets written from the config files is not exactly the right thing for some specific case. When this happens I usually modify the standard proc by wrapping a "shim" around it. This little bit of code makes this less crappy. It is less general than the code in stacking but works for me.

 proc shim:shift { name } {
    if { [info commands ${name}_shimmed] ne {} } { shim:shift ${name}_shimmed }
    rename $name ${name}_shimmed
 }

 proc shim { name args body } {
    shim:shift $name
    proc   $name $args $body
 }

 proc shim:next { args } { tailcall [lindex [info level -1] 0]_shimmed {*}$args }

Found another version of shim:next even more like stacking. Fixed up to use try instead of catch.

 proc shim:next { name args } {
    set level -1
    try {
        while { ![regexp "^${name}(_shimmed)*" [lindex [info level $level] 0]] } { incr level -1 }
    } on error message {
        error "cannot locate shim: $name"
    }

    [lindex [info level $level] 0]_shimmed {*}$args
 }

Using this kind of thing seems to be calling out for replacement with TclOO.