【发布时间】:2011-07-20 08:58:49
【问题描述】:
我目前正在尝试重新加载一个模块。我希望实现的目标是能够更改模块文件中已定义子例程中的某些内容,然后使用新定义重新加载该模块。
目前,我正在将test子例程中的打印语句更改为在等待子例程执行原始代码之后,在重新加载模块之前打印“这是一些不同的文本”。
但是,我目前收到的是消息:Subroutine test redefined at /Test/testmodule.pm line 9.
这正是我想要的,但是输出如下。
这是一些文字 在 /Test/testmodule.pm 第 9 行重新定义了子程序测试。 这是一些文字
我希望当模块重新加载时,它意识到子程序已被重新定义,下次执行测试子程序时,它将引用新定义而不是旧定义。
我已经搜索过有关重新加载模块的先前问题,但给出的答案是循环依赖项(包 A 使用 B,B 使用 A),或包中的命名空间冲突,但这不是这里的问题.我想要重新定义子例程,并使用新定义。
源代码: main.pl
#!/usr/bin/perl
use strict;
use warnings;
use Module::Reload::Selective;
use Test::testmodule;
while(1) {
test(); #run module's define subroutine
sleep(5); #stop terminal from being flooded too quickly
#Ensure that the module is reloaded
$Module::Reload::Selective::Options->{SearchProgramDir} = 1;
$Module::Reload::Selective::Options->{ReloadOnlyIfEnvVarsSet} = 0;
Module::Reload::Selective->reload(qw(Test::testmodule)); #reload!
}
源代码:testmodule.pm(在./Test/相对于main.pl)
#!/usr/bin/perl
use strict;
use warnings;
# allow exportation
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(test);
sub test {
print("this is some text\n"); # this line is edited in the source file to
# 'print("this is some different text\n");'
}
1;
任何对教程的指针或引用都会很棒。事实上,如果答案不是非常简单,不直接告诉我答案可以让我阅读您建议的材料并获得更好的整体理解。
所有需要的CPAN模块都已经安装好了,我可以确认testmodule.pm改完后写成功了。
操作系统:Scientific Linux CERN 6,内核版本 2.6.32-131.4.1.el6.x86_64
Perl:v5.10.1 (*) 专为x86_64-linux-thread-multi
非常感谢,
欧文。
【问题讨论】:
-
什么新定义?您正在重新加载相同的子程序,得到相同的输出。
-
感谢您的发帖。在第二段中,我提到我更改了 testmodule.pm 中的一行源代码。我应该编辑帖子以使其更加明确。感谢您提请我注意。
标签: perl perl-module