• Renaming a proc back to its original name in tcl script

    From Kavitha MS@21:1/5 to All on Sat Aug 12 10:16:32 2023
    Hi group,

    Below I have a proc called orig_proc and am renaming it to a new_proc. But once I rename it the orig_proc doesnt exist anymore. But the orig_proc is needed to be called by other procs also. So How do I rename the new_proc back to orig_proc

    proc orig_proc {} {
    puts "in original proc"
    }

    rename -force orig_proc new_proc

    proc orig_proc {} {
    eval new_proc // here instead of executing new_proc, I want new_proc to be renamed back to orig_proc
    m() // because m() calls the orig_proc in its definition and it runs into a recursive call which needs to be avoided, so I need new_proc to be renamed back to orig_proc
    }

    How to rename the new_proc back to orig_proc once again?
    In the first step I renamed the orig_proc, because I wanted m() also to be called in orig_proc's new definition. So i renamed it , but now within orig_proc redefinition i need it back to be renamed to its orig_proc name so that m() calls orig_proc and it
    doesnt end up in recursive call.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Leitgeb@21:1/5 to Kavitha MS on Sun Aug 13 21:51:51 2023
    Do you really need to play ping pong with the proc name, or do you
    rather want to be able to call the procedure by both names?

    Maybe it would help to create an alias of the procedure, so you can
    leave it in its own name, and still access it by another name, too:
    Look at https://wiki.tcl-lang.org/page/namespace and search for
    section "Make an Alias for a Command"


    Kavitha MS <kavitha.to@gmail.com> wrote:
    Hi group,

    Below I have a proc called orig_proc and am renaming it to a new_proc. But once I rename it the orig_proc doesnt exist anymore. But the orig_proc is needed to be called by other procs also. So How do I rename the new_proc back to orig_proc

    proc orig_proc {} {
    puts "in original proc"
    }

    rename -force orig_proc new_proc

    proc orig_proc {} {
    eval new_proc // here instead of executing new_proc, I want new_proc to be renamed back to orig_proc
    m() // because m() calls the orig_proc in its definition and it runs into a recursive call which needs to be avoided, so I need new_proc to be renamed back to orig_proc
    }

    How to rename the new_proc back to orig_proc once again?
    In the first step I renamed the orig_proc, because I wanted m() also to be called in orig_proc's new definition. So i renamed it , but now within orig_proc redefinition i need it back to be renamed to its orig_proc name so that m() calls orig_proc and
    it doesnt end up in recursive call.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kavitha MS@21:1/5 to Andreas Leitgeb on Sun Aug 13 23:20:32 2023
    On Monday, August 14, 2023 at 3:21:56 AM UTC+5:30, Andreas Leitgeb wrote:
    Do you really need to play ping pong with the proc name, or do you
    rather want to be able to call the procedure by both names?

    Maybe it would help to create an alias of the procedure, so you can
    leave it in its own name, and still access it by another name, too:
    Look at https://wiki.tcl-lang.org/page/namespace and search for
    section "Make an Alias for a Command"
    Kavitha MS <kavit...@gmail.com> wrote:
    Hi group,

    Below I have a proc called orig_proc and am renaming it to a new_proc. But once I rename it the orig_proc doesnt exist anymore. But the orig_proc is needed to be called by other procs also. So How do I rename the new_proc back to orig_proc

    proc orig_proc {} {
    puts "in original proc"
    }

    rename -force orig_proc new_proc

    proc orig_proc {} {
    eval new_proc // here instead of executing new_proc, I want new_proc to be renamed back to orig_proc
    m() // because m() calls the orig_proc in its definition and it runs into a recursive call which needs to be avoided, so I need new_proc to be renamed back to orig_proc
    }

    How to rename the new_proc back to orig_proc once again?
    In the first step I renamed the orig_proc, because I wanted m() also to be called in orig_proc's new definition. So i renamed it , but now within orig_proc redefinition i need it back to be renamed to its orig_proc name so that m() calls orig_proc
    and it doesnt end up in recursive call.



    Creating alias will make both names to be pointing to same proc orig_proc, but my intention of renaming the orig_proc is to redefine it to accomodate a new API call m1() in addition to its original definition. That's y I didnt think of creating an alias.
    Because old definition of orig_proc and new definition of renamed orig_proc will differ in their functionality. But m() internally calls orig_proc in its definition and since I have renamed orig_proc, it again tries to rename it which leads to a
    recursive call with no exit condition. So before I could call m1() in its new definition, I want the new_proc to be renamed back to orig_proc as below.

    proc orig_proc {} {
    puts "in original proc"
    }

    rename -force orig_proc new_proc

    proc orig_proc {} {
    rename new_proc orig_proc // here new_proc should be renamed back to orig_proc
    m() // because m() calls the orig_proc in its definition and it runs into a recursive call which needs to be avoided, so I need new_proc to be renamed back to orig_proc
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Mon Aug 14 10:53:47 2023
    * Kavitha MS <kavitha.to@gmail.com>
    | proc orig_proc {} {
    | puts "in original proc"
    | }

    | rename -force orig_proc new_proc

    | proc orig_proc {} {
    | eval new_proc // here instead of executing new_proc, I want new_proc to be renamed back to orig_proc
    | m() // because m() calls the orig_proc in its definition and it runs into a recursive call which needs to be avoided, so I need new_proc to be renamed back to orig_proc
    | }

    | How to rename the new_proc back to orig_proc once again?
    | In the first step I renamed the orig_proc, because I wanted m() also
    | to be called in orig_proc's new definition. So i renamed it , but now
    | within orig_proc redefinition i need it back to be renamed to its
    | orig_proc name so that m() calls orig_proc and it doesnt end up in
    | recursive call.

    You can rename the proc *while you're in it* just fine:

    proc orig_proc {} {
    puts "in original proc"
    }

    proc m {} {
    orig_proc
    }

    rename orig_proc new_proc

    proc orig_proc {} {
    puts "in temp orig_proc"
    new_proc
    rename orig_proc ""
    rename new_proc orig_proc
    m
    }


    % orig_proc
    in temp orig_proc
    in original proc
    in original proc

    (Note that my tcl 8.6 has no "rename -force", so I need to delete the
    orig_proc before I can override it again.)

    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From et99@21:1/5 to Kavitha MS on Mon Aug 14 10:51:40 2023
    On 8/13/2023 11:20 PM, Kavitha MS wrote:
    On Monday, August 14, 2023 at 3:21:56 AM UTC+5:30, Andreas Leitgeb wrote:
    Do you really need to play ping pong with the proc name, or do you
    rather want to be able to call the procedure by both names?

    Maybe it would help to create an alias of the procedure, so you can
    leave it in its own name, and still access it by another name, too:
    Look at https://wiki.tcl-lang.org/page/namespace and search for
    section "Make an Alias for a Command"
    Kavitha MS <kavit...@gmail.com> wrote:
    Hi group,

    Below I have a proc called orig_proc and am renaming it to a new_proc. But once I rename it the orig_proc doesnt exist anymore. But the orig_proc is needed to be called by other procs also. So How do I rename the new_proc back to orig_proc

    proc orig_proc {} {
    puts "in original proc"
    }

    rename -force orig_proc new_proc

    proc orig_proc {} {
    eval new_proc // here instead of executing new_proc, I want new_proc to be renamed back to orig_proc
    m() // because m() calls the orig_proc in its definition and it runs into a recursive call which needs to be avoided, so I need new_proc to be renamed back to orig_proc
    }

    How to rename the new_proc back to orig_proc once again?
    In the first step I renamed the orig_proc, because I wanted m() also to be called in orig_proc's new definition. So i renamed it , but now within orig_proc redefinition i need it back to be renamed to its orig_proc name so that m() calls orig_proc
    and it doesnt end up in recursive call.



    Creating alias will make both names to be pointing to same proc orig_proc, but my intention of renaming the orig_proc is to redefine it to accomodate a new API call m1() in addition to its original definition. That's y I didnt think of creating an
    alias. Because old definition of orig_proc and new definition of renamed orig_proc will differ in their functionality. But m() internally calls orig_proc in its definition and since I have renamed orig_proc, it again tries to rename it which leads to a
    recursive call with no exit condition. So before I could call m1() in its new definition, I want the new_proc to be renamed back to orig_proc as below.

    proc orig_proc {} {
    puts "in original proc"
    }

    rename -force orig_proc new_proc

    proc orig_proc {} {
    rename new_proc orig_proc // here new_proc should be renamed back to orig_proc
    m() // because m() calls the orig_proc in its definition and it runs into a recursive call which needs to be avoided, so I need new_proc to be renamed back to orig_proc
    }


    Did you delete a post? I'm seeing an earlier date/time on a follow-up posting.

    I also see m, m1, and method_m, in 3 posts are they all the same?

    If so, is this a tcl procedure or a command implemented in C code? You show these with ()'s and use // for online comments, which is used in C code. The ()'s are not needed in Tcl.


    I might do it this way (assuming method_m is a tcl proc):

    proc func {args} { ;# can be called with 0 or more args
    puts "In function"
    }

    rename func func_orig

    proc func {args} { ;# original plus additional call to method_m
    func_orig {*}$args ;# call original with any args (or none) called with
    method_m
    }

    proc method_m {} {
    if {[info command func_orig] eq ""} { ;# needs to know who to call
    func ;# new_proc hasn't yet been defined, use original name
    } else {
    func_orig ;# call the original version that now has this name
    }
    }

    I've guessed the actual problem is that method_m code really needs to always call the original version of func, but could be called both before and after the rename takes place.

    This way you only do the one rename (and one redefine of func), and don't rename it back.

    If I guessed wrong, well....ignore me :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to et99@rocketship1.me on Mon Aug 14 19:29:15 2023
    et99 <et99@rocketship1.me> wrote:

    I've guessed the actual problem is that method_m code really needs to
    always call the original version of func, but could be called both
    before and after the rename takes place.

    Which is the problem, much of this thread is still *guesswork* on our
    part.

    It so far has the feeling of the OP asking us to make work his specific solution to a problem, without telling us anything about the actual
    problem the solution is meant to solve. And if we were told the actual
    problem there might be a very different solution that is more workable
    than the given solution.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From et99@21:1/5 to Rich on Mon Aug 14 13:08:47 2023
    On 8/14/2023 12:29 PM, Rich wrote:
    et99 <et99@rocketship1.me> wrote:

    I've guessed the actual problem is that method_m code really needs to
    always call the original version of func, but could be called both
    before and after the rename takes place.

    Which is the problem, much of this thread is still *guesswork* on our
    part.

    It so far has the feeling of the OP asking us to make work his specific solution to a problem, without telling us anything about the actual
    problem the solution is meant to solve. And if we were told the actual problem there might be a very different solution that is more workable
    than the given solution.


    I didn't have a clue about this thread until I stumbled upon an earlier thread by the same OP that only appears when I use Thunderbird. It's also started on 8/12.

    In that posting, it seemed to suggest he was trying to create a wrapper that would add something to an existing proc. That post seems clear, although I suspect the OP is a C/C++ programmer somewhat new to Tcl. For example, he knows about {*} but uses it
    incorrectly. That and his use of C++ like comments and () as part of proc/function names intermixed in Tcl code.

    It remains to be seen if my guess was correct, however.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kavitha MS@21:1/5 to All on Tue Aug 15 03:13:33 2023
    On Tuesday, August 15, 2023 at 1:38:53 AM UTC+5:30, et99 wrote:
    On 8/14/2023 12:29 PM, Rich wrote:
    et99 <et...@rocketship1.me> wrote:

    I've guessed the actual problem is that method_m code really needs to
    always call the original version of func, but could be called both
    before and after the rename takes place.

    Which is the problem, much of this thread is still *guesswork* on our part.

    It so far has the feeling of the OP asking us to make work his specific solution to a problem, without telling us anything about the actual problem the solution is meant to solve. And if we were told the actual problem there might be a very different solution that is more workable than the given solution.

    I didn't have a clue about this thread until I stumbled upon an earlier thread by the same OP that only appears when I use Thunderbird. It's also started on 8/12.

    In that posting, it seemed to suggest he was trying to create a wrapper that would add something to an existing proc. That post seems clear, although I suspect the OP is a C/C++ programmer somewhat new to Tcl. For example, he knows about {*} but uses
    it incorrectly. That and his use of C++ like comments and () as part of proc/function names intermixed in Tcl code.

    It remains to be seen if my guess was correct, however.


    Hi,

    Your guess is right. I am new to tcl scripting. Its a tendency to use C/C++ coding syntax since I am used to that. That 's y by mistake i had used m().
    And solution provided by Ralf Fassel worked for me


    You can rename the proc *while you're in it* just fine:

    proc orig_proc {} {
    puts "in original proc"
    }

    proc m {} {
    orig_proc
    }

    rename orig_proc new_proc

    proc orig_proc {} {
    puts "in temp orig_proc"
    new_proc
    rename orig_proc ""
    rename new_proc orig_proc
    m
    }


    % orig_proc
    in temp orig_proc
    in original proc
    in original proc



    With this renaming orig_proc "" (i.e basically deleting it) and then renaming new_proc to orig_proc worked for me.
    Thank you all for all the possible solutions provided.
    Thanks once again

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kavitha MS@21:1/5 to Ralf Fassel on Tue Aug 15 03:15:03 2023
    On Monday, August 14, 2023 at 2:23:53 PM UTC+5:30, Ralf Fassel wrote:
    * Kavitha MS <kavit...@gmail.com>
    | proc orig_proc {} {
    | puts "in original proc"
    | }

    | rename -force orig_proc new_proc

    | proc orig_proc {} {
    | eval new_proc // here instead of executing new_proc, I want new_proc to be renamed back to orig_proc
    | m() // because m() calls the orig_proc in its definition and it runs into a recursive call which needs to be avoided, so I need new_proc to be renamed back to orig_proc
    | }

    | How to rename the new_proc back to orig_proc once again?
    | In the first step I renamed the orig_proc, because I wanted m() also
    | to be called in orig_proc's new definition. So i renamed it , but now
    | within orig_proc redefinition i need it back to be renamed to its
    | orig_proc name so that m() calls orig_proc and it doesnt end up in
    | recursive call.
    You can rename the proc *while you're in it* just fine:
    proc orig_proc {} {
    puts "in original proc"
    }
    proc m {} {
    orig_proc
    }

    rename orig_proc new_proc

    proc orig_proc {} {
    puts "in temp orig_proc"
    new_proc
    rename orig_proc ""
    rename new_proc orig_proc
    m
    }


    % orig_proc
    in temp orig_proc
    in original proc
    in original proc

    (Note that my tcl 8.6 has no "rename -force", so I need to delete the orig_proc before I can override it again.)

    R'
    Thank you. It worked

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From et99@21:1/5 to Kavitha MS on Tue Aug 15 08:18:34 2023
    On 8/15/2023 3:13 AM, Kavitha MS wrote:
    On Tuesday, August 15, 2023 at 1:38:53 AM UTC+5:30, et99 wrote:
    On 8/14/2023 12:29 PM, Rich wrote:
    et99 <et...@rocketship1.me> wrote:

    I've guessed the actual problem is that method_m code really needs to
    always call the original version of func, but could be called both
    before and after the rename takes place.

    Which is the problem, much of this thread is still *guesswork* on our
    part.

    It so far has the feeling of the OP asking us to make work his specific
    solution to a problem, without telling us anything about the actual
    problem the solution is meant to solve. And if we were told the actual
    problem there might be a very different solution that is more workable
    than the given solution.

    I didn't have a clue about this thread until I stumbled upon an earlier thread by the same OP that only appears when I use Thunderbird. It's also started on 8/12.

    In that posting, it seemed to suggest he was trying to create a wrapper that would add something to an existing proc. That post seems clear, although I suspect the OP is a C/C++ programmer somewhat new to Tcl. For example, he knows about {*} but uses
    it incorrectly. That and his use of C++ like comments and () as part of proc/function names intermixed in Tcl code.

    It remains to be seen if my guess was correct, however.


    Hi,

    Your guess is right. I am new to tcl scripting. Its a tendency to use C/C++ coding syntax since I am used to that. That 's y by mistake i had used m().
    And solution provided by Ralf Fassel worked for me


    You can rename the proc *while you're in it* just fine:

    proc orig_proc {} {
    puts "in original proc"
    }

    proc m {} {
    orig_proc
    }

    rename orig_proc new_proc

    proc orig_proc {} {
    puts "in temp orig_proc"
    new_proc
    rename orig_proc ""
    rename new_proc orig_proc
    m
    }


    % orig_proc
    in temp orig_proc
    in original proc
    in original proc



    With this renaming orig_proc "" (i.e basically deleting it) and then renaming new_proc to orig_proc worked for me.
    Thank you all for all the possible solutions provided.
    Thanks once again



    Another approach is to use tcl's "come from" ability using [info frame -1]

    Consider:

    proc foo {args} {
    set caller_frame [info frame -1]
    puts "caller frame dict: \{$caller_frame\}"
    set caller [dict get $caller_frame proc]
    puts "caller is $caller"

    }

    proc the_caller {args} {
    foo
    }

    the_caller

    This will output from foo:

    caller frame dict: {type proc line 2 cmd foo proc ::the_caller level 1}
    caller is ::the_caller


    And so you could break the recursion this way.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)