#!/usr/bin/perl use 5.010; package A; $a = 10; sub print_var{ $a += 10; } package B; $a = 20; package A; say $A::a; say &A::print_var(); say &{A::print_var}(); say "------通过符号引用的方式获取变量和函数------"; $pkg = 'A'; $varname = 'a'; $method_name = 'print_var'; say ${"$pkg::$varname"}; say ${"A::a"}; say &{"A::print_var"}(); say &{"$pkg::$method_name"}(); say "------------面向对象-------"; package Employee; sub new{ my($name,$age,$height) = @_; %emhash = ( name => $name, age => $age, height => $height, ); bless \%emhash,'Employee'; #在对象上标注包名 return \%emhash; } sub promote{ my($emp) = @_; $emp->{age} = $emp->{age}-10; } $employee = Employee::new("giggs",40,185); say $employee->{age};#40 Employee::promote($employee);#调用对象的实例方法 say $employee->{age};#30 $employee->promote();#调用对象的实例方法 say $employee->{age};#20 if(ref($employee)){#引用 say $employee->promote();#10 say ref($employee); }else{ Employee::promote($employee); } say "------------面向对象demo---------------------------------"; use packages::Cocoa; #$cup = new Cocoa; #构造对象 #$cup = Cocoa::new(); #$cup = Cocoa->new('giggs',40,180); #say $cup->{name};#访问属性 #say $cup->{age}; #say $cup->{height}; $cup = Cocoa->new(); #方法调用 ->操作符为间接调用 $cup->setImports("javax.xml.bind.annotation.XmlAccessType","javax.xml.bind.annotation.XmlAccessorType","javax.xml.bind.annotation.XmlType"); $cup->declareMain("DBUtils","SessionFactory","userImplements"); $cup->printContent(); $cup->closeMain(); #Cocoa::setImports($cup,"javax.xml.bind.annotation.XmlAccessType","javax.xml.bind.annotation.XmlAccessorType","javax.xml.bind.annotation.XmlType"); #Cocoa::declareMain($cup,"DBUtils","SessionFactory","userImplements"); #Cocoa::printContent($cup); #Cocoa::closeMain($cup); say +((1,2,3)[1,2,3])[1,2,3]; say "------------面向对象demo--继承----"; #类方法通过@ISA数组继承,变量的继承必须明确设定 use packages::Coffee; $cup = Coffee->new(); say "---init values---------"; say "Coffee:$cup->{Coffee}"; say "Bean:$cup->{Bean}"; say "----start change Coffee Type----------"; $cup->setCoffeeType("CCCoffee"); say "----Coffee type is now 《$cup->{Coffee}》----"; say "----start change Bean Type----------"; $cup->setBeanType("Mixed"); say "----Bean type is now《$cup->{Bean}》----"; say "-------------绑定-------------------------"; use packages::AC; tie $tem,'AC'; #tie 变量,类名,列表; $x = $tem; say $x; $tem = 20; #(tired $tem)->STORE(20); say $tem; untie $tem;#undef $tem;# $x = $tem; say $x;