perl – 使用require访问全局变量的值?

perl – 使用require访问全局变量的值?,第1张

概述是否可以使用require在另一个perl脚本中访问声明的全局变量的值? 例如. Config.pl #!/usr/bin/perluse warnings;use strict;our $test = "stackoverflow" Main.pl #!/usr/bin/perluse warnings;use stricts;require "Config.pl"print 是否可以使用require在另一个perl脚本中访问声明的全局变量的值?

例如.

Config.pl

#!/usr/bin/perluse warnings;use strict;our $test = "stackoverflow"

Main.pl

#!/usr/bin/perluse warnings;use stricts;require "Config.pl"print "$test\n";print "$config::test\n";
解决方法 当然.你建议的方式几乎可行.尝试:

Config.pl:

use warnings;use strict;our $test = "stackoverflow";

和主程序:

#!/usr/bin/perluse warnings;use strict;  require "Config.pl";our $test;print "$test\n";

当您调用require时,该文件将在与调用者相同的命名空间中执行.因此,如果没有任何名称空间或我的声明,任何分配的变量都将是全局变量,并且对于脚本是可见的.

总结

以上是内存溢出为你收集整理的perl – 使用require访问全局变量的值?全部内容,希望文章能够帮你解决perl – 使用require访问全局变量的值?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1260531.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存