Passing multiple args to a tcl proc in one variable

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Passing multiple args to a tcl proc in one variable

  • Creator
    Topic
  • #47902
    Wesley Combs
    Participant

      I have a tcl proc that accepts multiple arguments like so

      proc ECP_Send_Email {recipient email_server subject body} {

         #Do work here.

      }

      I want to call that proc sending it the arguments.

      Well I want to pass all the arguments in one variable though, like so:

      set strArgs {test@test.com myserver test test}

      ECP_Send_Email $strArgs

      is there a way to do this? Maybe a breakout command that will break out each of the items into a variable and pass it to the proc?

      Thanks

    Viewing 3 reply threads
    • Author
      Replies
      • #57027
        Charlie Bursell
        Participant

          Sure.

          Change the proc to:

          proc procname {args} {

          args will be a list of arguments passed.  You can pass as many as you like

        • #57028
          Tom Henderson
          Participant

            Something like that.  First, put your arguments into a list variable, then use the eval command to call your proc with that list.  The eval command “flattens” your list so they look like individual variables.

            Something like:

            set listArgs

              eval ECP_Send_Email $listArgs

              The eval command will let you construct just about any command dynamically, then call it.  It works with lists, though, not strings.  Create the command as a list, then use eval.

              But if you’re doing a proc call, you want to be sure that the list you’re passing to the proc has the same number of arguments as the proc is expecting, or you will get a run-time error.

          • #57029
            Troy Morton
            Participant

              I personally prefer keyed lists.  This allows you to add or remove arguments to the proc without affecting the fucntionality or compatibility if the proc is being used in multiple places .. like so ..

              Code:


                       #
                       # Populate the arguments to send to the proc SendEmailMessage
                       # and call the proc.
                       #
                       keylset mailargs MH         $mh
                       keylset mailargs TOFILE     $blnTrcWrite
                       keylset mailargs HOSP       $msghosp
                       keylset mailargs MAILTO     $mailto
                       keylset mailargs MAILFROM   $mailfrom
                       keylset mailargs MAILSUBJ   $mailsubj
                       keylset mailargs MAILMSG    $mailmsg
                       keylset mailargs MAILSERVER $mailserver
                       keylset mailargs MAILDOMAIN $maildomain
                       keylset mailargs MAILSTART  $mailstart
                       keylset mailargs MAILSTOP   $mailstop

                       SendEmailMessage $mailargs

              … and in the called proc get the data out of the argument keyed list …

              Code:


              proc SendEmailMessage { mailargs } {

                 keylget mailargs         MH mh
                 keylget mailargs     TOFILE blnTrcWrite
                 keylget mailargs       HOSP hosp
                 keylget mailargs     MAILTO mailto
                 keylget mailargs   MAILFROM mailfrom
                 keylget mailargs   MAILSUBJ mailsubj
                 keylget mailargs    MAILMSG mailmsg
                 keylget mailargs MAILSERVER mailserver
                 keylget mailargs MAILDOMAIN maildomain
                 keylget mailargs  MAILSTART mailstart
                 keylget mailargs   MAILSTOP mailstop

              As I mentioned this gives you alot of flexibility to add or remove arguments and functionality to a proc while keeping it backwards compatible with other places its being used.

              Also, it doesn’t matter what order you supply the arguments because the list is keyed.  So, you don’t have to remember that the first argument is X and the second is Y the third is Z and take a chance of putting them in the wrong order when calling the proc.

              Hope this helps

            • #57030
              Scott Folley
              Participant

                Yeah Troy, that is my favorite as well.  It is also nice to keep in mind that you can use optional arguments to a TCL proc like this:

                proc SendEmailMessage { mailargs {debug “0”} } {

                    if {$debug} {

                           echo “I am in debug mode”

                }

                That way you can send all of your information in via the keyed list variable and add an additional variable to indicate whether you are in debug mode..

                Just my $.02

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