Impressum
GoTo:
Home
 
Reporters Sans Frontières | Reporter ohne Grenzen Akazien Verlag   
 
Lesezeichen [ Info # Jobs # QR-Code # Publikationen ]Sa 20 April 2024 11:30:46


 huecker.com # Grundlagen der Programmierung | Tcl Tutorial.
--

 

. Learning the existence of commands and variables ? - info .

[ Previous | Index | Next ]

The info command allows a Tcl program to obtain information from the Tcl interpreter about the current state of the interpreter. The next three lessons cover aspects of the info command.

This lesson covers the info subcommands that return information about which procs, variables, or commands are currently in existence in this instance of the interpreter. By using these subcommands you can determine if a variable or proc exists before you try to access it.

The example code shows how to use the info exists command to make an incr that will never return a no such variable error, since it checks to be certain that the variable exists before incrementing it.

Info commands that return lists of visible commands and variables.

info commands ?pattern?
Returns a list of the commands that match pattern, using the string match rules. If pattern is not provided, a list of all commands is returned.
info exists varName
Returns 1 if varName exists as a variable in the current context, otherwise returns 0.
info globals ?pattern?
Returns a list of the global variables that match pattern, using the string match rules. If pattern is not provided, a list of all global variables is returned.
info locals ?pattern?
Returns a list of the local variables that match pattern, using the string match rules. If pattern is not provided, a list of all local variables is returned.
info procs ?pattern?
Returns a list of the procs that match pattern, using the string match rules. If pattern is not provided, a list of all procs is returned.
info vars ?pattern?
Returns a list of the variables that match pattern, using the string match rules. If pattern is not provided, a list of all variables is returned.

--

. Example .

   proc safeIncr {val {amt 1}} {
    upvar $val v
    if {[info exists v]} { incr v $amt } else { set v $amt }
   }

   if {[info procs safeIncr] == "safeIncr"} {
    safeIncr a
   }

   puts "After calling SafeIncr with a non existent variable: $a"

   set a 100
   safeIncr a
   puts "After calling SafeIncr with a variable with a value of 100: $a"

   safeIncr b -3
   puts "After calling safeIncr with a non existent variable by -3: $b"

   set b 100
   safeIncr b -3
   puts "After calling safeIncr with a variable whose value is 100 by -3: $b"

   puts "\nThese variables have been defined: [lsort [info vars]]"
   puts "\nThese globals have been defined:   [lsort [info globals]]"

   set exist [info procs localproc];
   if {$exist == ""} {
    puts "\nlocalproc does not exist at point 1"
   }

   proc localproc {} {
    global argv;

    set loc1 1;
    set loc2 2;
    puts "\nLocal variables accessible in this proc are: [lsort [info locals]]"
    puts "\nVariables accessible from this proc are: [lsort [info vars]]"
    puts "\nGlobal variables visible from this proc are: [lsort [info globals]]"
   }

   set exist [info procs localproc];
   if {$exist != ""} {
    puts "localproc does exist at point 2"
   }

   localproc;
  

--
[ 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.
Send Page Print Page LinkedIn follow me on twitter RSS Feeds & Podcasts