-----------------------------------------------------------------------------------------------------
Why namespaces? If you've ever had issues with global variables or procedures in your script conflicting with other scripts, you need namespaces. Namespaces are the closest that Tcl gets to object oriented programming. They allow the programmer to place variables and procedures inside one neat named-package in Tcl so as not to conflict with anything in the global namespace.
Lets Begin!
Code: Select all
namespace eval MyScript {
variable response "Hello World!"
bind pub - "hi" MyScript::respond
proc respond {nick uhost hand chan text} {
variable response
puthelp "PRIVMSG $nick :$response"
return 1
}
}
Code: Select all
catch MyScript::uninstall
namespace eval MyScript {
variable response "Hello World!"
bind pub - "hi" MyScript::respond
proc respond {nick uhost hand chan text} {
variable response
puthelp "PRIVMSG $nick :$response"
return 1
}
bind evnt - prerehash MyScript::uninstall
proc uninstall {args} {
unbind pub - "hi" MyScript::respond
unbind evnt - prerehash MyScript::uninstall
namespace delete MyScript
}
}
Code: Select all
set ns "MyScript"
catch ${ns}::uninstall
namespace eval $ns {
unset ::ns
variable response "Hello World!"
bind pub - "hi" [namespace current]::respond
proc respond {nick uhost hand chan text} {
variable response
puthelp "PRIVMSG $nick :$response"
return 1
}
bind evnt - prerehash [namespace current]::uninstall
proc uninstall {args} {
unbind pub - "hi" [namespace current]::respond
unbind evnt - prerehash [namespace current]::uninstall
namespace delete [namespace current]
}
}
All Done!
-----------------------------------------------------------------------------------------------------
Edit: Replaced [set] commands with [variable] inside namespace after reading user's tip.