导入的 perl 变量导入没有值
已解决:事实证明,我的问题根源在于我没有在 @EXPORT_OK 赋值中的 DEBUGVAR 前面加上 $ 以及“use config_global qw” (配置调试变量);”线。由于它没有引发错误,所以我无法知道这是问题所在。因此,解决方法是在这些点上将正确的语法放在变量前面。
所以我试图掌握编写和导入 perl 模块的技巧。我不知道为什么做到这一点如此困难,但我在这个看似微不足道的任务上遇到了很大的麻烦。这是我的模块的内容:
package global_config;
use strict;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(DEBUGVAR);
our ($DEBUGVAR);
our $DEBUGVAR = "Hello, World!";
return 1;
这是导入模块的 perl 脚本的内容:
use strict;
use config_global qw(config, DEBUGVAR);
our %config;
our $DEBUGVAR;
print "variable imported with value: ".$DEBUGVAR;
输出是“使用值导入的变量:”,没有其他内容。我的变量似乎正在失去它的价值。我做错了什么?
编辑:经过一番摆弄并打开警告后,我将问题隔离为 $DEBUGVAR 从未真正导入。当我通过 $config_global:DEBUGVAR 使用它时,它按预期工作。现在的问题是它没有导入到命名空间中。什么给?
SOLVED: As it turns out, my problem was rooted in the fact that I was not putting a $ in front of DEBUGVAR in the @EXPORT_OK assignment and the "use config_global qw(config DEBUGVAR);" line. Since it raises no error, I had no way to know this was the issue. So, the fix is to place the proper syntax in front of your variables at these points.
So I am trying to get the hang of writing and importing perl modules. I don't know why it was made so difficult to do this, but I am having a great deal of trouble with this seeimingly trivial task. Here is the contents of my module:
package global_config;
use strict;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(DEBUGVAR);
our ($DEBUGVAR);
our $DEBUGVAR = "Hello, World!";
return 1;
Here are the contents of my perl script that imports the module:
use strict;
use config_global qw(config, DEBUGVAR);
our %config;
our $DEBUGVAR;
print "variable imported with value: ".$DEBUGVAR;
The output is "variable imported with value:", and nothing else. My variable appears to be losing it's value. What am I doing wrong?
EDIT: After fiddling around a bit, and turning warnings on, I have isolated the issue to being that $DEBUGVAR is never actually imported. When I use it via $config_global:DEBUGVAR, it works as expected. The issue now is that it is not importing into the namespace. What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我看到几个问题:
qw()
语法中使用逗号。qw
获取每个空格分隔的短语并将其放入数组元素中。这两者是相同的:
你有:
应该是:
这是较新的导出器语法:
@EXPORT_OKAY
)。查看 File::Spec。默认情况下,它的子例程使用完全限定的包名称。$global_config::DEBUGVAR
访问相关变量,因此无需导出它。$DEBUGVAR
。有多种方法可以解决您的困境,但最好的方法是使用面向对象的 Perl 来帮助设置此变量,甚至允许用户更改它。
要使用此模块,我只需创建一个新对象,然后将为我设置“调试”:
这解决了几个问题:
I see several issues:
qw()
syntax. Theqw
takes each whitespace separated phrase and puts it in an array element.These two are the same:
You have:
It should be:
Here's the newer Exporter Syntax:
@EXPORT_OKAY
). Take a look at File::Spec. It uses fully qualified package names for its subroutines by default.$global_config::DEBUGVAR
, so there's no real need to export it.$DEBUGVAR
.There are several ways around your quandary, but the best is to use object oriented Perl to help set this variable, and even allow users to change it.
To use this module, I simply create a new object, and Debug will be set for me:
This solves several issues:
虽然从包中导出变量不一定是推荐的做法,但为此,您需要使用要导出的变量的实际名称。在本例中,它是
$DEBUGVAR
,而不是子例程的名称DEBUGVAR
。在使用配置模块的脚本中,您不需要将
$DEBUGVAR
变量声明为our
,因为导入的变量不受严格变量的约束。While exporting variables from a package isn't necessarily a recommended practice, to do so, you need to use the actual name of the variable you are exporting. In this case it is
$DEBUGVAR
and notDEBUGVAR
which would be the name of a subroutine.In the script using the config module, you do not need to declare the
$DEBUGVAR
variable asour
, since imported variables are exempt from strict vars.你把这个名字弄混了,它看起来像:
尽管人们会认为这会发出警告。除非你不使用警告...?
ETA:
此外,该变量有两个声明。调试时确实需要使用警告,否则,您将一事无成。
You have gotten the name mixed up, it looks like:
Though one would think that would issue warnings. Unless you are not using warnings...?
ETA:
Also, you have two declarations on that variable. You really need to use warnings when debugging, otherwise, you'll never get anywhere.
您确定要在此处使用逗号吗:
另外,您没有导出配置,因此它可能会更好地工作,因为:
我还将删除最后一个
our $DEBUGVAR;
因为它可能会将其设置为 undef (或者至少把它放在“使用”行之前)——但我对此不确定。Are you sure you want a comma here:
Also, you aren't exporting config, so it might work better as:
I'd also remove the last
our $DEBUGVAR;
since it might set it to undef (or at least put it before the "use" line) -- I am not sure about this though.