[英]Initializing global variable on Perl module (with BEGIN block) include
I'm trying to initialize a global variable from a Perl module that has a BEGIN
block, but I can't manage to get it work. 我正在尝试从具有
BEGIN
块的Perl模块初始化全局变量,但是我无法使其正常工作。
This is the Perl module 这是Perl模块
Package A::B;
our $var;
BEGIN{
$var ||= "/some/default/path";
#create/access files/folders in $var
}
This is my CGI script 这是我的CGI脚本
use A::B;
$A::B::var = "/correct/path";
But #error
returned because $var is not the correct path 但是
#error
返回了,因为$ var不是正确的路径
The BEGIN
block is being executed before the correct path is assigned to $var
. 在将正确的路径分配给
$var
之前,正在执行BEGIN
块。 Is there a way to work around this without having to remove code from the BEGIN
block? 有没有一种解决方法,而不必从
BEGIN
块中删除代码?
BEGIN { $A::B::var = "/correct/path" }
use A::B;
This answer is unsatisfying to me, but I can't think of any other way, given how your A::B is designed. 这个答案令我不满意,但是考虑到您的A :: B是如何设计的,我想不出任何其他方式。 Ideally a module doesn't depend on who is using it or how often, but this module can really only be used once.
理想情况下,一个模块不取决于使用它的人或使用频率,但实际上该模块只能使用一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.