Perl 常量定义在专用包中
我想为主 Perl 程序和其他包的所有常见声明设置一个专用包,而不是在每个标头中重复这些声明。我肯定弄错了,但无法弄清楚其背后的理由:
让我们假设:
- 我已在my_common_declarations.pm包中设置了我的通用数据。
- 我想在另一个包中使用这些数据,例如 my_perl_utils.pm。
#!/usr/bin/perl -w
package my_perl_utils;
use parent qw(Exporter);
our @EXPORT_OK = qw(f1 f2);
use my_common_declarations qw(debugme);
my %setup = &debugme;
my $DEBUGME = $setup{setup}{debugme};
# This generates this error : "Use of uninitialized value"
use constant true => $setup{setup}{'true'};
print "=" x25, "\nDEBUG true :\nimport = " . $setup{setup}{'true'} . "\nconstant = " , true , "\n", "=" x25, "\n";
sub f1{
# some rationals using the true or false constants
}
sub f2{
}
1;
我无法成功地无错误地声明“true”常量。
我应该在主程序中仅导入一次公共声明包并在其中相应地声明常量,还是在需要该常量的每个包中重新声明它?
谢谢
I would like to set up a dedicated package for all common declarations to main perl program and other packages as well, without repeating these declarations in every headers. I get it wrong for sure but could not figure out the rationals behind that :
Let's assume :
- I have setup my common data within the package my_common_declarations.pm.
- I want to use these data within another package, my_perl_utils.pm for example.
#!/usr/bin/perl -w
package my_perl_utils;
use parent qw(Exporter);
our @EXPORT_OK = qw(f1 f2);
use my_common_declarations qw(debugme);
my %setup = &debugme;
my $DEBUGME = $setup{setup}{debugme};
# This generates this error : "Use of uninitialized value"
use constant true => $setup{setup}{'true'};
print "=" x25, "\nDEBUG true :\nimport = " . $setup{setup}{'true'} . "\nconstant = " , true , "\n", "=" x25, "\n";
sub f1{
# some rationals using the true or false constants
}
sub f2{
}
1;
I can't succeed in getting the 'true' constant declared without errors.
Shall I import the common declarations package only once in the main program and declare the constant accordingly in there, or redeclare it within each package where I need this constant ?
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的问题是脚本中运行时和编译时之间的交互。任何
use
声明都有一个隐式的BEGIN {...}
块,这意味着它在编译时发生。对%setup
的赋值发生在运行时,在声明常量之后。解决此问题的方法是声明变量,然后在BEGIN
块中对其执行赋值。这样,该变量将在调用useconstant...
时定义:或者,由于您要导出一个返回散列的函数,为什么不让它返回一个散列引用,然后您可以将代码编写为:
在这种情况下,由于
debugme
是在编译时由第一个use
语句导入的,因此它可以在use 常量中使用...
行。但是,既然您正在为公共声明声明一个包,为什么不将常量的定义移动到该包中,然后将诸如
true
之类的内容添加到该包的导出列表中呢?The problem you are running into is the interaction between runtime and compile time in your script. Any
use
declaration has an implicitBEGIN {...}
block around it, which means that it takes place at compile time. Your assignment to%setup
happens at runtime, after the constant has been declared. The fix for this is to declare the variable, and then perform the assignment to it in aBEGIN
block. That way, the variable will be defined by the timeuse constant ...
is called:Alternatively, since you are exporting a function that returns a hash, why not have it return a hash reference, then you could write your code as:
In that case, since
debugme
is imported by the firstuse
statement at compile time, it is available in theuse constant ...
line.But since you are declaring a package for common declarations, why not just move the definition of your constants into that package, and then add things like
true
to the export list for that package?