标签: uplevel

如何在Perl的上部作用域中本地化变量?

在开发使用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)

perl goto local uplevel

6
推荐指数
1
解决办法
440
查看次数

TCL命名空间和堆栈帧之间有什么区别?

Upvar创建指向不同堆栈帧中的变量的链接,有时称为调用堆栈不同的范围.

Upvar还用于为全局(或命名空间)变量2创建别名.但是命名空间只能由namespace eval命令创建.proc命令创建一个新的堆栈帧.

命名空间和调用堆栈似乎是TCL命名上下文可以更改的两种方式.Upvar和Uplevel可以在命名空间和调用堆栈上工作.

我做对了吗?我还没有看到调用堆栈和命名空间之间的直接比较,因此我的问题.

callstack namespaces tcl uplevel upvar

2
推荐指数
1
解决办法
446
查看次数

当值有多个单词时,tcl Uplevel set命令失败

全部,我想做的事: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)

有关如何克服此错误的任何建议.感谢您的帮助.

tcl uplevel

0
推荐指数
1
解决办法
158
查看次数

标签 统计

uplevel ×3

tcl ×2

callstack ×1

goto ×1

local ×1

namespaces ×1

perl ×1

upvar ×1