如何访问实例化对象的当前包的符号表?例如,我有这样的事情:
my $object = MyModule->new;
# this looks in the current package, to see if there's a function named run_me
# I'd like to know how to do this without passing a sub reference
$object->do_your_job;
如果在do_your_job
我使用的实现中__PACKAGE__
,它将在MyModule
包中搜索。我怎样才能让它看起来在正确的包装中?
编辑:我会尽量让这个更清楚。假设我有以下代码:
package MyMod;
sub new {
return bless {},$_[0]
}
sub do_your_job {
my $self = shift;
# of course find_package_of is fictional here
# just for this example's sake, $pkg should be main
my $pkg = find_package_of($self);
if(defined &{ $pkg . '::run_me' }) {
# the function exists, call it.
}
}
package main;
sub run_me {
print "x should run me.\n";
}
my $x = MyMod->new;
# this should find the run_me sub in the current package and invoke it.
$x->do_your_job;
现在,$x
应该以某种方式注意到这main
是当前包,并搜索它的符号表。我尝试使用Scalar::Util
's'blessed,但它仍然给了我MyModule
而不是main
。希望这现在更清楚了。