我正在尝试编写一个模块,该模块应该能够根据变量的内容调用特定的方法。我决定使用哈希将这些名称映射到函数引用。我已经编写了一些概念验证代码,看看这是否有效,并且我在此过程中遇到了一些问题。
我遇到参考函数调用无法访问我的$ self-> ...数据成员的问题。
下面是显示我遇到的问题的实际复制 - 粘贴概念验证示例。
#!/usr/bin/env perl
use strict;
use warnings;
package My::Test;
sub new {
my $class = shift;
my $self = {};
$self->{_currentType} = undef;
$self->{_currentData} = undef;
$self->{_mapping} = undef;
return bless $self, $class;
}
# Setup the mapping
sub createMapping {
my $self = shift;
my %mapping = ( 'mapping.1' => \&My::Test::handler1,
'mapping.2' => \&My::Test::handler2,
'mapping.3' => \&My::Test::handler3, );
$self->{_mapping} = \%mapping;
}
# This is the main entrypoint for actual processing where the appropriate handler function is invoked.
sub startMeUp {
my ($self, $mapType, $someDataToHandle) = @_;
$self->{_currentType} = $mapType;
$self->{_currentData} = $someDataToHandle;
# Now call this function
$self->{_mapping}->{$mapType}->();
}
# These functions below gets called, but the values of $self are not retained. Why?
sub handler1 {
my $self = shift;
print "I am a handler for $self->{_currentType}!\n";
}
sub handler2 {
my $self = shift;
print "I am a handler for $self->{_currentType}!\n";
}
sub handler3 {
my $self = shift;
print "I am a handler for $self->{_currentType}!\n";
}
sub manualFunction {
my $self = shift;
print "I am a manual call, I am $self->{_currentType}!\n";
}
1;
然后通过简单的调用程序脚本调用上面的模块,如下所示:
#!/usr/bin/env perl
use strict;
use warnings;
use My::Test;
my $test = My::Test->new();
$test->createMapping();
# We want to try to execute the handler2 function (mapping.2 is mapped to handler2 via createMapping).
# The function call works but when done this way, I cannot use
# $self->{someVariableName} to access module members as I'd like.
$test->startMeUp('mapping.2');
# The follow will work.
$test->manualFunction();
运行perl脚本会产生以下结果:
% perl test.pl
Use of uninitialized value in concatenation (.) or string at My/Test.pm line 48.
I am a handler for !
I am a manual call, I am mapping.2!
我在找到解决方案时遇到问题,为什么通过哈希表调用引用的 startMeUp 函数不保留 $ self-> {某些类成员变量中包含的值这里}
我试图搜索此问题,但未能找到有关此特定问题的问题/答案。
我不确定在这种情况下,我想做的事情是不可能的,或者我是否只是做错了,所以我希望你们中的一些人能指出我正确的方向。
(当然,仅仅为三个元素实现这样的解决方案是过分的,但在实际实现中会有更多,并且我宁愿避免编写一个很好的if-elsif语句,它也是一个很好的运动)。
提前谢谢!
祝你好运, 克劳斯
答案 0 :(得分:2)
如果您从
更改映射设置sub createMapping {
my $self = shift;
my %mapping = ( 'mapping.1' => \&My::Test::handler1,
'mapping.2' => \&My::Test::handler2,
'mapping.3' => \&My::Test::handler3, );
$self->{_mapping} = \%mapping;
}
到
sub createMapping {
my $self = shift;
my %mapping = ( 'mapping.1' => sub { $self->handler1 } ,
'mapping.2' => sub { $self->handler2 } ,
'mapping.3' => sub { $self->handler3 } );
$self->{_mapping} = \%mapping;
}
您的测试脚本将有效。
&My::Test::handler1
会将handler1
称为简单函数,而不是对象方法。