Perl 常量定义在专用包中

发布于 2024-10-30 16:26:13 字数 838 浏览 0 评论 0原文

我想为主 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

另类 2024-11-06 16:26:13

您遇到的问题是脚本中运行时和编译时之间的交互。任何 use 声明都有一个隐式的 BEGIN {...} 块,这意味着它在编译时发生。对 %setup 的赋值发生在运行时,在声明常量之后。解决此问题的方法是声明变量,然后在 BEGIN 块中对其执行赋值。这样,该变量将在调用 useconstant... 时定义:

use my_common_declarations 'debugme';

my (%setup, $DEBUGME);  # declare variables with file scope

BEGIN {
    %setup   = debugme;                # assign to variables at compile time
    $DEBUGME = $setup{setup}{debugme};
}

use constant true => $setup{setup}{true}; # %setup is defined now

或者,由于您要导出一个返回散列的函数,为什么不让它返回一个散列引用,然后您可以将代码编写为:

use my_common_declarations 'debugme';

use constant true => debugme->{setup}{true};

在这种情况下,由于 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 implicit BEGIN {...} 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 a BEGIN block. That way, the variable will be defined by the time use constant ... is called:

use my_common_declarations 'debugme';

my (%setup, $DEBUGME);  # declare variables with file scope

BEGIN {
    %setup   = debugme;                # assign to variables at compile time
    $DEBUGME = $setup{setup}{debugme};
}

use constant true => $setup{setup}{true}; # %setup is defined now

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:

use my_common_declarations 'debugme';

use constant true => debugme->{setup}{true};

In that case, since debugme is imported by the first use statement at compile time, it is available in the use 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文