Đang chuẩn bị liên kết để tải về tài liệu:
Beginning Perl Third Edition PHẦN 8
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
của tôi $ Galileo = người (". 9,81 Pisa Apts" Họ = "Galilei", firstname = "Galileo", địa chỉ =, nghề nghiệp = "bombadier",); Ngoài ra còn có một cú pháp gọi phương pháp, mà bạn sẽ đặc biệt được sử dụng với xây dựng: $ Galileo = new Person (.); các nhà xây dựng sẽ kiểm tra các đối số có thể chấp nhận được, | CHAPTER 13 OBJECT-ORIENTED PERL my galileo lastname firstname address occupation Person- new Galilei Galileo 9.81 Pisa Apts. bombadier There s also another syntax for calling methods which you ll particularly see used with the constructor my galileo new Person . The constructor will now check that the arguments are acceptable do any conversion it requires and create a hash reference bless it and return it to us. More on this later in this chapter. Destructors When the object is no longer in use when it s a lexical variable that goes out of scope Perl automatically destroys it. However before doing so Perl will attempt to call a method named DESTROY . If the class provides this method it should be responsible for any tasks that need to be performed before the object is disposed of. For instance your FTP session object will want to ensure that it has closed the connection to the remote server. An Example It is now time for an example. Let s start off by using a class that is already created for us Net FTP.2 This class also known as a module allows you to create objects that transfer files to and from an FTP server. The following example will connect to the CPAN and download the README.html file. This example will illustrate some of the buzz words mentioned previously. usr bin perl ftp.pl use strict use Net FTP my ftp Net FTP- new ftp.cpan.org or die Couldn t connect @ n ftp- login anonymous ftp- cwd pub CPAN ftp- get README.html ftp- close 2 This code was written by Graham Barr. Thanks Graham 292 CHAPTER 13 OBJECT-ORIENTED PERL Network and firewalls permitting this should retrieve the file although it may take some time. Here is the proof on a Windows machine perl ftp.pl dir READNE.html README 1 HTM 2 902 . README.html The first line of interest in this program is use Net FTP This line finds the file that contains the definition of the Net FTP class as you learned in Chapter 12 its name happens to be FTP.pm and it is located in a directory named Net and compiles it .