On Sunday 20 April 2008 04:47, Bas Scheffers wrote:
> More information on scope here: http://wiki.tcl.tk/11921
>
> In general, creating variables inside a procedure and then using
> global or upvar/uplevel to make them available once the procedure
> returns is pretty bad form.

It might be more exact to say that you should fully understand why you are 
using either uplevel or upvar, and what the possible alternatives are. 

The only way to either use or return an array from a procedure is to use 
upvar. But there are good and bad ways to do it. The good way is to pass the 
name of the array in to the procedure so that you have control of exactly 
what the name of the array is going to be:

proc create_myarray { arrayName } {
    upvar $arrayName localArray
    array set localArray {a 1 b 2}
}

proc create_mystery_array { } {
    upvar A A
    array set A {a 1 b 2}
}

But also notice that the equivalent of upvar is used in regular tcl commands:

% set mylist a
% lappend mylist b

If [lappend] didn't exist, you could do:

proc lappend { listName args} {
    upvar $listName localList
    set localList [concat $localList $args]
}

tom jackson


> I would recommend you use the return value of the proc for the value
> you want in your ADP, like:
>
> <%
> proc foo {} {
>       return "bar"
> }
>
> set value [foo]
> %>
> <%= $value %>
>
> You should also consider having procedure creating parts of your HTML,
> which can be made easy using subst:
>
> <%
> proc createBox {title contents} {
>       return [subst {
>               <div class="boxTitle">$title</div>
>               <div class="boxContents">$contents</div>
>       }]
> }
> %>
> <%= [createBox "It's all about scope" "blah blah blah" ] %>
>
> The use of subst here makes it possible to not escape the " character
> and still have the variables included.
>
> While I do not use a templating system, virtually all my HTML is
> created using procedures like the one above that I put in Tcl libraries.
>
> And all of the data from the database I get using other procedures
> that only select from the database and put it in a structure I can
> pass into one of the HTML procedures. My ADP pages usually have not
> much more than 10 to 20 lines in them; it makes everything easy to
> maintain and bug-free and re-using html and logic throughout the site
> is very easy.
>
> Hope that gives you some inspiration!
>
> Bas.
>
>
> --
> AOLserver - http://www.aolserver.com/
>
> To Remove yourself from this list, simply send an email to
> <[EMAIL PROTECTED]> with the body of "SIGNOFF AOLSERVER" in the
> email message. You can leave the Subject: field of your email blank.


--
AOLserver - http://www.aolserver.com/

To Remove yourself from this list, simply send an email to <[EMAIL PROTECTED]> 
with the
body of "SIGNOFF AOLSERVER" in the email message. You can leave the Subject: 
field of your email blank.

Reply via email to