在开发使用AUTOLOAD或其他子例程调度技术的Perl模块时,我已经多次运行以下模式:
sub AUTOLOAD {
my $self = $_[0];
my $code = $self->figure_out_code_ref( $AUTOLOAD );
goto &$code;
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,并caller看到正确的范围.
现在我想做的是在执行期间本地设置$_相等.这将是这样的:$self&$code
sub AUTOLOAD {
my $self = $_[0];
my $code = $self->figure_out_code_ref( $AUTOLOAD );
local *_ = \$self;
# and now the question is how to call &$code
# goto &$code; # wont work since local scope changes will
# be unrolled before the goto
# &$code; # will preserve the local, but caller …Run Code Online (Sandbox Code Playgroud) 全部,我想做的事:Proc A调用Proc B,使用B的uplevel命令我试图在proc A范围中设置变量.值具有空格时发生错误.
proc B { } {
set string1 "Test"
set string2 "Test with space"
uplevel 1 set key1 $string1
uplevel 1 set key2 $string2
return 0
}
proc A { } {
set res [B]
puts "key1 is $key1"
puts "key2 is $key2"
}
Run Code Online (Sandbox Code Playgroud)
如果我注释掉key2,它工作正常.添加key2时,它会失败,并显示以下错误.
wrong # args: should be "set varName ?newValue?"
while executing
"set key2 Test with space"
Run Code Online (Sandbox Code Playgroud)
有关如何克服此错误的任何建议.感谢您的帮助.