:TITLE: State of the interpreter - info ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl28.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: There are a number of subcommands that provide information about the current state of the interpreter. These commands provide access to information like the current version and patchlevel, what script is currently being executed, how many commands have been executed, or how far down in the call tree the current proc is executing.

Note that the pid command is not part of the info command, but a command in its own right.

Commands that return information about the current state of the interpreter

info cmdcount
Returns the total number of commands that have been executed by this interpreter.
info level ?number?
Returns the stack level at which the compiler is currently evaluating code. 0 is the top level, 1 is a proc called from top, 2 is a proc called from a proc, etc.

If number number is a positive value, info level returns a the name and arguments of the proc at that level on the stack. Number is that same value that file level would return if it were called in the proc being referenced.

If number number is a negative value, it refers to the current level plus number. Thus, , info level returns a the name and arguments of the proc at that level on the stack.

info patchlevel
Returns the value of the global variable tcl_patchlevel. This is the revision level of this interpreter.
info script
Returns the name of the file currently being evaluated, if one is being evaluated. If there is no file being evaluated, returns an empty string.
info tclversion
Returns the value of the global variable tcl_version. This is the patch level of this interpreter.
pid
Returns the pid of the current Tcl interpreter.
:TEXT_END: :LESSON_TEXT_START_LEVEL 1: There are a number of subcommands that provide information about the current state of the interpreter. These commands provide access to information like the current version and patchlevel, what script is currently being executed, how many commands have been executed, or how far down in the call tree the current proc is executing.

The info tclversion and info patchlevel can be used to find out if the revision level of the interpreter running your code has the support for features you are using. If you know that certain features are not available in certain revisions of the interpreter, you can define your own procs to handle this, or just exit the program with an error message.

The info cmdcount and info level can be used while optimizing a Tcl script to find out how many levels and commands were necessary to accomplish a function.

Note that the pid command is not part of the info command, but a command in its own right.

Commands that return information about the current state of the interpreter

info cmdcount
Returns the total number of commands that have been executed by this interpreter.
info level ?number?
Returns the stack level at which the compiler is currently evaluating code. 0 is the top level, 1 is a proc called from top, 2 is a proc called from a proc, etc.

If number number is a positive value, info level returns a the name and arguments of the proc at that level on the stack. Number is that same value that file level would return if it were called in the proc being referenced.

If number number is a negative value, it refers to the current level plus number. Thus, , info level returns a the name and arguments of the proc at that level on the stack.

info patchlevel
Returns the value of the global variable tcl_patchlevel. This is the revision level of this interpreter.
info script
Returns the name of the file currently being evaluated, if one is being evaluated. If there is no file being evaluated, returns an empty string.
info tclversion
Returns the value of the global variable tcl_version. This is the patch level of this interpreter.
pid
Returns the pid of the current Tcl interpreter.
:TEXT_END: :LESSON_TEXT_START_LEVEL 2:

The info command also includes several subcommands which provide information about the state of the interpreter. These commands provide access to information like the current version and patchlevel, what script is currently being executed, how many commands have been executed, or how far down in the call tree the current proc is executing.

The info tclversion and info patchlevel can be used to find out if the revision level of the interpreter running your code has the support for features you are using. If you know that certain features are not available in certain revisions of the interpreter, you can define your own procs to handle this, or just exit the program with an error message.

The info cmdcount and info level can be used while optimizing a Tcl script to find out how many levels and commands were necessary to accomplish a function.

Run the example, and examine the output while you read this description of the example code.

The first two lines show how many instructions have been executed so far. The initial output is high because of all the commands that the interpreter executes setting itself up to run the script. Note that the number of commands increases by two between the two lines. There are two commands in the first line, the puts, and the [info cmdcount].

The patchlevel and tclversion calls will return the same value if there are no patches applied to the version of the interpreter you are running. If patches have been applied, then the patchlevel subcommand will return a string that includes patches, while tclversion will return the base version level.

The info script command will return the name of the script that is being executed. When you click the Run Example button, the TclTutor program writes the code in the Example Code window to a file in the /tmp directory, and executes from that script.

When programs are invoked under the Unix operating system, they are assigned a number. This number is the Program ID number, or PID for short. When you need to interact with a process at the lowest level of the operating system, it is referred to by that number. That number is also guaranteed to be unique. No two processes on a system will be using the same PID at the same time.

Therefore, the PID can be used to generate a unique file name. The file name reported in script is generated with a line of code like this:

set filename "/tmp/xmpl.[pid].tcl"

The pid command is shown in this example code printing out the PID of the script. Each time you click the Run Example button, it will display a new number.

The proc factorial demonstrates a way to use the info level command to write an inefficient factorial program. The factorial proc will continue calling itself until the current level matches the value that was passed as the argument. When this level has been reached, the nested calls to factorial will unwind as each invocation of factorial returns the value from the previous call to factorial times ($val-$lvl).

When a function keeps calling itself it is called recursion.

When factorial is called the first time, the level is 1. Since $lvl is not equal to $val, line after the if statement is executed. The Tcl interpreter sees that the return statement requires the return from a call to factorial, and it then invokes factorial again.

This time, $lvl is 2. Again, the if statement is false, and factorial is invoked again.

This time, since a proc has called a proc which called a proc, the level is 3. In the example code, this matches $val, so the function returns the value of $val.

That value is read in the last line of the previous invocation of factorial, when $lvl was 2. ($val-$lvl) = 3-2, = 1. So, the last line becomes 1*3, and that invocation of factorial returns 3 to the previous (first) invocation of factorial.

In the first invocation of factorial, the level was 1. 3-1=2, so the last line returns 2*3, which is 6.

It's obvious that multiplying a number by 1 is an unnecessary step. Try modifying the example code to reduce the number of recursions and see how many fewer commands you can use to calculate the factorial.

Commands that return information about the current state of the interpreter

info cmdcount
Returns the total number of commands that have been executed by this interpreter.
info level ?number?
Returns the stack level at which the compiler is currently evaluating code. 0 is the top level, 1 is a proc called from top, 2 is a proc called from a proc, etc.

If number number is a positive value, info level returns a the name and arguments of the proc at that level on the stack. Number is that same value that file level would return if it were called in the proc being referenced.

If number number is a negative value, it refers to the current level plus number. Thus, , info level returns a the name and arguments of the proc at that level on the stack.

info patchlevel
Returns the value of the global variable tcl_patchlevel. This is the revision and patch level of this interpreter.
info tclversion
Returns the value of the global variable tcl_version. This is the revision and patch level of this interpreter.
info script
Returns the name of the file currently being evaluated, if one is being evaluated. If there is no file being evaluated, returns an empty string.
pid
Returns the pid of the current Tcl interpreter.
:TEXT_END: :CODE_START: puts "This is how many commands have been executed: [info cmdcount]" puts "Now *THIS* many commands have been executed: [info cmdcount]" puts "\nThis interpreter is revision level: [info tclversion]" puts "This interpreter is at patch level: [info patchlevel]" puts "The Pid for this program is [pid]" proc factorial {val} { puts "Current level: [info level] - val: $val" set lvl [info level] if {$lvl == $val} {return $val;} return [expr ($val-$lvl) * [factorial $val]]; } set count1 [info cmdcount] set fact [factorial 3] set count2 [info cmdcount] puts "The factorial of 3 is $fact" puts "Before calling the factorial proc, $count1 commands had been executed" puts "After calling the factorial proc, $count2 commands had been executed" puts "It took [expr $count2-$count1] commands to calculate this factorial" :TEXT_END: