Forum OpenACS Q&A: Defining ad_procs within a script?

Collapse
Posted by Jade Rubick on
On an ACS 3.4.10 installation...

Is it bad to use ad_proc within a normally executed .tcl page? I
define an ad_proc that is used by a procedure within just that page.

Collapse
Posted by Don Baccus on
Hmmm...it will probably be recompiled each time the script's run, rather than just once when it's loaded as would be the case if it were in the library.

I'm not sure, though.  There's a Rob Mayoff hack in AOLserver 3.3+ad13 that save compiled bytes for the top-level script itself, but I think embedded procs may get recompiled (because "proc" itself is just a command, after all).

Does anyone know?

Collapse
Posted by Carl Coryell-Martin on
There is another issue if you are using the ATS.  In some contexts pages are run in the template namespace and a proc will be defined in that space and hang around in the thread.  I read in some of the docs for the ATS that defining procs in .tcl pages was not reccommended.

cheers,

Collapse
Posted by Tom Jackson on

You can do this for testing purposes, but once the proc works you should move it out of the page. Once defined, the proc probably will remain in the thread's interp, so as long as that interp remains alive, the proc you define in one page will be there if the same thread processes a different request. So it would be bad if this proc was overwriting a proc that was defined at startup, if the proc was used on more than the one page you redefine it on.

Collapse
Posted by David Walker on
If you're just looking to reuse a piece of code in a bunch of different places you could also use a tcl string for that as shown below

set code_to_reuse {
 ns_write $var1
 ns_write $var2
}

set var1 xxx
set var2 yyy
eval $code_to_reuse
set var1 123
set var2 456
eval $code_to_reuse