Karam
Karam

Reputation: 13

How can i use "eval" procedure in TCL?

What does eval mean in the following code?

proc poissontraffic { src dst interval starttime } {
   global ns_ node_
   set udp($src) [new Agent/UDP]
   eval $ns_ attach-agent \$node_($src) \$udp($src)
   set null($dst) [new Agent/Null]
   eval $ns_ attach-agent \$node_($dst) \$null($dst)
   set expl($src) [new Application/Traffic/Exponential]
   eval \$expl($src) set packetSize_ 70
   eval \$expl($src) set burst_time_ 0
                                    # idle_time + pkt_tx_time = interval
   eval \$expl($src) set idle_time_ [expr $interval*1000.0-70.0*8/250]ms
   eval \$expl($src) set rate_ 250k
   eval \$expl($src) attach-agent \$udp($src)
   eval $ns_ connect \$udp($src) \$null($dst)
   $ns_ at $starttime "$expl($src) start"
}

Upvotes: 0

Views: 688

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137787

The eval command concatenates its arguments and evaluates the resulting string as a Tcl script. The concatenation is done by stripping spaces from either end of each argument and then joining them together with a single space between.

In the given code, for example,

eval $ns_ connect \$udp($src) \$null($dst)

effectively does two rounds of substitutions, once to fill in $ns_, $src and $dst, and again to read from $udp(...), $null(...), and run a command.

You can probably replace it with this:

$ns_ connect $udp($src) $null($dst)

That'll only be a problem if $ns_ is a multi-word value. Since this is fundamentally OTcl where object names are usually well-behaved, it probably isn't and all the evals will be adding is confusion, slowness and insecurity.

Upvotes: 1

Related Questions