当前位置:网站首页>Detailed explanation of TCL scripting language (1)

Detailed explanation of TCL scripting language (1)

2022-07-23 19:08:00 qq_ forty-four million nine hundred and eighty-five thousand si

Introduction to language

TCL It's a very common scripting language , Powerful . It was first called “ Tool command language ”“Tool Command Language”, But that's not what it means now , But we still call it TCL, Pronunciation is "tickle”.TCL It's often used in rapid prototyping , scripting ,GUI And testing .

It actually contains two parts : A language and a library . First ,Tcl It's a simple scripting language , It is mainly used to issue commands to some interactive programs, such as text editors 、 Debugger and shell. It has a simple syntax and strong extensibility ,Tcl You can create new procedures to enhance their built-in command capabilities . secondly ,Tcl It's a library bag , Can be embedded in applications ,Tcl The library contains a parser 、 Routines and for executing built-in commands can enable you to expand ( Define a new process ) Library function . Applications can produce Tcl Command and execute , Commands can be generated by the user , It can also be read from an input of the user interface ( Buttons or menus, etc ). but Tcl After receiving the command, the library decomposes it and executes the built-in command , There are often recursive calls .

Tcl The data type is simple . Yes Tcl Come on , It has only one kind of data to process —— character string .Tcl Store variable values as strings , Don't care about its actual use type . embedded Tk(toolkit) Graphics tools can provide simple and rich graphics functions , So that users can easily create a simple graphical interface .

Tcl The execution is interactive ,Tcl Provides an interactive command interface , There are two interfaces :tclsh and wish.tclsh Only support Tcl command ,wish Support Tcl and Tk command . Through the interactive interface , Then we can execute UNIX shell command , Execute command by command , And get the execution results immediately .

Tcl/Tk Can provide cross platform support .Tcl The language can run in most of today's popular UNIX、WINDOWS and Macintosh Wait for the system , And the command is universal , It's just that the details of the startup are a little different .

Basic grammar

 1、** Command structure **:commandName arguments. Each command is passed through line feed or ; separate . for example :

set foo 0

set bar 1;
2、** notes **: Use # notes , However, it should be noted that the following comments are wrong :
 set foo0 # This is the note ( error )
  because tcl The parser always thinks that a command should end with a newline or semicolon , The others are considered parameters in the same line . So the right thing to do is :
 set foo 0;  # This is the note 

In fact, for people who are used to compiled programming languages, add a at the end of each line ; It should be taken for granted , So we should not be unaccustomed to this .

  3、 data type :tcl Forms such as... In other languages are not supported int, double ,char And so on , The only support is string type . That is to say, a variable can be understood as different types at different times .
4、 Variable :tcl Two types of variables can be defined , Variables and arrays .

 Variable : stay tcl Variables can be assigned directly without declaration . Examples of assigning values to variables :

set ba 1;

set ma “qian lifeng”; # The function of quotation marks is to tell the compiler to include spaces 

 The value of the obtained variable can be prefixed with the corresponding variable name $ To obtain , for example :

puts $ba;  # Print out ba Value 

 Array : Arrays do not need to be declared , Direct assignment , for example :

set qian(0) 1;

set qian(1) 2;

 An interesting thing is that array subscripts can be written out of order , It's not necessarily a number , Can be a string . for example :

set qian(123) f;

set qian(6789)fs;

set qian(good)yes;

 We can use puts $qian(123);  To get the contents of the array . Of course tcl Arrays also support multidimensional arrays , The declaration form is :

setqian(1,1,1,1,1) fs; # Any number of dimensions 

 If we want to view the information of an array that has been assigned , have access to parray command : Such as :

parray qian;   # Will print out qian All the information of the array 
 5、 String manipulation :string  The basic syntax of the command is as follows ( there string1,string2 It can be a string or a variable ,option Is one of the following options ).

    stringoption string1 string2;

    option Operation options for :

    compare Compare according to the order of the dictionary . according to string1 <, =, >string2 Return respectively -1, 0, 1

    first return string2 For the first time string1 The location of , If not string1 Then return to -1

    last and first contrary 

    trim from string1 Delete the beginning and end of appear in string2 The characters in 

    tolower return string1 The new string after all characters in are converted to lowercase characters 

    toupper return string1 All strings in are converted to uppercase strings 

    length return string1 The length of 

     Example :

    string length$qian;  # The variable will be returned qian The length of 
6、 Digital operation : because tcl Only one of them string Variable of type , So when you want to operate on numbers ,tcl Provides incr and expr Two operands .

incr The basic usage of is :

incrvariable integer(variable Must be a number )

 For example, I want to add a number 3

set a 3;

incr a 3;  # take a Add 3, If we want to reduce 3, Then for  incr a–3;

 default incr a Equate to a++ It means , That is, self increase 1.

expr The basic grammar of is :

expr functionnumber

expr Is a syntax designed to provide more complex operations , For example, multiplication and division, and so on . When performing arithmetic operations, you must put expr Commands are placed before arithmetic operations . for example :

set a 20; set b4;

set c [expr$a/$b];  # At this time c The value of is 5

 besides ,expr You can also recognize some functions and their return values as follows :

abs(x) x The absolute value of 

round(x) x The integer value obtained after rounding 

sin(x) x Sine of 

…………………………………..

 for instance :

set a [exprsin(0.3)]; # Calculation 0.3 Sine of 
7、 escape 

 stay tcl Can be used in {
    } Enclose the string that needs to be escaped . for example :

set a 5;

set b {
    $a};  # here b The value of is ”$a“, instead of 5.
8、 flow control 

if control flow , Basic grammar :

if {
      expression  } {
    

    # operation ;

} else {
    

     # Other operations ;

}

 It should be noted that if….else, if……elseif… Medium else,elseif And if The one behind the {
     On the same line .

 

switch control flow : Basic grammar :

switch $x {
    

            The number 1 {
      operation 1 ;}
 The number 2 {
      operation 2 ;}

}
9、 loop 

tcl Three loop commands are provided , Respectively :for,foreach,while. Besides tcl Two cycle control commands are also provided . Be careful : The opening curly brackets of the program block in the three commands must be on the same line as the three commands .

1、while Basic grammar :while {
     Conditions } {
      Block  }

2、foreach Loop through a set of arguments , And will execute his loop body every time . The basic structure :  foreachvariable {
    items} {
    block}

 there variable It's the name of the variable , Easy block Use in . give an example :

foreach element{
    0 m n b v} {
    

        switch$element {
    

        #  Judge element Value 

     }

}

3、for Is the most commonly used loop . Its basic structure is :for{
    initialization} {
    condition} {
    increment} {
    body}

 for example :for {
    set i 0} {
    $i< 10} {
    incr i} {
    puts $i;}  # Will print out 0 To 9

Program example

#add a member function call "greet"
Class mom
mom instproc greet {
    } {
    
         $selfinstvar age_
         puts"$age_ year old mom say: How are you doing?"
}
 
#Create a child class of "mom" called"kid"
#and overide the member function "greet"
Class kid -superclass mom
kid instproc greet {
    } {
    
         $selfinstvar age_
         puts"$age_ year old kid say: What's up, dude?"
}
 
#Create a mom and a kid object, set each age
set a [new mom]
$a set age_ 45
set b [new kid]
$b set age_ 15
 
#Calling member function "greet" of eachobject
$a greet
$b greet
原网站

版权声明
本文为[qq_ forty-four million nine hundred and eighty-five thousand si]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231641422770.html