: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
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.
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
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.
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
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.