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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。