Call a seperate tcl proc from a different file

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Call a seperate tcl proc from a different file

  • Creator
    Topic
  • #50940
    Jerry Tilsley
    Participant

      All,

      I am wanting to start breaking out some of my repetitive code to single procs. And then call these procs with parameters in other scripts.  That way when I need to make a change, I can just change it in one place.

      Example: TPS1.tcl would contain the code to connect to a db and insert a line into a table.  TPS2.tcl and TPS3.tcl would call TPS1 during a route UPoC.

      Can someone give me some guidance on how this is done, especially with the parameters.

      Thanks,

      Jerry

    Viewing 5 reply threads
    • Author
      Replies
      • #68117
        Jim Kosloskey
        Participant

          Jerry,

          I may not have all the ways here:

          1. call the invoked proc with the variable included in the invocation. Ex. if proc to be invoked is called proc2 then proc2 $var1 $var2 $var3, etc. Of course a catch around the call would be good and the called proc should return some sort of value if only to indicate success – so set var [proc2 $var1 $var2 $var3 ,etc.] (again with a catch if you want.

          2. set a global and reference the same global in the called proc.

          3. Use namespaces and packages and make your library of support modules a part of the package (this is the way I have begun to move).

          4. upvar a variable in proc2 that was set in proc1 (the xpm type procs do this to get the xlate variables from the Xlate). Then the call does not have any arguments.

          5. Build you argumenbts in a Table and use Table lookup to find your arguments.

          As I said, I may not have covered everything nor is there any extreme detail.

          email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

        • #68118
          Jerry Tilsley
          Participant

            Ok, I like option number 2.  Just going to take some work to figure out how to move my scripts that direction.

            Thanks for your input!

          • #68119
            John Perks
            Participant

              Jim’s methods are not mutually exclusive, by the way.

              They all could be utilized to some degree, depending on the needs of your situation.

              #2 is probably the easiest to implement in terms of coding changes.  You can easily put the duplicated code into a called proc and just remember to globalize the necessary variables.

              But that called proc probably no longer fits the profile of a tps procedure in terms of args it gets from the engine.  So use #3 and put those called procs in a package. then they would no longer be indexed in the current tclindex and consequently would no longer clutter up the gui proc selection windows in the IDE.  If they are no longer tps procedures you would not want them there anyway.

              And using namespaces and packages obviates the need to keep all your proc names unique, which become a hassle as your collection of procs grows

              You can combine both (user defined) passed arguments (#1) and global variables (#2) in the same procedure to further enahance the versatility of the procs you design.  After all, some variables you may want to share amongst all procs called from a main proc, such the $msg.  Other variables you may want only local, yet at the same time you need to pass their value back to the calling procedure.  It really depends on your needs; if you intend to make your called procs available to other main procedures developed later, now is the time to design them with that versatility and re-usability.

            • #68120
              Jerry Tilsley
              Participant

                I appreciate everyone’s input on this.  I will like to get a little clarification on how to do something here.  I have started to build a couple of packages, and have them pulled into a couple procs, but would like to use namespaces with them as well.  How can I combine namespaces and packages?  Does anyone have any examples of how these are combined and used with other procs inside cloverleaf?

                Again, thanks for all the help!

              • #68121
                Troy Morton
                Participant

                  This link explains how to create a Tcl Package.  There is also a very good section in the book “Practicle Prgramming in Tcl and Tk” by Brent B. Welch.

                  <a href="https://usspvlclovertch2.infor.com/viewtopic.php?t=2596&highlight=create+package&#8221; class=”bbcode_url”>https://usspvlclovertch2.infor.com/viewtopic.php?t=2596&highlight=create+package

                • #68122
                  Troy Morton
                  Participant

                    Code:


                    namespace eval nsAT {

                     proc timeInSeconds { hrmin } {
                       lassign [split $hrmin :] hr min
                     
                       set myHr [string trimleft $hr 0]
                       set myMin [string trimleft $min 0]
                       
                       if [cequal $myHr “”] { set myHr 0 }
                       if [cequal $myMin “”] { set myMin 0 }
                       
                       set hrsecs  [expr $myHr * 60 * 60]
                       set minsecs [expr $myMin * 60]
                       
                       return [expr $hrsecs + $minsecs]
                     }
                     
                     proc now { } {
                      return [clock format [clock seconds] -f “%m/%d/%Y %H:%M:%S”]
                     }
                     
                    }

                    nsAT::TimeInSeconds returns the number of elapsed seconds

                    nsAT::now returns the current date and time

                Viewing 5 reply threads
                • The forum ‘Cloverleaf’ is closed to new topics and replies.