tailieunhanh - Beginning Perl Third Edition PHẦN 9

Khi đối tượng không còn sử dụng khi đó là một biến từ vựng mà đi ra khỏi phạm vi-Perl tự động phá hủy nó. Tuy nhiên, trước khi thực hiện, Perl sẽ cố gắng để gọi một phương thức có tên là DESTROY (). đối tượng theo định hướng PERL | CHAPTER 14B INTRODUCTION TO CGI textfield -name lastname br submit end_form end_html This program generates the same form as and it processes the form the same as . Note that it builds the form using methods. There are a few methods worth noting. First the start_form method builds the beginning form tag. The action can be specified with -action cgi-bin but we re using the default action which is the same CGI program that built the form. The form is eventually closed with end_form . The text form widget is created with textfield -name lastname . The textfield method is one of many methods used to create form widgets. Basically there s a method for each different type of widget available. For a complete list see perldoc CGI. Let s Play Chess It s time to roll all the topics we have discussed into a single example. This CGI script will be a web implementation of the chess program we discussed in Chapter 11. Since we are playing chess on the Web we ll call this CGI script . This program will illustrate that with just a little bit of additional code we can web-enable a program we wrote for the shell. Before we look at the program it is important to note that a CGI script is stateless. That means the CGI script itself can t remember anything about the most recent execution or state of the script. As a result we somehow have to remember the recent state of the chessboard so we can pick up the game from the last move the user made. This is different from the program each move was made within the same execution of the program so always knew the state from move to move. We will keep track of the state of the chessboard in a file named . This file will be an eight-line file with each line being one row on the board. Each row will have its eight pieces colon separated. Here is the initial state of the chessboard WR WP WN WP BP BP BR BN WB WQ WK WB WN WR WP WP WP WP WP WP BP BP BP BP BP BP BB