Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to copy a proc

5 views
Skip to first unread message

newtop...@yahoo.com

unread,
Sep 24, 2006, 8:45:26 AM9/24/06
to
I need to duplicate an existing proc once my program starts running.
This is easy enough to do with info args/info body commands.

However, this does not work if the original proc is compiled. Rename
does not work either, since I would need to make a copy of the
original, and leave the original intact.

Is there a way to achieve this?

Eric Hassold

unread,
Sep 24, 2006, 8:54:03 AM9/24/06
to
newtop...@yahoo.com wrote :

You may find "interp alias" useful (see interp manpage). In your case:

interp alias {} newname {} procname

where {} refer to current interpreter. E.g.:

% interp alias {} print {} puts
print
% print hello
hello

Note however this is not exactly a copy, but an alias. If original
procedure is deleted or modified, newname will be modified/undefined either.

Eric

-----
Eric Hassold
Evolane - http://www.evolane.com/

Bryan Oakley

unread,
Sep 24, 2006, 10:27:06 AM9/24/06
to

When you say "copy" or "duplicate", what exactly are you trying to do?
Do you just need identical functionality with a different name? Do you
plan on modifying the copy?

In other words, do you truly need a copy or can other solutions work?
For example, you can use interp aliases to use more than one name, or
command overloading (essentially, renaming and then writing your own
wrapper to call the original) to add new functionality.


--
Bryan Oakley
http://www.tclscripting.com

newtop...@yahoo.com

unread,
Sep 24, 2006, 12:53:13 PM9/24/06
to
Eric Hassold wrote:
> You may find "interp alias" useful (see interp manpage). In your case:
>
> interp alias {} newname {} procname
>
> where {} refer to current interpreter. E.g.:
>
> % interp alias {} print {} puts
> print
> % print hello
> hello


This is exactly what I was looking for. Thanks a lot!

newtop...@yahoo.com

unread,
Sep 24, 2006, 12:58:54 PM9/24/06
to
Bryan Oakley wrote:
> When you say "copy" or "duplicate", what exactly are you trying to do?
> Do you just need identical functionality with a different name? Do you
> plan on modifying the copy?
>
> In other words, do you truly need a copy or can other solutions work?
> For example, you can use interp aliases to use more than one name, or
> command overloading (essentially, renaming and then writing your own
> wrapper to call the original) to add new functionality.

What I was looking for was essentially command overloading. I did not
want to define a wrapper function primarily because this particular one
gets called a lot within a double nested loop of matrix calculations.
It works just fine now, and I was worried about a negative impact on
efficiency, which may or may not be valid at this early stage.

Interp aliases seem to do exactly what I wanted.

Thanks!

Donal K. Fellows

unread,
Sep 24, 2006, 7:58:11 PM9/24/06
to
Eric Hassold wrote:
> You may find "interp alias" useful (see interp manpage). In your case:
> interp alias {} newname {} procname

There is one other way to achieve this effect (with all the same caveats
but a slightly different performance profile):

namespace export procname ;# If you haven't already
namespace eval ::tmp { ;# This is arbitrary
namespace import ::procname
rename procname ::newname
namespace delete [namespace current]
}

Note that with this sort of alias, if you delete the original then the
alias completely vanishes as well. This is distinct from the kind of
aliases created with [interp alias].

Donal.

Donal K. Fellows

unread,
Sep 25, 2006, 4:48:58 AM9/25/06
to
Donal K. Fellows wrote:
> namespace eval ::tmp { ;# This is arbitrary

Ugh, I should review the comments more carefully before posting. I meant
to say that the namespace name is arbitrary, but it reads like the whole
line of code is totally arbitrary. Goes to show I shouldn't post at that
time of night. (The code itself is correct though.)

Donal.

newtop...@yahoo.com

unread,
Sep 25, 2006, 12:56:18 PM9/25/06
to

Thanks for the clarification. I have not done much with namespaces
yet, but I will keep this in mind for new projects.

Michael A. Cleverly

unread,
Sep 25, 2006, 11:20:09 PM9/25/06
to
On Sun, 24 Sep 2006, Donal K. Fellows wrote:

> There is one other way to achieve this effect (with all the same caveats
> but a slightly different performance profile):
>
> namespace export procname ;# If you haven't already
> namespace eval ::tmp { ;# This is arbitrary
> namespace import ::procname
> rename procname ::newname
> namespace delete [namespace current]
> }
>
> Note that with this sort of alias, if you delete the original then the
> alias completely vanishes as well. This is distinct from the kind of
> aliases created with [interp alias].

Wow. I've never thought of that. I learned something mind-alteringly
cool and I'm not even in Naperville yet. :-D

Michael

Donal K. Fellows

unread,
Sep 26, 2006, 4:35:21 AM9/26/06
to
Michael A. Cleverly wrote:
> Wow. I've never thought of that. I learned something mind-alteringly
> cool and I'm not even in Naperville yet. :-D

I wish I'd been the one to invent it, but I actually copied the
technique out of the Tk test suite. :-)

Donal.

0 new messages