-1

I need to modify a parameter named test inside process_data and switch cases outside that function depending on test value.

I couldn't pass it by reference using upvar because the process_data represent a static function for processing received udp packet, and it won't accept more than two parameters 'size and data'.
Also, as far as I found, there is no returned value for the process_data function.

Code:

set test "0"

Agent/UDP instproc process_data {size data} {
     //some stuff
     if (...)
        set test "1"
}

// switch cases depending on test value.
Eibo
  • 245
  • 2
  • 14

1 Answers1

1

You don't need an extra argument to use upvar if you know the name of the variable you're going to alias. You should be able to do either of these (do not use both):

global test
upvar #0 test test

It's not really classic modular programming, but it will work.

A good place to put the command is at the beginning of the procedure body, like so:

Agent/UDP instproc process_data {size data} {
    global test
    # some stuff
}

Same thing if you use upvar #0 test test (those two commands are basically equivalent).

Documentation: global, upvar

Peter Lewerin
  • 13,140
  • 1
  • 24
  • 27
  • I'm not an expert in tcl coding, so can you please edit your answer to be more specific where to put each, use my example if you like. Thanks – Eibo Aug 06 '16 at 22:36
  • Inside `process_data`, I can change `test` as I like, but outside `process_data` it stays "0" as I've set it in the beginning. – Eibo Aug 07 '16 at 09:26
  • @EmadAldeen: is the outer `test` declared in a namespace or in some other scope than the global one? – Peter Lewerin Aug 07 '16 at 10:45
  • `test` is declared in global scope, I need something similar to 'returned value' from `process_data`. – Eibo Aug 07 '16 at 10:50
  • @EmadAldeen: if you use `global test`, the `test` variable outside the procedure will have the same value as the `test` variable inside it. Something is not right here. Can you edit your question to show more of your code? – Peter Lewerin Aug 07 '16 at 13:15
  • sorry it was another issue, your answer worked. thanks – Eibo Aug 07 '16 at 18:47