<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://forum.eggheads.org/app.php/feed/topic/12900" />

	<title>egghelp/eggheads community</title>
	<subtitle>Discussion of eggdrop bots, shell accounts and tcl scripts.</subtitle>
	<link href="https://forum.eggheads.org/index.php" />
	<updated>2009-02-28T13:21:50-04:00</updated>

	<author><name><![CDATA[egghelp/eggheads community]]></name></author>
	<id>https://forum.eggheads.org/app.php/feed/topic/12900</id>

		<entry>
		<author><name><![CDATA[nml375]]></name></author>
		<updated>2009-02-28T13:21:50-04:00</updated>

		<published>2009-02-28T13:21:50-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=87615#p87615</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=87615#p87615"/>
		<title type="html"><![CDATA[Namespaces]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=87615#p87615"><![CDATA[
When dealing with namespaces, and making sure bindings work correctly, I'd create a separate namespace called eggdrop with a replacement for the bind function, utilizing the <strong class="text-strong">namespace code</strong> along with <strong class="text-strong">uplevel</strong> to capture the actual namespace. No more hazzle with <strong class="text-strong">namespace current</strong>..<br>Keep in mind that your binds will look a little messy however.<br><div class="codebox"><p>Code: </p><pre><code>namespace eval eggdrop {#First export functions for easy access: namespace export bind proc bind {type flag mask proc} {  ::bind $type $flag $mask [uplevel 1 [list namespace code $proc]] }}</code></pre></div>To use this, you'd simply do something like this:<div class="codebox"><p>Code: </p><pre><code>namespace eval myspace { #import the namespace along with bind, so we don't have to use the long version ::eggdrop::bind namespace import ::eggdrop::* proc myproc {nick host handle text} {  puthelp "PRIVMSG $nick :Hello $nick! I see you from $host, know you as $handle, and recieved $text from you." } bind msg - foo myproc}</code></pre></div><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8052">nml375</a> — Sat Feb 28, 2009 1:21 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[user]]></name></author>
		<updated>2006-12-30T11:59:57-04:00</updated>

		<published>2006-12-30T11:59:57-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=69361#p69361</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=69361#p69361"/>
		<title type="html"><![CDATA[slight error]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=69361#p69361"><![CDATA[
To create a variable inside a namespace you have to use 'variable', not 'set'. If a variable by that name exists in a parent namespace, you'll change the existing one outside your namespace (which defeats the purpose of this whole name conflict avoiding thing <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_razz.gif" width="15" height="15" alt=":P" title="Razz">)<div class="codebox"><p>Code: </p><pre><code>% set test 1; namespace eval test {set test 2}; set test2% set test2 1; namespace eval test2 {variable test2 2}; set test21</code></pre></div>You can assign a value to scalar variables using 'variable' (like i did in test 2), but if your variable is an array, do 'variable theArray', then 'set' / 'array set' to create elements. Just make sure you use 'variable' the first time you address a variable inside your namespace if you want to make sure you're not messing with parent namespaces.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=2878">user</a> — Sat Dec 30, 2006 11:59 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[Sir_Fz]]></name></author>
		<updated>2007-01-11T22:35:54-04:00</updated>

		<published>2006-12-30T09:33:09-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=69356#p69356</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=69356#p69356"/>
		<title type="html"><![CDATA[Namespaces]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=69356#p69356"><![CDATA[
<em class="text-italics">Written by <strong class="text-strong">BarkerJr</strong>. Source: <a href="http://www.barkerjr.net/irc/eggdrop/Scripting/namespaces.php" class="postlink">http://www.barkerjr.net/irc/eggdrop/Scr ... spaces.php</a>.</em><br>-----------------------------------------------------------------------------------------------------<br><br>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.<br><br><br>Lets Begin!<div class="codebox"><p>Code: </p><pre><code>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></pre></div>MyScript is the name of the namespace.  This will be the prefix used in the name of variables and procedures if they are being called from other namespaces (such as the global).  Notice that the bind specifies the full name in the procedure parameter. Another thing to note is that the variable $response resides within the namespace.  That means it's not a global variable.  So, inside the procedure, it is declared as a variable, rather than global.  Note that variables must be declared on separate lines if you have more than one.  They cannot be declared in one long line like globals.<div class="codebox"><p>Code: </p><pre><code>catch MyScript::uninstallnamespace 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></pre></div>Rather than forcing users to .restart to uninstall scripts, lets uninstall on rehash.  If the user still has the script in the conf, it'll be loaded automatically after the rehash.  First of all, binds are not directly associated with the namespace, so they have to be unbound separatly.  Then the namespace itself can be deleted.  Don't forget to kill any timers that may be in your script here, too.  Also, in order to also uninstall/reinstall for users that .tcl source the script, add the top line to call the uninstall procedure.  The catch is there so that the bot doesn't crash if the uninstall procedure doesn't exist yet (first run).<div class="codebox"><p>Code: </p><pre><code>set ns "MyScript"catch ${ns}::uninstallnamespace 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]  }}</code></pre></div>If you are worried about the the namespace possibly conflicting with another namespace, do something like this.  First, set the namespace in a global variable.  It's only used in the first two lines, so we unset the global variable after, so as not to clutter up the global namespace.  After that, [namespace current] should be used anywhere MyScript was used.  Doing this will make it practically impossible for your script to conflict with someone else's.<br><br><br>All Done!<br>-----------------------------------------------------------------------------------------------------<br>Edit: Replaced [set] commands with [variable] inside namespace after reading user's tip.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=3085">Sir_Fz</a> — Sat Dec 30, 2006 9:33 am</p><hr />
]]></content>
	</entry>
	</feed>
