qw(:everything) 在 Perl 的 use 行上起什么作用?
这是一个令人尴尬的问题,但为什么这条线可以工作,而另一条线却不能呢?
工作线路:
use strict;
use warning;
use Wx qw(:everything);
my $dialog = Wx::MessageDialog->new(
$self,
"About test\n" . "Version 0.01\n",
"About Test",
wxOK | wxCENTRE
);
非工作线路:
use strict;
use warning;
use Wx;
my $dialog = Wx::MessageDialog->new(
$self,
"About test\n" . "Version 0.01\n",
"About Test",
wxOK | wxCENTRE
);
错误消息,来自非工作线路:
Bareword "wxOK" not allowed while "strict subs" in use at test.pl line 123.
Bareword "wxCENTRE" not allowed while "strict subs" in use at test.pl line 123.
BEGIN not safe after errors--compilation aborted at test.pl line 348.
This is an embarrassing question to ask but why does this line work while the other doesn't?
Working line:
use strict;
use warning;
use Wx qw(:everything);
my $dialog = Wx::MessageDialog->new(
$self,
"About test\n" . "Version 0.01\n",
"About Test",
wxOK | wxCENTRE
);
Non-working line:
use strict;
use warning;
use Wx;
my $dialog = Wx::MessageDialog->new(
$self,
"About test\n" . "Version 0.01\n",
"About Test",
wxOK | wxCENTRE
);
Error message, from non-working line:
Bareword "wxOK" not allowed while "strict subs" in use at test.pl line 123.
Bareword "wxCENTRE" not allowed while "strict subs" in use at test.pl line 123.
BEGIN not safe after errors--compilation aborted at test.pl line 348.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它相当于:
该代码会将 Wx 中的 ':everything' 导入到当前命名空间中。我的猜测是,Wx 使用 Exporter,并且在使用 ':everything' 调用时要导入一组内容。
您可以查看Wx 的来源和Exporter 的来源,尝试更深入地理解它。
我错过了你的工作/非工作示例。非工作项不起作用,因为
wxOK
和wxCENTRE
常量未导入到当前命名空间中。这是使用 Exporter 完成的,如上所述。It is the equivalent of this:
That code will import ':everything' from Wx into the current namespace. My guess is that Wx uses Exporter and has a group of things to import when called with ':everything'.
You can check the Wx's source and Exporter's source to try to make more sense of it.
I missed your working/non-working example. The non-working one doesn't work because the
wxOK
andwxCENTRE
constants are not imported into the current namespace. This is done using Exporter, as explained above.与 非常相似
所以
与相同
字符串
:everything
被传递到 Wx 的导出例程 (import
)。语义完全取决于模块,但我认为 Wx 将其定义为“导出我可能导出的所有内容”。大多数模块都使用:ALL
来实现这一点(如果它们提供的话)。看起来 Wx 不会导出任何东西,除非你要求,所以
wxOK
和wxCENTRE
不存在于你的命名空间中,除非你告诉 Wx 使用或
导出它们 供将来参考,请包含错误消息等,而不是简单地说某些内容不起作用。
is very similar to
So
is the same as
The string
:everything
is passed to the Wx's export routine (import
). The semantics are completely up to the module, but I presume Wx to defines it to be "export everything I can possibly export". Most modules use:ALL
for that, if they provide it at all.It looks like Wx doesn't export anything unless you ask, so
wxOK
andwxCENTRE
don't exist in your namespace unless you tell Wx to export them usingor
For future reference, please include error messages and such instead of simply saying something doesn't work.