导入的 perl 变量导入没有值

发布于 2024-12-23 08:00:21 字数 836 浏览 3 评论 0原文

已解决:事实证明,我的问题根源在于我没有在 @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 技术交流群。

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

发布评论

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

评论(4

春庭雪 2024-12-30 08:00:21

我看到几个问题:

  • 您不应在 qw() 语法中使用逗号。 qw 获取每个空格分隔的短语并将其放入数组元素中。

这两者是相同的:

my @bar = qw(foo bar barfu);          #No commas!
my @bar = ("foo", "bar", "barfu");    #Commas Required

你有:

our @EXPORT_OK = qw(DEBUGVAR);

应该是:

our @EXPORT_OK = qw($DEBUGVAR);
  • 你应该使用较新的导出器语法:

这是较新的导出器语法:

package global_config;
use strict;
use warnings;

use Exporter 'import';   #Not "require". No need for "@ISA"

our @EXPORT_OK = qw(DEBUGVAR);

our $DEBUGVAR = "Hello, World";

1;    #Makes no real difference, but you shouldn't say "return 1". Just standard.
  • 最后,你在做什么导出变量?这只是一个不好的做法。
    • 现在导出任何东西都受到质疑——甚至是函数。它污染了用户的名称空间。 (至少您使用的是@EXPORT_OKAY)。查看 File::Spec。默认情况下,它的子例程使用完全限定的包名称。
    • 可通过完整包名称 $global_config::DEBUGVAR 访问相关变量,因此无需导出它。
    • 如果每个人都这样做怎么办?是的,你最后一次听说这个借口是在幼儿园,但它在这里也适用。想象一下,如果有多个模块导出 $DEBUGVAR

有多种方法可以解决您的困境,但最好的方法是使用面向对象的 Perl 来帮助设置此变量,甚至允许用户更改它。

package MyPackage;

use strict;
use warnings;
use feature qw(say);

sub new {
   my $class = shift;
   my $debug = shift;  #Optional Debug Value

   my $self = {};
   bless $self, $class;

   if (not defined $debug) {
      $debug = "Hello, world!";

   $self->Debug($debug);
   return $self;
}

sub Debug {
   my $self  = shift;
   my $debug = shift;

   if (defined $debug) {
      $self->{DEBUG} = $debug;
   }
   return $debug;
}

1;

要使用此模块,我只需创建一个新对象,然后将为我设置“调试”:

use strict;
use warnings;
use MyPackage               #No exporting needed

#Create an object w/ Debug value;
my $obj = MyPackage->new;   #Debug is the default.
say $obj->Debug;            #Prints "Hello, world!"

# Change the value of Debug
$obj->Debug("Foo!");
say $obj->Debug;            #Now prints "Foo!"

#Create a new object with a different default debug
$obj2 = MyPackage->new("Bar!");
say $obj2->Debug;           #Print "Bar!";

这解决了几个问题:

  • 它允许多个调试值,因为每个对象现在都有自己的值
  • 不用担心名称空间污染或访问包变量。同样,您所需要的一切都包含在对象本身中。
  • 由于复杂性隐藏在对象本身内部,因此调试问题变得更加容易。
  • 这是新的首选方法,因此您不妨习惯语法并能够阅读面向对象的 Perl 代码。你会越来越多地看到它。

I see several issues:

  • You should not use a comma in the qw() syntax. The qw takes each whitespace separated phrase and puts it in an array element.

These two are the same:

my @bar = qw(foo bar barfu);          #No commas!
my @bar = ("foo", "bar", "barfu");    #Commas Required
  • If you're exporting a variable, you need to put the sigil in front of it.

You have:

our @EXPORT_OK = qw(DEBUGVAR);

It should be:

our @EXPORT_OK = qw($DEBUGVAR);
  • You should use the newer Exporter syntax:

Here's the newer Exporter Syntax:

package global_config;
use strict;
use warnings;

use Exporter 'import';   #Not "require". No need for "@ISA"

our @EXPORT_OK = qw(DEBUGVAR);

our $DEBUGVAR = "Hello, World";

1;    #Makes no real difference, but you shouldn't say "return 1". Just standard.
  • Finally, what are you doing exporting variables? That's just a bad practice.
    • Exporting anything is now questioned -- even functions. It pollutes the user's namespace. (At least you're using @EXPORT_OKAY). Take a look at File::Spec. It uses fully qualified package names for its subroutines by default.
    • The variable in question is accessible via the full package name $global_config::DEBUGVAR, so there's no real need to export it.
    • What if everybody did it? Yes, you last heard of this excuse in kindergarten, but it applies here. Imagine if several modules exported $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.

package MyPackage;

use strict;
use warnings;
use feature qw(say);

sub new {
   my $class = shift;
   my $debug = shift;  #Optional Debug Value

   my $self = {};
   bless $self, $class;

   if (not defined $debug) {
      $debug = "Hello, world!";

   $self->Debug($debug);
   return $self;
}

sub Debug {
   my $self  = shift;
   my $debug = shift;

   if (defined $debug) {
      $self->{DEBUG} = $debug;
   }
   return $debug;
}

1;

To use this module, I simply create a new object, and Debug will be set for me:

use strict;
use warnings;
use MyPackage               #No exporting needed

#Create an object w/ Debug value;
my $obj = MyPackage->new;   #Debug is the default.
say $obj->Debug;            #Prints "Hello, world!"

# Change the value of Debug
$obj->Debug("Foo!");
say $obj->Debug;            #Now prints "Foo!"

#Create a new object with a different default debug
$obj2 = MyPackage->new("Bar!");
say $obj2->Debug;           #Print "Bar!";

This solves several issues:

  • It allows multiple values of debug because each object now has its own values
  • There is no worry about namespace pollution or accessing package variables. Again, all you need is contained in the object itself.
  • It's easier to debug issues since the complexity is hidden inside the objects themselves.
  • It's the new preferred method, so you might as well get use to the syntax and be able to read object oriented Perl code. You'll be seeing it more and more.
锦爱 2024-12-30 08:00:21

虽然从包中导出变量不一定是推荐的做法,但为此,您需要使用要导出的变量的实际名称。在本例中,它是 $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 not DEBUGVAR which would be the name of a subroutine.

In the script using the config module, you do not need to declare the $DEBUGVAR variable as our, since imported variables are exempt from strict vars.

浪菊怪哟 2024-12-30 08:00:21

你把这个名字弄混了,它看起来像:

use config_global ...

package global_config;

尽管人们会认为这会发出警告。除非你不使用警告...?

ETA:

our @EXPORT_OK = qw($DEBUGVAR);
                    ^  

此外,该变量有两个声明。调试时确实需要使用警告,否则,您将一事无成。

You have gotten the name mixed up, it looks like:

use config_global ...

package global_config;

Though one would think that would issue warnings. Unless you are not using warnings...?

ETA:

our @EXPORT_OK = qw($DEBUGVAR);
                    ^  

Also, you have two declarations on that variable. You really need to use warnings when debugging, otherwise, you'll never get anywhere.

昨迟人 2024-12-30 08:00:21

您确定要在此处使用逗号吗:

use config_global qw(config, DEBUGVAR);

另外,您没有导出配置,因此它可能会更好地工作,因为:

use config_global qw(DEBUGVAR);

我还将删除最后一个 our $DEBUGVAR; 因为它可能会将其设置为 undef (或者至少把它放在“使用”行之前)——但我对此不确定。

Are you sure you want a comma here:

use config_global qw(config, DEBUGVAR);

Also, you aren't exporting config, so it might work better as:

use config_global qw(DEBUGVAR);

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.

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