my $var = "aaaaaaaaa"; print "1---\$var is $var\n"; &fun1; &fun2; #子程序用的变量会被销毁 print "3---\$var2 is $var2\n"; print "4----\$var2 is $$var3\n"; sub fun1{ print "2----\$var is $var\n"}; ##外部变量在子程序中仍旧有用 sub fun2{ my $var2="bbbbbbbbbb"; # my $var3=\$var2; $var3=\$var2; print "\$var3 is $$var3\n"; }; #此时的结果: G:\运维系统开发\Tk\1>perl 4.pl 1---$var is aaaaaaaaa 2----$var is aaaaaaaaa $var3 is bbbbbbbbbb 3---$var2 is 4----$var2 is 要想子程序里的变量在外部生效,引用变量必须为全局变量 G:\运维系统开发\Tk\1>perl 4.pl 1---$var is aaaaaaaaa 2----$var is aaaaaaaaa $var3 is bbbbbbbbbb 3---$var2 is 4----$var2 is bbbbbbbbbb