package provide extend 1.0
package require Tcl 8.5
# extend a command with a new subcommand
proc extend {cmd body} {
if {![namespace exists ${cmd}]} {
set wrapper [string map [list %C $cmd %B $body] {
namespace eval %C {}
rename %C %C::%C
namespace eval %C {
proc _unknown {junk subc args} {
return [list %C::%C $subc]
}
namespace ensemble create -unknown %C::_unknown
}
}]
}
append wrapper [string map [list %C $cmd %B $body] {
namespace eval %C {
%B
namespace export -clear *
}
}]
uplevel 1 $wrapper
}Here's the file command extended with newer and newerthan subcommands:
extend file {
proc newer {a b} {
return [expr {[file mtime $a] > [file mtime $b]}]
}
proc newerthan {mtime path} {
return [expr {[file exists $path] && ([file mtime $path] > $mtime)}]
}
}Here's the dict command extended with the modify subcommand: # extra useful dict commands
extend dict {
proc modify {var args} {
upvar 1 $var dvar
foreach {name val} $args {
dict set dvar $name $val
}
}
}LV So, this seems like a nice bit of functionality. Would it be useful enough to include either in Tcl itself or at least Tcllib?
quick hacks ?
AMG: See also my [dict getnull] example in [dict get].
