Perl语言入门和提高12

Perl语言入门和提高12

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Spring, 2008 Perl 语言高级编程专题 Lesson 12 周晓方 courses@xfzhou.homeftp.org Spring, 2008 2 To understand Perl-OOP , you must understand perl-ref . These on-line documents are talking about Perl-OOP: perlboot : tutorial from a very special point of view. perltoot : Tom's object-oriented tutorial for Perl, Must read. perltootc : Tom's words on Class Data in Perl perlobj : The official document about Perl-Objects perlbot : Bag'o Object Tricks perltie : Tie variables with objects, very interesting. Class is a namespace ( i.e. A function in a Class is usually called a ' METHOD '. Perl searchs for a method hierachically in @ISA tree, track back to a hidden base class called ' UNIVERSAL '. Turns to ' AUTOLOAD ' method if none's found. Object is an instance of Class. An obj. belongs to a Class . Spring, 2008 3 Constructor can be any name, usually ' new '. Constructor returns a ' bless 'ed hash-ref. Destructor is always named 'DESTROY'. Perl will call it automatically when necessory. Destructor function is usually not necessary at all. Comp.pm package Comp; use vars qw($id); $id = 0; sub new($$) { my($type) = shift; # The hidden argument ' Comp ' my($x, $y) = @_; # Two given argument $x = 0 if not $x; $y = 0 if not $y; $id++; my($this) = # This is the obj data {x=>$x, y=>$y, id=>$id}; print "Got a new #$id :\t($x, $y).\n"; bless $this, $type; # Must bless with ' Comp ' } # sub DESTROY { …… } # 十有八九不必写什么 DESTROY 函数 1; # 模块最后必须返回一个非零值 Spring, 2008 4 Improve our Constructor How to invoke a constructor? $obj = class -> new ( arguments ) What perl actually do? $obj = class :: new (' class ' , argument ) Call constructor like an object method We write: $objB = $objA -> new ( arguments ) L Perl does: $objB = class :: new ( $objA , arguments ) ref($obj) returns class name. Let's improve constructor: the main-code.pl use Comp; $a = Comp->new(); # ok $b = Comp::new(); # bad, missing class name. $c = Comp::new('Comp'); # Not perl style. Don't do that $d = $a->new(); # use with improved constructor 1; Improved Comp.pm sub new { my $proto = shift; my $type = ref($proto) || $proto; bless $this, $type; Spring, 2008 5 Object Data Object data returned by an constructor must be a blessed reference. It's usually a hash-ref . But also can be a array-ref (e.g. TK :: After ), a code-ref (very tricky, see examples in perltoot ), or even an simple scalar-ref . See example code 101 ~ 104. Array-ref Constructor my $this=[$x, $y, $id]; bless $this, $type; Scalar-ref Constructor my $this= \"$x:$y:$id"; bless $this, $type; Code-ref Constructor my $data={x=>$x,y=>$y, id=>$id}; my $this = sub { my $field = shift; if (@_){$data->{$field}=shift} return $data->{$field}; }; # 对象的数据对外完全不可见
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/27/2011 for the course CS Perl taught by Professor Guo during the Spring '09 term at Xiamen University.

Page1 / 3

Perl语言入门和提高12

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online