使用严格时,perl 找不到子 class 方法
when using strict, perl can't find a child class method
我在 perl 中使用 strict 时遇到问题。我有一个名为 FlowStep 的父 class,我有一个子 classes FlowStep_*,我想从 FlowStep 继承它。当我 运行 时,Perl 会立即加载 FlowStep。根据用户希望执行的流程步骤 运行,有代码可以为该步骤动态加载特定包。例如,如果用户运行s "myFlowManager -step foo",制作的流程管理器代码将运行,确定步骤foo的包,并加载它。
所以基本上是一些类似这样的代码:
sub runPerlModule {
my $this = shift;
my $PMfullPath = $$this{"subPM"} ;
my $path = dirname($PMfullPath);
my $module = basename($PMfullPath, ".pm");
# dynamically load the module
push(@INC, $path);
eval "require $module";
# create a new object
my $obj = $module->new();
# call the child class's init code
$obj->init();
$obj->run();
}
一个示例流程步骤称为 FlowStep_RunTiming。在 FlowStep_RunTiming 中,我有以下内容:
use FlowStep;
use strict;
package FlowStep_RunTiming;
use base qw(FlowStep); #i think this is the only command needed to setup inheritance
sub new {
# some code to create
}
sub run {
# some code to run
}
1;
我在 FlowStep_RunTiming 中使用 strict 时遇到问题。如果 FlowStep_RunTiming 中没有语法错误,则没有问题。但是,如果 FlowStep_RunTiming 中有错字,那么当我 运行 程序时,perl 只是抱怨没有为 FlowStep_RunTiming.[=13 定义的 运行() 方法=]
包 FlowStep_RunTiming 是动态选择的,因此在 perl 可以对其进行 lint 之前开始执行。 perl 必须有一种方法来报告真正的问题,即报告语法错误和行号以及 FlowStep_RunTiming 中的错误。现在必须运行,失败,并通过肉眼发现语法错误。
我没有正确设置子 class 吗?有没有办法让 perl 报告真正的语法错误而不给出 运行() 未定义的 "false" 消息。任何人都有如何正确设置它的例子吗?
感谢您的帮助。
在 child 我这样做;服务是parent perl
use Service;
our @ISA= qw( Service );
sub new
{
my ($class, $args) = @_;
my $self = $class->SUPER::new($args );
bless $self, $class;
return $self;
}
sub prerun{ #something calling run
my ($self) = @_;
$self->run(); #self becomes the first argument to run
}
并且您需要将 class/object 传递给每个函数
在parent;
sub run
{
my ($self) = @_;
}
然后从child
将eval "require $module";
改为
unless (eval "require $module") {
die "Error in $module: $@";
}
或者在出现打字错误时做任何你想做的事情。
参见perldoc perlvar and perldoc -f eval。
我在 perl 中使用 strict 时遇到问题。我有一个名为 FlowStep 的父 class,我有一个子 classes FlowStep_*,我想从 FlowStep 继承它。当我 运行 时,Perl 会立即加载 FlowStep。根据用户希望执行的流程步骤 运行,有代码可以为该步骤动态加载特定包。例如,如果用户运行s "myFlowManager -step foo",制作的流程管理器代码将运行,确定步骤foo的包,并加载它。
所以基本上是一些类似这样的代码:
sub runPerlModule {
my $this = shift;
my $PMfullPath = $$this{"subPM"} ;
my $path = dirname($PMfullPath);
my $module = basename($PMfullPath, ".pm");
# dynamically load the module
push(@INC, $path);
eval "require $module";
# create a new object
my $obj = $module->new();
# call the child class's init code
$obj->init();
$obj->run();
}
一个示例流程步骤称为 FlowStep_RunTiming。在 FlowStep_RunTiming 中,我有以下内容:
use FlowStep;
use strict;
package FlowStep_RunTiming;
use base qw(FlowStep); #i think this is the only command needed to setup inheritance
sub new {
# some code to create
}
sub run {
# some code to run
}
1;
我在 FlowStep_RunTiming 中使用 strict 时遇到问题。如果 FlowStep_RunTiming 中没有语法错误,则没有问题。但是,如果 FlowStep_RunTiming 中有错字,那么当我 运行 程序时,perl 只是抱怨没有为 FlowStep_RunTiming.[=13 定义的 运行() 方法=]
包 FlowStep_RunTiming 是动态选择的,因此在 perl 可以对其进行 lint 之前开始执行。 perl 必须有一种方法来报告真正的问题,即报告语法错误和行号以及 FlowStep_RunTiming 中的错误。现在必须运行,失败,并通过肉眼发现语法错误。
我没有正确设置子 class 吗?有没有办法让 perl 报告真正的语法错误而不给出 运行() 未定义的 "false" 消息。任何人都有如何正确设置它的例子吗?
感谢您的帮助。
在 child 我这样做;服务是parent perl
use Service;
our @ISA= qw( Service );
sub new
{
my ($class, $args) = @_;
my $self = $class->SUPER::new($args );
bless $self, $class;
return $self;
}
sub prerun{ #something calling run
my ($self) = @_;
$self->run(); #self becomes the first argument to run
}
并且您需要将 class/object 传递给每个函数
在parent;
sub run
{
my ($self) = @_;
}
然后从child
将eval "require $module";
改为
unless (eval "require $module") {
die "Error in $module: $@";
}
或者在出现打字错误时做任何你想做的事情。
参见perldoc perlvar and perldoc -f eval。