??????? Perl???????????
????????
????Perl????
??????????
?????????
???????
?]????
?????????????
????????????
???????
???????????
??????
??????????????
?????Perl???????????
?????????????Perl???????????(OOP)?????????????????????????????????????????????
????????
???(module)????Perl??(pachage)??Perl???????????????????????????????x?????????
???http://www.metronet.com??perlmod??perlobj??
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Perl??????????????????????Perl5?????????????Z???????????
???????????????????????????Perl????????????????
.???????Perl????????????????????
.?????????Perl???????????????????????
.??????????????????????
????Perl????
???????????Perl???????????????????????Perl???????????????????????????????????Perl5???????????????????????C++?????????????????????????Perl4?????????????e??(::)??????????????????????
??????????????????????Perl???????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????x?1?????????????????????????????????????????????????y??????????????(')???????????????????????????????$class'$member????Perl5???????e??????????????????????$class'$member??$class::$member?????
??????????
??????????????????????????????????????????????Cocoa??????????????????????Java???????????????????????????????????Java??????????????????Java???????????????????????
???????????????Cocoa.pm??????(?????pm?????????????????Perl Module)???????????????????????????????????????????????????1;??????????????????????????????????1;?????????????Perl?????????????????e??????Perl????????????????????????
package Cocoa;
#
# Put "require" statements in for all required,imported packages
#
#
# Just add code here
#
1; # terminate the package with the required 1;
??????????????????????????????????????????????????new()????????????????????????new()?????????????????
?????????
???????????????????????????????????????????????????????????????????????????????y?????????bless()?????????
bless YeReference [,classname]
YeReference??????????????????????classname?????????????????????????????????????????
????????????????????????????????????????????????
sub new {
my $this = {}; # Create an anonymous hash, and #self points to it.
bless $this; # Connect the hash to the package Cocoa.
return $this; # Return the reference to the hash.
}
1;
{}??????????????/???????????????????????????????????????????$this??????bless()??????????????????????????Cocoa??????????????????????????????????????????
??new()?????????$this????????????????????????????????????????????????????????????Perl??????????????????????????????
$cup = new Cocoa;
???????????e???????????????
1 #!/usr/bin/perl
2 push (@INC,'pwd');
3 use Cocoa;
4 $cup = new Cocoa;
????????Perl???????????????????????????????????@INC??????????????????????????????????????t???????????????/home/test/scripts/????????????????????
push (@INC , "/home/test/scripts");
????????????????Cocoa.pm???????????????use??????Perl??@INC????????Cocoa.pm????????????????????????use???????????????????????new????????????????Perl?????????????????????????????????????????????????????????????
$cup = cocoa->new();
???????C??????????????e????????Cocoa????new()???????
$cup = Cocoa::new();
??????????????????????????Cocoa.pm???????????????????????????????????????????????????????????????????
???
1????????????????????????
2????????my???????????????????
3??????????????????local?????????????????????????????
4????????????????????????????
??????????Cocoa??????????
sub new {
my $this = {};
print "\n /* \n ** Created by Cocoa.pm \n ** Use at own risk";
print "\n ** Did this code even get pass the javac compiler? ";
print "\n **/ \n";
bless $this;
return $this;
}
??????????e????????????????????????????????????
sub new {
my $this = {}
bless $this;
$this->doInitialization();
return $this;
}
?????????????????????????????????????????????????????new?????????new????????????????
sub new {
my $class = shift; # Get the request class name
my $this = {};
bless $this, $class # Use class name to bless() reference
$this->doInitialization(); return $this;
}
??????????????????????????????????
Cocoa::new()
Cocoa->new()
new Cocoa
??????bless????????????????????bless??????????????bless????????????C??Pascal?????????????????????????????????????????????????????????????????n????????Perl?????????????????????
?????????????????????????Perl????bless??????????????????????????bless?????????????????????????????????????????????????????????
???????
???????????new()?????????????????????????????????????????????????????????????????new()????????????????????????
???????????????????????????????????????
?????????????
sub new {
my $type = shift;
my %parm = @_;
my $this = {};
$this->{'Name'} = $parm{'Name'};
$this->{'x'} = $parm{'x'};
$this->{'y'} = $parm{'y'};
bless $this, $type;
}
?????g??????????
sub new {
my $type = shift;
my %parm = @_;
my $this = [];
$this->[0] = $parm{'Name'};
$this->[1] = $parm{'x'};
$this->[2] = $parm{'y'};
bless $this, $type;
}
?????????????????????????
$mug = Cocoa::new( 'Name' => 'top','x' => 10,'y' => 20 );
??????=>????????????????????=>?????????????????
print "Name=$mug->{'Name'}\n";
print "x=$mug->{'x'}\n";
print "y=$mug->{'y'}\n";
?]????
Perl????????????????Perl????????????????????????????Perl????????I???????????????q??????????????????????????????Perl?????????????????????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????shift??????self??this???????????????????????
1. sub nameLister {
2. my $this = shift;
3. my ($keys ,$value );
4. while (($key, $value) = each (%$this)) {
5. print "\t$key is $value.\n";
6. }
7. }
?????????????
???????????????Cocoa.pm??????????????????????????????????Cocoa.pm?????????????????????????Exporter????????????????????????
require Exporter;
@ISA = qw (Exporter);
??????????Exporter.pm??????Exporter????????@ISA???????????????????????????????????@EXPORT?????????????????????????closeMain??declareMain?????????
@EXPORT = qw (declareMain , closeMain);
Perl??????????@ISA?????????@ISA?????????????????????????????Perl??????????????????????????@INC?????????@INC?????????????????@ISA???A????(??)????????????????????????????@ISA????????@ISA??????????????????
????????????????????????????@ISA???E??????????????????@ISA???????????Perl???AUTOLOAD()??????????????????????????????????sub???^?????AUTOLOAD???????????use Autoload;??????autoload.pm????AUTOLOAD??????????????Perl???????????????????AUTOLOAD???????Perl???UNIVERSAL??????????????????????Perl??????????????????????????
????????????
????????????????????????????????????????????????????????????????????????????????????????????????????Cocoa?????????????????????
package Cocoa;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(setImports, declareMain, closeMain);
#
# This routine creates the references for imports in Java functions
#
sub setImports{
my $class = shift @_;
my @names = @_;
foreach (@names) {
print "import " . $_ . ";\n";
}
}
#
# This routine declares the main function in a Java script
#
sub declareMain{
my $class = shift @_;
my ( $name, $extends, $implements) = @_;
print "\n public class $name";
if ($extends) {
print " extends " . $extends;
}
if ($implements) {
print " implements " . $implements;
}
print " { \n";
}
#
# This routine declares the main function in a Java script
#
sub closeMain{
print "} \n";
}
#
# This subroutine creates the header for the file.
#
sub new {
my $this = {};
print "\n /* \n ** Created by Cocoa.pm \n ** Use at own risk \n */ \n";
bless $this;
return $this;
}
1;
????????????????Perl?????????????????????????????Java applet??????????????
#!/usr/bin/perl
use Cocoa;
$cup = new Cocoa;
$cup->setImports( 'java.io.InputStream', 'java.net.*');
$cup->declareMain( "Msg" , "java.applet.Applet", "Runnable");
$cup->closeMain();
?????????????????Msg??Java applet???????(extend)??java.applet.Applet?????????????(runnable)????????????????????????
Cocoa::setImports($cup, 'java.io.InputStream', 'java.net.*');
Cocoa::declareMain($cup, "Msg" , "java.applet.Applet", "Runnable");
Cocoa::closeMain($cup);
???????????
/*
** Created by Cocoa.pm
** Use at own risk
*/
import java.io.InputStream;
import java.net.*;
public class Msg extends java.applet.Applet implements Runnable {
}
????????->???????????????????????????????????????????????$cup->setImports( 'java.io.InputStream', 'java.net.*');???e??????Cocoa::setImports($cup, 'java.io.InputStream', 'java.net.*');?????????????Cocoa::setImports $cup, 'java.io.InputStream', 'java.net.*' ;
???????
????????????????????????????????????????????????????????Espresso??Qava???????????grind????????::????????????Qava???????
$mess = Qava::grind("whole","lotta","bags");
Qava::grind($mess, "whole","lotta","bags");
?????????????????????????????????????????????????????????????????
$method = $local ? "Qava::" : "Espresso::";
$cup->{$method}grind(@args);
???????????
Perl??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????DESTROY()??????DESTROY()??????????????????????????????????DESTROY()?????????????????DESTROY()??????Perl???????????????????????????????????bless??DESTROY()??????????????????DESTROY()??????????????????????????????????????????????????
?????????????????DESTROY()???????????????????????
sub DESTROY {
#
# Add code here.
#
}
???????????Perl???????????????????????????????ʦ??????????????????????????????????????????????????Perl??????????????????????????????????????????????????????UNIX?????????????????????????????????????????????????
??????
???????@ISA????????????????????څ????????????????Bean.pm??Coffee.pm??????Coffee.pm???Bean.pm???????????????????????????????????????????????????????????????????????????????????
Bean.pm????????
package Bean;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(setBeanType);
sub new {
my $type = shift;
my $this = {};
$this->{'Bean'} = 'Colombian';
bless $this, $type;
return $this;
}
#
# This subroutine sets the class name
sub setBeanType{
my ($class, $name) = @_;
$class->{'Bean'} = $name;
print "Set bean to $name \n";
}
1;
????????$this???????????????????????'Bean'???????'Colombian'??????setBeanType()??????'Bean'??????????$class???????????????????
Coffee.pm????????
1 #
2 # The Coffee.pm file to illustrate inheritance.
3 #
4 package Coffee;
5 require Exporter;
6 require Bean;
7 @ISA = qw(Exporter, Bean);
8 @EXPORT = qw(setImports, declareMain, closeMain);
9 #
10 # set item
11 #
12 sub setCoffeeType{
13 my ($class,$name) = @_;
14 $class->{'Coffee'} = $name;
15 print "Set coffee type to $name \n";
16 }
17 #
18 # constructor
19 #
20 sub new {
21 my $type = shift;
22 my $this = Bean->new(); ##### <- LOOK HERE!!! ####
23 $this->{'Coffee'} = 'Instant'; # unless told otherwise
24 bless $this, $type;
25 return $this;
26 }
27 1;
??6??require Bean;????????Bean.pm?????????????????????setCoffeeType()???????t??????$class->{'Coffee'}????????????new()??$this???Bean.pm????????????????????????????????????????????????????????????????????Bean.pm????????????????????????????????
my $this = {}; #????
my $this = $theSuperClass->new(); #???
?????????????????????
1 #!/usr/bin/perl
2 push (@INC,'pwd');
3 use Coffee;
4 $cup = new Coffee;
5 print "\n -------------------- Initial values ------------ \n";
6 print "Coffee: $cup->{'Coffee'} \n";
7 print "Bean: $cup->{'Bean'} \n";
8 print "\n -------------------- Change Bean Type ---------- \n";
9 $cup->setBeanType('Mixed');
10 print "Bean Type is now $cup->{'Bean'} \n";
11 print "\n ------------------ Change Coffee Type ---------- \n";
12 $cup->setCoffeeType('Instant');
13 print "Type of coffee: $cup->{'Coffee'} \n";
???????????????
-------------------- Initial values ------------
Coffee: Instant
Bean: Colombian
-------------------- Change Bean Type ----------
Set bean to Mixed
Bean Type is now Mixed
------------------ Change Coffee Type ----------
Set coffee type to Instant
Type of coffee: Instant
??????????????????????????????????'Bean'??'Coffee'????????????????????????????????
???????????????????????Coffee.pm??????????makeCup()??????????
sub makeCup {
my ($class, $cream, $sugar, $dope) = @_;
print "\n================================== \n";
print "Making a cup \n";
print "Add cream \n" if ($cream);
print "Add $sugar sugar cubes\n" if ($sugar);
print "Making some really addictive coffee ;-) \n" if ($dope);
print "================================== \n";
}
?????????????????????????????????????????????????
1 #!/usr/bin/perl
2 push (@INC,'pwd');
3 use Coffee;
4 $cup = new Coffee;
5 #
6 # With no parameters
7 #
8 print "\n Calling with no parameters: \n";
9 $cup->makeCup;
10 #
11 # With one parameter
12 #
13 print "\n Calling with one parameter: \n";
14 $cup->makeCup('1');
15 #
16 # With two parameters
17 #
18 print "\n Calling with two parameters: \n";
19 $cup->makeCup(1,'2');
20 #
21 # With all three parameters
22 #
23 print "\n Calling with three parameters: \n";
24 $cup->makeCup('1',3,'1');
???????????
Calling with no parameters:
==================================
Making a cup
==================================
Calling with one parameter:
==================================
Making a cup
Add cream
==================================
Calling with two parameters:
==================================
Making a cup
Add cream
Add 2 sugar cubes
==================================
Calling with three parameters:
==================================
Making a cup
Add cream
Add 3 sugar cubes
Making some really addictive coffee ;-)
==================================
???????????makeCup()???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????M??????
????????????????
???????????????????????????????????????????????????????????????????????????Bean.pm????????printType()??????????
sub printType {
my $class = shift @_;
print "The type of Bean is $class->{'Bean'} \n";
}
????????@EXPORT???????????
@EXPORT = qw ( setBeanType , printType );
????????????printType()???????????????
$cup->Coffee::printType();
$cup->printType();
$cup->Bean::printType();
??????????
The type of Bean is Mixed
The type of Bean is Mixed
The type of Bean is Mixed
????????????????????????????printType()?????????????????????????????????????????????printType()????????????Coffee.pm????????
#
# This routine prints the type of $class->{'Coffee'}
#
sub printType {
my $class = shift @_;
print "The type of Coffee is $class->{'Coffee'} \n";
}
????????@EXPORT???
@EXPORT = qw(setImports, declareMain, closeMain, printType);
????????????????
The type of Coffee is Instant
The type of Coffee is Instant
The type of Bean is Mixed
?????????????Bean::???????????????????????????????????
???????????????????????????????????????????????SUPER::????????????????$this->SUPER::function(...argument list...); ????????@ISA?????????????????SUPER::?IBean::?????$cup->SUPER::printType(); ?????????????????
The type of Bean is Mixed
?????Perl???????????
OOP??????????????????OOP???????????????????????Perl???????????my?????????????????????Perl????????????????????????????????????????????????????????????????????????????????????????????
???1??????????????????????????
2????????????????????????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????local()????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????????????????
use coffee::Bean;
???????????????@INC????????????Coffee?????????Bean.pm?????????Bean.pm???./Coffee???????????????????use????????????????????????????????????????????????
use Another::Sub::Menu;
????????????????
./Another/Sub/Menu.pm
????? ????? ??
paper | appdir | ssv
ŷ