1、XXX.pm 文件里面的第一行要是:package  XXX;
2、要有构造函数 sub new,实现如下:
sub new {
my $class = shift; # Get the request class name
my $self = {};
my ($name)=@_;
my $self = {
"name" =>$name
};
bless $self, $class; # Use class name to bless() reference
return $self;
} 3、接着用sub YYY{} 定义自己的函数
例如:
sub setBeanType{
my ($class, $name) = @_;//传进来的第一个参数是类似c++的this指针,第二个才是真正的参数
$class->{'Bean'} = $name;
print "Set bean to $name \n";
} [root@wx03 test]# cat p1.pm
package p1;
use Data::Dumper;
sub new {
my $self = {};
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my ($name)=@_;
my $self = {
"name" =>$name
};
bless $self, $class; # Use class name to bless() reference
print "111111111111111111\n";
$str=Dumper($self);
print "\$str is $str\n";
return $self; }; sub setBeanType{
my ($self, $name) = @_;##//传进来的第一个参数是类似c++的self指针,第二个才是真正的参数
$self->{'Bean'} = $name;
print "Set bean to $name \n";
$str=Dumper($self);
print "\$str is $str\n";
}; 1; [root@wx03 test]# cat p1.pm
package p1;
use base qw(p2);
use Data::Dumper;
sub new {
my $self = {};
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my ($name)=@_;
my $self = {
"name" =>$name
};
bless $self, $class; # Use class name to bless() reference
print "111111111111111111\n";
$str=Dumper($self);
print "\$str is $str\n";
return $self; }; sub setBeanType{
my ($self, $name) = @_;##//传进来的第一个参数是类似c++的self指针,第二个才是真正的参数
$self->{'Bean'} = $name;
print "Set bean to $name \n";
$str=Dumper($self);
print "\$str is $str\n";
}; 1; [root@wx03 test]# cat p1.pl
unshift(@INC,"/root/test");
require p1;
$ua=p1->new('lily');
print "2222222222222222\n";
$str=$ua->setBeanType(scan); [root@wx03 test]# perl p1.pl
111111111111111111
$str is $VAR1 = bless( {
'name' => 'lily'
}, 'p1' ); 2222222222222222
Set bean to scan
$str is $VAR1 = bless( {
'Bean' => 'scan',
'name' => 'lily'
}, 'p1' ); 不断的填充这个对象: 使用基类:
use base 是面向对象编程时,用来描述“基类”的:

perl 面向对象 use base的更多相关文章

  1. Perl面向对象(1):从代码复用开始

    官方手册:http://perldoc.perl.org/perlobj.html 本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象 ...

  2. Perl面向对象(2):对象

    本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. 已有的代码结构 现在有父类Animal ...

  3. Perl 面向对象编程的两种实现和比较:

    <pre name="code" class="html">https://www.ibm.com/developerworks/cn/linux/ ...

  4. perl面向对象

    来源: http://www.cnblogs.com/itech/archive/2012/08/21/2649580.html Perl面向对象     首先让我们来看看有关 Perl 面向对象编程 ...

  5. Perl面向对象(3):解构——对象销毁

    本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. perl中使用引用计数的方式管理内存, ...

  6. perl 面向对象编程

    今天看到一个perl面向对象编程的例子,充分体现了如何对数据进行封装: 自己模仿写一个读取配置文件的例子, 配置文件的内容如下 samtools_binary = /usr/bin/samtools ...

  7. perl 面向对象demo

    Vsftp:/root/perl/17# cat Critter.pm package Critter; sub new { my $self = {}; my $invocant = shift; ...

  8. perl 面向对象 new方法

    [root@wx03 test]# cat Scan.pm package Scan; sub new{ my $class = shift; my $self={ 'a'=>11, 'b'=& ...

  9. C# 面向对象的base的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

随机推荐

  1. 利用GDataXML解析XML文件

    1.导入GDataXMLNode.h 和 GDataXMLNode.m文件 2.导入libxml2库文件 3.工程target下Bulid Settings  搜索search 找到Hearder S ...

  2. Nginx阅读笔记(四)之root和alias

    nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri ...

  3. LinuxC安装gcc

    使用centos进行C编程的时候使用gcc hello.c提示 bash:gcc:command not found 此时需要给Linux安装gcc命令如下 1 yum -y install gcc ...

  4. Next SIEM

    http://security.ctocio.com.cn/76/12715576.shtml http://yepeng.blog.51cto.com/3101105/1155802/ http:/ ...

  5. Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer'

    @Configuration public class HttpSessionConfig { @Bean public static ConfigureRedisAction configureRe ...

  6. 关于String和StringBuffer的理解问题:指针、变量的声明、变量的值的变化

    问题描述: 首先,看一个小的测试程序: public static void main(String[] args) { testStringBuffer test = new testStringB ...

  7. paip.php-gtk 桌面程序 helloworld总结

    paip.php-gtk 桌面程序 helloworld总结 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.cs ...

  8. 解决 Xcode7 中多个模拟器的办法

    转自: http://www.oschina.net/code/snippet_196012_50574 1.关闭xcode 2.终端输入 sudo killall -9 com.apple.Core ...

  9. iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)

    版权声明:本文为博主原创文章,转载请声明出处:http://blog.csdn.net/jinnchang 1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系.每个 iOS 应用程 ...

  10. 在VS上配置OpenCV

    这几篇帖子讲的挺仔细的,而且不坑,结合看看就没问题了~~ http://www.cnblogs.com/cuteshongshong/p/4057193.html http://my.phirobot ...