Reporters Sans Frontières | Reporter ohne Grenzen die Amika Methode   
Lesezeichen [ Jobs # QR-Code # Projekte ]Do 18 Juli 2024 23:03:09 # Grundlagen der Programmierung | Tcl Tutorial.


. Creating Commands - eval .

[ Previous | Index | Next ]

One difference between Tcl and most other compilers is that Tcl will allow an executing program to create new commands and execute them while running.

A tcl command is defined as a list of strings in which the first string is a command or proc. Any string or list which meets this criteria can be evaluated and executed.

The eval command will evaluate a list of strings as though they were commands typed at the % prompt or sourced from a file. The eval command normally returns the final value of the commands being evaluated. If the commands being evaluated throw an error (for example, if there is a syntax error in one of the strings), then eval will throw an error.

Note that either concat or list may be used to create the command string, but that these two commands will create slightly different command strings.

eval arg1 ??arg2?? ... ??argn??
Evaluates arg1 - argn as one or more Tcl commands. The args are concatenated into a string, and passed to tcl_Eval to evaluate and execute.

Eval returns the result (or error code) of that evaluation.


. Example .

   set cmd {puts "Evaluating a puts"}
   puts "CMD IS: $cmd"
   eval $cmd

   if {[string match [info procs newProcA] ""] } {
    puts "\nDefining newProcA for this invocation"
    set num 0;
    set cmd "proc newProcA "
    set cmd [concat $cmd "{} {\n"]
    set cmd [concat $cmd "global num;\n"]
    set cmd [concat $cmd "incr num;\n"]
    set cmd [concat $cmd " return \"/tmp/TMP.[pid].\$num\";\n"]
    set cmd [concat $cmd "}"]
    eval  $cmd

   puts "\nThe body of newProcA is: \n[info body newProcA]\n"

   puts "newProcA returns: [newProcA]"
   puts "newProcA returns: [newProcA]"

   # Defind a proc using lists

   if {[string match [info procs newProcB] ""] } {
    puts "\nDefining newProcB for this invocation"
    set cmd "proc newProcB "
    lappend cmd {}
    lappend cmd {global num; incr num; return $num;}

    eval  $cmd

   puts "\nThe body of newProcB is: \n[info body newProcB]\n"
   puts "newProcB returns: [newProcB]"

[ Home | Top ]
[ . Previous | Index | Next . ]
Der Inhalt dieser Seite wurde am 06.11.2019 um 12.19 Uhr aktualisiert.
Navigation Seminare Magic Software Projekte Publikationen Kontakt Home
   huecker dot com * Germany | Datenschutz
© 1999, 2024 Franz-Josef Hücker. All Rights Reserved.
Contact Print Page LinkedIn follow me on twitter RSS Feeds & Podcasts