I got this mostly working, but the results are interesting (or
incorrect?), and I wonder if you can explain why certain things
happened the way they did.

Attempt 1:

I first tried your recommendation to put the snippet you gave in my
config file.  This didn't work; it gave a fatal config error: invalid
command name "ns_ictl".

Attempt 2:
Next, I put the script in my private tcl library as the first file to
load.  The tcl library loaded fine, but then on hitting a .tcl page, I
got:

[06/Feb/2005:18:58:08][3063.25187840][-conn:web2-users::0] Error: can't
rename "proc": command doesn't exist
can't rename "proc": command doesn't exist
    while executing
"rename proc _proc"

I think this is because the rename operates more globally than does the
redefinition of proc, which you explained below is due to "faking" the
interp cloning process.

Attempt 3:
I modified the script so the rename only happens once, but the
definition of proc happens on every 'oncreate', as shown below.  This
worked, but I noticed that the ns_log that logs the proc names ran
twice.  First it ran when the tcl library was getting sourced.  Then,
when I hit a .tcl page, and I saw a notice for each proc again.  I
imagine this is also due to the faking of cloning, where AOLServer is
actually redefining each proc again when a new interpreter is created.
I also see the same behavior when I explicitly run 'ns_ictl update.'
(When I loaded a second tcl page, it did not happen; the same interp
was likely getting reused without update.)

The problem with this setup is that my new proc is getting used in the
cloning process rather than the actual one which has been renamed to
_proc.  The point of doing the rename/redefine is to wrap [proc] with
extra stuff.  That stuff -- which often has side-effects -- should only
happen once.  So now I'm in a bit of a bind.  How can I get all my code
to use my proc, and the faking-of-cloning process to use the real proc?
 It would be great if there was a 'ns_ictl aftercreate' command so the
tcl-defined [proc] can be used for the cloning and the user-defined
proc gets used for everything else.  Any ideas on how to fake this?

Also, I'm using AOLServer 4.0.9 with Tcl 8.4.9, though I would hope the
solution would not vary vastly from one version set to the next.

thanks,
--dan

------- code snippet 000-rename-proc.tcl -------------

set script {
    _proc proc {proc_name args} {
       set proc_namespace [uplevel {::namespace current}]
       if { $proc_namespace != "::" } {
          regsub {^::} $proc_namespace {} proc_namespace
          set proc_name "${proc_namespace}::${proc_name}"
       }

       ns_log notice "PROC: $proc_name"
       set procdef [list _proc $proc_name]
       eval [concat $procdef $args]
   }
}

ns_ictl oncreate $script

rename proc _proc
eval $script

------- code snippet 000-rename-proc.tcl -------------


---------------------------------------- dan chak web: http://www.chak.org

On Feb 6, 2005, at 5:45 PM, Dossy Shiobara wrote:

On 2005.02.06, Dan Chak <[EMAIL PROTECTED]> wrote:

I've noticed some strange behavior in AOLServer when renaming the proc command. I put the following example stub definition in the tcl library before the rest of the library loads:

What version of AOLserver? Did you put it in the shared Tcl library, or the private Tcl library?

My tcl library loads fine, but when I try to load a page, I get the
following errors in my log (and a page error):

[06/Feb/2005:14:39:01][2824.25187840][-conn:web2-users::0] Error:
invalid command name "proc"
invalid command name "proc"
    while executing
"proc ::load {args} {
            set libfile [lindex $args 0]
            for {set i 0} {$i < [llength $::_pkglist]} {incr i} {
                set to..."
[06/Feb/2005:14:39:01][2824.25187840][-conn:web2-users::0] Error:
invalid command name "ns_sourceproc"
invalid command name "ns_sourceproc"
    while executing
"ns_sourceproc {}"

Looks like you've got enabletclpages = true, and this is a request for a .tcl page.

It seems like the 'rename' and the definition of the new proc command
happen in different contexts, the latter of which is not seen by the
main interpreter process.  Any ideas on how to get this working?

Well, since Tcl doesn't have a "clone this interp" capability, we kind of "fake it" in AOLserver. We create a "master interp" which sources all the Tcl at start-up, then dump its contents as best we can (which generally means capturing the proc definitions for all procs listed in [info procs]), building up a Tcl script used to initialize new interps. [ns_ictl get] will dump you a copy of the script that the server is using.

Of course, there's no way to ask a Tcl interp "tell me about all the
[rename]'s that have happened in this interp since it was created" so
that we could include them in the master init script.  (Thought: we
could rename the [rename] proc, replacing it with a wedge that logs
what
procs are being renamed then actually performing the rename, but we'd
need to do it in the right sequence in the master init script or else
we
stand a chance of breaking things.)

If you want to do things other than define procs in your Tcl libraries
at start-up which you want to have happen in all subsequent interps,
you
want to look at [ns_ictl oninit] or [ns_ictl oncreate], whichever is
appropriate.  In this particular case, you probably want [ns_ictl
oncreate], like so:

    ns_ictl oncreate {
        rename proc _proc
        _proc proc {name args body} {
            ns_log notice "proc: $name $args"
            _proc $name $args $body
        }
    }

Why is it important to rename AND redefine [proc] in the same oncreate
script block?  Because you want this to happen right after the interp
is
created, before the "master init script" is sourced.  If you define the
[proc] proc in your regular Tcl script, it'll get defined in the master
interp. (which may be what you want -- more on this later), and
captured
and put into the master init script.  Why is this bad?  Because your
new
definition of [proc] will likely come somewhere in that master init
script, but the [rename proc _proc] will happen at the very beginning.
So, if anything tries to define a proc in the master init script before
your new [proc] definition happens, well ... it'll throw a Tcl error
while it's sourcing.

So, what IF you want to rename the [proc] proc to affect how the rest
of
the startup Tcl is sourced, AND any future interps as well?  You may
want to try something like this:

    set script {
        rename proc _proc
        _proc proc {name args body} {
            ns_log notice "proc: $name $args"
            _proc $name $args $body
        }
    }
    ns_ictl oncreate $script
    eval $script

Of course, for maximum effect, you want to make sure that this snippet
is the first thing to get executed in the master interp.  You may want
to even put this in the config.tcl rather than in the Tcl library to
ensure it gets sourced before anything in either of the Tcl libraries,
but I haven't tested this.

HTH,

-- Dossy

--
Dossy Shiobara                       mail: [EMAIL PROTECTED]
Panoptic Computer Network             web: http://www.panoptic.com/
  "He realized the fastest way to change is to laugh at your own
    folly -- then you can let go and quickly move on." (p. 70)


-- 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