<?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/15420" />

	<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>2008-02-25T07:34:37-04:00</updated>

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

		<entry>
		<author><name><![CDATA[user]]></name></author>
		<updated>2008-02-25T07:34:37-04:00</updated>

		<published>2008-02-25T07:34:37-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=81145#p81145</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=81145#p81145"/>
		<title type="html"><![CDATA[upvar]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=81145#p81145"><![CDATA[
You can also give upvar an absolute level number (#0 for the global scope) <div class="codebox"><p>Code: </p><pre><code>upvar #0 globalvarName localvarName</code></pre></div>...or a fully qualified variable name (making the level irrelevant) <div class="codebox"><p>Code: </p><pre><code>upvar ::ns1::ns2::ns3::namespaceVar localVar</code></pre></div>If you create a proc that gets the name of the variable to import from an external source, make sure you specify a level, or you could get in trouble (if the variable name is an integer)<div class="codebox"><p>Code: </p><pre><code>proc myset {var val} {upvar 1 $var Var; set Var $val}</code></pre></div><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=2878">user</a> — Mon Feb 25, 2008 7:34 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[nml375]]></name></author>
		<updated>2008-02-23T11:08:42-04:00</updated>

		<published>2008-02-23T11:08:42-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=81090#p81090</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=81090#p81090"/>
		<title type="html"><![CDATA['static' vars, upvar and uplevel]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=81090#p81090"><![CDATA[
Upvar accesses variables in the proc that called the current one (or global if this is the first level of call). Uplevel executes a piece of tcl-code in the same level as above. Using the <em class="text-italics">level</em> argument with either allows you to move several levels upward in the function call stack.<br><br>A quick demonstration of upvar:<div class="codebox"><p>Code: </p><pre><code>proc sub1 {} { set myvar "Foo" puts stdout "sub1: myvar is $myvar" sub2 puts stdout "sub1: myvar is $myvar"}proc sub2 {} { upvar myvar thevar if {[catch {puts stdout "sub2: myvar is $myvar"} error]} {  puts stdout "sub2: Could not read local \$myvar: $error" } puts stdout "sub2: thevar is $thevar" set thevar "Bar" puts stdout "sub2: Setting thevar to $thevar"}sub1</code></pre></div><blockquote class="uncited"><div>sub1: myvar is Foo<br>sub2: Could not read local $myvar: can't read "myvar": no such variable<br>sub2: thevar is Foo<br>sub2: Setting thevar to Bar<br>sub1: myvar is Bar</div></blockquote>Here I use upvar in sub2 in order to access, and modify, the value of "myvar" which only exists in sub1. This is possible since sub2 was called by sub1, and moving one step upwards in the function call stack would place us in sub1, giving us access to the variables here. As sub2 exits, the link between sub1-&gt;myvar and sub2-&gt;thevar is broken while sub1-&gt;myvar retains its value. As sub1 exits, it's variablespace is wiped and myvar is lost.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8052">nml375</a> — Sat Feb 23, 2008 11:08 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[incith]]></name></author>
		<updated>2008-02-23T00:37:23-04:00</updated>

		<published>2008-02-23T00:37:23-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=81083#p81083</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=81083#p81083"/>
		<title type="html"><![CDATA['static' vars, upvar and uplevel]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=81083#p81083"><![CDATA[
If you did something like..<div class="codebox"><p>Code: </p><pre><code>proc myProc {} {  set last_result ""  # fails already, last_result is not going to be remembered}</code></pre></div>The static proc does basically push variables into the namespace.  It seems useful to me, but I do not understand upvar and uplevel so I'm not sure how it works, or it's the best solution.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=6130">incith</a> — Sat Feb 23, 2008 12:37 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[user]]></name></author>
		<updated>2008-02-22T11:18:43-04:00</updated>

		<published>2008-02-22T11:18:43-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=81065#p81065</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=81065#p81065"/>
		<title type="html"><![CDATA['static' vars, upvar and uplevel]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=81065#p81065"><![CDATA[
I'm not sure I get what you want. Do you want the variable to be saved, to keep its value even if the interpreter is restarted or are you just looking for a way to use global variables inside a namespace?<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=2878">user</a> — Fri Feb 22, 2008 11:18 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[incith]]></name></author>
		<updated>2008-02-22T09:53:12-04:00</updated>

		<published>2008-02-22T09:53:12-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=81063#p81063</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=81063#p81063"/>
		<title type="html"><![CDATA['static' vars, upvar and uplevel]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=81063#p81063"><![CDATA[
Well, all my scripts use namespaces and variables already.  With this you don't have to setup variables for use later on at the beginning.. <br><br>This works well for quick things, not that variables may or may not.<br><br>Other applicable uses for static variables might include something like:<br><div class="codebox"><p>Code: </p><pre><code># never send the same result twice, barebones examplestatic last_resultwhile {$::last_result eq $cur_result} {  cur_result = [fetch_result]}set "::last_result" $cur_result</code></pre></div><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=6130">incith</a> — Fri Feb 22, 2008 9:53 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[metroid]]></name></author>
		<updated>2008-02-22T09:16:53-04:00</updated>

		<published>2008-02-22T09:16:53-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=81061#p81061</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=81061#p81061"/>
		<title type="html"><![CDATA['static' vars, upvar and uplevel]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=81061#p81061"><![CDATA[
Why don't you just use variable?<br><br>AFAIK, variable isn't reset after a rehash.<br><div class="codebox"><p>Code: </p><pre><code>namespace eval somens {    variable moo 1}</code></pre></div><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=5078">metroid</a> — Fri Feb 22, 2008 9:16 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[incith]]></name></author>
		<updated>2008-02-22T00:14:23-04:00</updated>

		<published>2008-02-22T00:14:23-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=81049#p81049</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=81049#p81049"/>
		<title type="html"><![CDATA['static' vars, upvar and uplevel]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=81049#p81049"><![CDATA[
This post is meant for those that can explain this crap to me. <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><br><br>I wanted to make use of static variables in a script recently, that is, vars that will not get reset once the script is re-ran, etc.  Basically I went and searched, discovered static vars do not exist in Tcl, and then found this code: <a href="http://phaseit.net/claird/comp.lang.tcl/tcl-examples.html#static" class="postlink">http://phaseit.net/claird/comp.lang.tcl ... tml#static</a><div class="codebox"><p>Code: </p><pre><code>    proc static {args} {      # set procName [lindex [info level [expr [info level]-1]] 0]      set procName "incith::test"      # putlog "procName is (${procName})\n"      foreach varName $args {        uplevel 1 "upvar #0 {$procName:$varName} $varName"      }    }</code></pre></div>A quick usage example of this would be:<div class="codebox"><p>Code: </p><pre><code>namespace eval incith {  namespace eval test {    proc myProc {} {      static count      if {![info exists "incith::test:count"]} {        set "incith::test:count" 1      }      incr "incith::test:count"      putlog "count is now ${count}\n"    }  }}</code></pre></div>Declaring the 'static count' variable pushes the count var into the incith::test namespace and as such will not be forgotten.<br><br>As you can see I modified the set procName, which I'll now explain.  When I first put it into a test script, the test script was under the same namespace above, <em class="text-italics">namespace eval incith { namespace eval test {</em>, and $procName became incith::test::PROC_NAME:var, where PROC_NAME was the calling procedure, e.g. incith::test::myProc:count based on the above example.  Then I went and added proc static into my XRL script and procName would not fetch the namespace it was in, but instead it would only get the procedure name, and e.g. store the var as "myProc:count", this was baffling to me, which is why I just ended up forcing set procName to my namespaces.<br><br>The questions in all of this, is this really necessary?  Is there some extra work being done?  The point is just to not forget variables basically, so that when the proc is re-run it knows what a certain variable it's using was since the last call, and so on.  It works, at least, but I can't help but feel that it's just plain wrong.  It's also dang annoying to be typing out 60 char variable names with the namespaces etc.  Discuss!<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=6130">incith</a> — Fri Feb 22, 2008 12:14 am</p><hr />
]]></content>
	</entry>
	</feed>
