tailieunhanh - Beginning Perl Third Edition PHẦN 8

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 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 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 file. This example will illustrate some of the buzz words mentioned previously. usr bin perl use strict use Net FTP my ftp Net FTP- new or die Couldn t connect @ n ftp- login anonymous ftp- cwd pub CPAN ftp- get 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 dir README 1 HTM 2 902 . 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 and it is located in a directory named Net and compiles it .