proc evalCode {b} {
set i 0
while {$i < 1} {
if {[regexp {(.*)\x3C\x3F\x20(.*)\x20\x3F\x3E(.*)} $b a b c d]} {
if {[info exists return]} {
set return "[eval $c]$d$return"
} else {
set return "[eval $c]$d"
}
} else {
set return $b$return
set i 1
}
}
return $return
}
Quick and dirty way using 'string first' (could be improved by making it accept other kinds of whitespace after/before the "embedded code tags"
Executing the code in the scope of the proc is probably not what you want - I moved the execution to the global namespace using 'uplevel'
(I use a global variable for the result to be able to append data from within the embedded code...)
The result you got was what you asked for. My version appends what ever the evaluation of the code block returns to the return value, so if you want to OVERWRITE the return variable directly like that, make sure you return an empty value at the end of the code. Take a look at my echo proc (or USE it?)
Keeping the whitespace around the code inside the code tag (if that's what you meant) makes no sense...if you want whitespace, add it from within the code or outside the tag.
Here's another proc that does the same thing but looks better (and it also works with different kinds of whitespace around the code)
Ofloo wrote:i wana read a page like php but then tcl but not overwrite the procs variable when evaluating them..
Something like this?
Your httpd recieves a request.
You extract the filename from the request uri to determine how to proceed.
If the file exists and is of a type where embedded tcl should be executed...
1) create environment to execute the code in (child namespace or interpreter) and environment variables you find useful (get/post variables, cookies etc.)
2) evaluate the embedded code in this new environment (appending output to output buffer) and output the end result to your client.
3) destroy environment
i am trying to understand name space for a while thing i could understand from it was that it was like a script where you put crap in then if you need it you can call upon it and execute the code later on ..
the name space part is a bit where i am stuck on not sur how to use it ..
i think i understand how to use echo and i like it besides the fact that you make use of global variables but then again it won't matter when you work with name spaces .. i have no clue what void is supposed to do .. what i understood from uplevel is that it is like a pointer in C .. could be wrong there tho..
i am currently looking for a good example of how to use name spaces .. i remember i had a website of wiki something for syntax in tcl but lost it while i installed bsd i axidently format everything :/ to use the whole drive was a and quit was q and having a azerty keyboard those 2 keys are switched so when i wanted to quit i was removing the hd .. anyway and i remember you wrote something like a word counter .. or something .. that i remember was with name spaces ..
I don't know if this is a good namespace example (surely not better than some professional stuff you'd find on wiki.tcl.tk and elsewhere - after all, I'm primarily C/C++ developer and not scripter), but my ircd bridge/emulator for eggdrop uses namespace and uplevel
hmm its nice script.. but all i can make up from that script is that name spaces won't allow to over write global variables from one to an other script .. like writing all global variables into an array.. then export only the array..
yep, that was my reasoning; since I had a lot of vars & procs with pretty common names that would otherwise clash with other scripts (even with eggdrop's build-in names) and I didn't feel like introducing a common prefix and delimiter, using namespace was the obvious alternative (albeit a simplistic use; namespaces can be nested and you can build namespace hierarchies)