Version 5 of Writing case insensitive procedures using unknown

Updated 2010-03-22 16:44:28 by AMG

Case Insensitive TCL commands

MaheshD: Some days back I faced an issue with writing case insensitive procedure names.

    proc abc {} {
        puts "Called function abc() .."
    }

This should work with other 7 variants ABC, abC, aBc, aBC, Abc, AbC, aBc.

The solution to this could be to use the unknown function in TCL.

# define the original command name list.

    set orig_cmd_names [list abc]

# Rename the original unknown to unknown_orig

    rename unknown unknown_orig

# Define a customized unknown procedure.

    proc unknown {args} {
        set cmd [lindex $args 0]
        global orig_cmd_names
        foreach oCmd $orig_cmd_names {
            if {[string equal -nocase $oCmd $cmd]} {
                interp alias "" $cmd "" $oCmd
                tailcall $oCmd {*}[lrange $args 1 end]
            }
        }
        # Not found .. :-(, call the base unknown function.
        unknown_orig $args
    }

    %abC
    Called function abc() ..
    %Abc
    Called function abc() ..

MG would recommend using string equal instead of regexp to compare the strings, otherwise you could get inaccurate matches when command names contain chars with special meaning for regexp (proc a..z).

MJ would recommend to install interp aliases for the other capitalisations as well so subsequent calls to the different capitalisation don't invoke unknown. Executing the command in global scope is probably an error anyway. It will fail nicely if the original command uses uplevel or upvar for instance.

AMG: This sounds like a job for tailcall!! Also I implemented MG's and MJ's suggestions.