PERLBOOT(7) Perl Programmers Reference Guide PERLBOOT(7) NAME perlboot - DESCRIPTION , perl, perlobj , , perltoot, perl. , ,. (perlsub), (perlref et. seq.), () (perlmod), ,. If we could talk to the animals... : sub Cow::speak { print "a Cow goes moooo!\n"; } sub Horse::speak { print "a Horse goes neigh!\n"; } sub Sheep::speak { print "a Sheep goes baaaah!\n" } Cow::speak; Horse::speak; Sheep::speak; : a Cow goes moooo! a Horse goes neigh! a Sheep goes baaaah! . , , . : # Cow::speak, Horse::speak, Sheep::speak @pasture = qw(Cow Cow Horse Sheep Sheep); foreach $animal (@pasture) { &{$animal."::speak"}; } : a Cow goes moooo! a Cow goes moooo! a Horse goes neigh! a Sheep goes baaaah! a Sheep goes baaaah! . . "no strict subs" , . ? . ? Introducing the method invocation arrow , "Class->method" ()"Class" "method" (Here, "Class" is used in its "category" meaning, not its "scholastic" meaning.) ,. ,: # Cow::speak, Horse::speak, Sheep::speak as before Cow->speak; Horse->speak; Sheep->speak; : a Cow goes moooo! a Horse goes neigh! a Sheep goes baaaah! . ,,. , . : $a = "Cow"; $a->speak; # invokes Cow->speak ! , . , "use strict refs" . Invoking a barnyard : sub Cow::speak { print "a Cow goes moooo!\n"; } sub Horse::speak { print "a Horse goes neigh!\n"; } sub Sheep::speak { print "a Sheep goes baaaah!\n" } @pasture = qw(Cow Cow Horse Sheep Sheep); foreach $animal (@pasture) { $animal->speak; } , . . "speak" : "print" . "goes" "says" , . The extra parameter of method invocation : Class->method(@args) "Class::method" Class::method("Class", @args); (,"inheritance" ,). (). "Sheep" speaking : sub Sheep::speak { my $class = shift; print "a $class goes baaaah!\n"; } : sub Cow::speak { my $class = shift; print "a $class goes moooo!\n"; } sub Horse::speak { my $class = shift; print "a $class goes neigh!\n"; } $class . ,. ? . . Calling a second method to simplify things "speak" "sound". . { package Cow; sub sound { "moooo" } sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n" } } , "Cow->speak" , "speak" "Cow" $class. "Cow->sound" , "moooo". "Horse" ? { package Horse; sub sound { "neigh" } sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n" } } . CowHorse "speak" ? ,! Inheriting the windpipes , "Animal", "speak": { package Animal; sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n" } } , "inherits" "Animal" , : { package Cow; @ISA = qw(Animal); sub sound { "moooo" } } @ISA . . "Cow->speak" ? , Perl. , "Cow". Perl "Cow::speak". , Perl @Cow::ISA. , "Animal" Perl "Animal" "speak", "Animal::speak". , , . "Animal::speak" , $class "Cow" (). "$class->sound" , "Cow->sound" , , @ISA. ! @ISA @ISA ( "is a" "ice-uh"), "Cow" ("is a") "Animal" ,, , . "Animal" @ISA, . ,, @ISA . , @ISA (), . "use strict", @ISA, , ("my"). "my"(),. : @Cow::ISA = qw(Animal); : package Cow; use vars qw(@ISA); @ISA = qw(Animal); , : package Cow; use Animal; use vars qw(@ISA); @ISA = qw(Animal); : package Cow; use base qw(Animal); . Overriding the methods , : # Animal package from before { package Mouse; @ISA = qw(Animal); sub sound { "squeak" } sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n"; print "[but you can barely hear it!]\n"; } } Mouse->speak; : a Mouse goes squeak! [but you can barely hear it!] , "Mouse" speak , "Mouse->speak" "Animal->speak". "overriding". , "Mouse" "Animal", "speak" "Mouse" . "Animal->speak" , . "Mouse" "Animal" ,? ! , "Animal::speak" : # Animal package from before { package Mouse; @ISA = qw(Animal); sub sound { "squeak" } sub speak { my $class = shift; Animal::speak($class); print "[but you can barely hear it!]\n"; } } $class ("Mouse") "Animal::speak" , . ? , "Animal->speak", "Animal" "Mouse" , "sound" , . , "Animal::speak" . "Animal::speak" , @Animal::ISA ? , . , "Animal" . , @ISA "speak" "Animal" . , . Starting the search from a different place Perl: # same Animal as before { package Mouse; # same @ISA, &sound as before sub speak { my $class = shift; $class->Animal::speak; print "[but you can barely hear it!]\n"; } } . , "Animal" "speak", "Animal" . $class, "speak" "Mouse::sound" . . @ISA . , "Mouse" @ISA, "speak". ,? The SUPER way of doing things SUPER "Animal" "SUPER" , (@ISA): # same Animal as before { package Mouse; # same @ISA, &sound as before sub speak { my $class = shift; $class->SUPER::speak; print "[but you can barely hear it!]\n"; } } "SUPER::speak" @ISA "speak", $class @ISA Where we're at so far... : Class->method(@args); : $a = "Class"; $a->method(@args); : ("Class", @args) Class::method("Class", @Args); "Class::method", @Class::ISA () "method" ,. , ,(),,. , ,. , , . A horse is a horse, of course of course -- or is it? ----? "Animal" "Horse" : { package Animal; sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n" } } { package Horse; @ISA = qw(Animal); sub sound { "neigh" } } "Horse->speak" "Animal::speak" "Horse::sound" : a Horse goes neigh! . , . , ? , . . ",instance" . . Perl, , ,: my $name = "Mr. Ed"; my $talking = \$name; $talking ( $name ), "bless": bless $talking, Horse; "Horse" . , $talking "Horse" . , . , . Invoking an instance method . , $talking : my $noise = $talking->sound; "sound", Perl $talking blessed (). , $talking. (, .) ,: Perl , "Horse", . , "Horse::sound" (), : Horse::sound($talking) , . "neigh", $noise . Horse::sound, @Horse::ISA . (blessed)(). Accessing the instance data ,. : { package Horse; @ISA = qw(Animal); sub sound { "neigh" } sub name { my $self = shift; $$self; } } ,: print $talking->name, " says ", $talking->sound, "\n"; "Horse::name" , @_ $talking, shift $talking $self. ( $self, , .) , $self , "Mr. Ed", . : Mr. Ed says neigh. How to build a horse ,, . ,,"". ,, ? , Horse : { package Horse; @ISA = qw(Animal); sub sound { "neigh" } sub name { my $self = shift; $$self; } sub named { my $class = shift; my $name = shift; bless \$name, $class; } } , "named" : my $talking = Horse->named("Mr. Ed"); , "Horse::named" "Horse" "Mr. Ed". "bless" $name , $name . , . , "named", "Horse" . (). , Perl "new", "new" . ,. (,?) Inheriting the constructor "Horse" ? . , "Animal" ,:: { package Animal; sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n" } sub name { my $self = shift; $$self; } sub named { my $class = shift; my $name = shift; bless \$name, $class; } } { package Horse; @ISA = qw(Animal); sub sound { "neigh" } } , "speak" ? my $talking = Horse->named("Mr. Ed"); $talking->speak; : a Horse=SCALAR(0xaca42ac) goes neigh! ? "Animal::speak" , . ,,. Making a method work with either classes or instances . "ref" . , "undef". "name" : sub name { my $either = shift; ref $either ? $$either # it's an instance, return name : "an unnamed $either"; # it's a class, return generic } , "?:" (dereference). . $either : my $talking = Horse->named("Mr. Ed"); print Horse->name, "\n"; # prints "an unnamed Horse\n" print $talking->name, "\n"; # prints "Mr Ed.\n" "speak" : sub speak { my $either = shift; print $either->name, " goes ", $either->sound, "\n"; } "sound" . ! Adding parameters to a method : { package Animal; sub named { my $class = shift; my $name = shift; bless \$name, $class; } sub name { my $either = shift; ref $either ? $$either # it's an instance, return name : "an unnamed $either"; # it's a class, return generic } sub speak { my $either = shift; print $either->name, " goes ", $either->sound, "\n"; } sub eat { my $either = shift; my $food = shift; print $either->name, " eats $food.\n"; } } { package Horse; @ISA = qw(Animal); sub sound { "neigh" } } { package Sheep; @ISA = qw(Animal); sub sound { "baaaah" } } : my $talking = Horse->named("Mr. Ed"); $talking->eat("hay"); Sheep->eat("grass"); : Mr. Ed eats hay. an unnamed Sheep eats grass. : Animal::eat($talking, "hay"); More interesting instances ? , . . '"(instance variables)""(member variables) ? (blessed). ,. my $bad = bless { Name => "Evil", Color => "black" }, Sheep; "$bad->{Name}" "Evil", "$bad->{Color}" "black". "$bad->name" name, ,. ,: ## in Animal sub name { my $either = shift; ref $either ? $either->{Name} : "an unnamed $either"; } "named" , : ## in Animal sub named { my $class = shift; my $name = shift; my $self = { Name => $name, Color => $class->default_color }; bless $self, $class; } "default_color" ? , "named" name, , . , : ## in Sheep sub default_color { "white" } , "Animal" "backstop" : ## in Animal sub default_color { "brown" } , "name" "named" "structure" , , "speak" . A horse of a different color ,. . ## in Animal sub color { $_[0]->{Color} } sub set_color { $_[0]->{Color} = $_[1]; } : $_[0] , "shift". (.) Mr. Ed: my $talking = Horse->named("Mr. Ed"); $talking->set_color("black-and-white"); print $talking->name, " is colored ", $talking->color, "\n"; : Mr. Ed is colored black-and-white Summary ,,,,(accessor). . getters,setters (destructor),(indirect object notation),(subclasses that add instance data),per-class data,(overloading),"isa" "can" ,("UNIVERSAL" class),. . ,. SEE ALSO perlobj (Perl,), perltoot (), perlbot (), ,Damian ConwayPerl (Object Oriented Perl) Class::Accessor, Class::Class, Class::Contract, Class::Data::Inheritable, Class::MethodMaker Tie::SecureHash COPYRIGHT Copyright (c) 1999, 2000 by Randal L. Schwartz and Stonehenge Consulting Services, Inc. Permission is hereby granted to distribute this document intact with the Perl distribution, and in accordance with the licenses of the Perl distribution; derived documents must include this copyright notice intact. Portions of this text have been derived from Perl Training materials originally appearing in the Packages, References, Objects, and Modules course taught by instructors for Stonehenge Consulting Services, Inc. and used with permission. Portions of this text have been derived from materials originally appearing in Linux Magazine and used with permission. redcandle 2001129 http://cmpp.linuxforum.net man man https://github.com/man-pages-zh/manpages- zh perl v5.8.3 2003-11-25 PERLBOOT(7)