C 动态-浅绑定和深绑定
我正在研究这个问题,我得到了答案: 静态:13C 动态-浅绑定和深绑定,c,perl,compiler-construction,scope,C,Perl,Compiler Construction,Scope,我正在研究这个问题,我得到了答案: 静态:13 动态-深度绑定:2对于静态范围和具有浅绑定情况的动态范围,为什么不尝试它们呢?使用具有静态作用域的Perl: my $x = 2; my $y = 1; sub f3($) { my $z = shift; $x = $z + $x + $y; } sub f2($$) { my ($p, $z) = @_; my $x = 5; $p->($z); } sub f1($) { my $z = shift; my
动态-深度绑定:2对于静态范围和具有浅绑定情况的动态范围,为什么不尝试它们呢?使用具有静态作用域的Perl:
my $x = 2;
my $y = 1;
sub f3($) {
my $z = shift;
$x = $z + $x + $y;
}
sub f2($$) {
my ($p, $z) = @_;
my $x = 5;
$p->($z);
}
sub f1($) {
my $z = shift;
my $y = $z;
f2(\&f3, $y);
}
f1(4);
print "$x\n";
我得到了7
(即4+2+1
)。将my
s更改为local
s以获得浅绑定的动态范围,我得到了2
,正如您所预测的那样
使用深度绑定测试动态范围更为棘手,因为支持它的语言太少了。在年,我发布了Perl代码,通过传递对标量的引用的散列,“手动”实现深度绑定;使用同样的方法:
#!/usr/bin/perl -w
use warnings;
use strict;
# Create a new scalar, initialize it to the specified value,
# and return a reference to it:
sub new_scalar($)
{ return \(shift); }
# Bind the specified procedure to the specified environment:
sub bind_proc(\%$)
{
my $V = { %{+shift} };
my $f = shift;
return sub { $f->($V, @_); };
}
my $V = {};
$V->{x} = new_scalar 2;
$V->{y} = new_scalar 1;
sub f3(\%$) {
my $V = shift;
my $z = $V->{z}; # save existing z
$V->{z} = new_scalar shift; # create & initialize new z
${$V->{x}} = ${$V->{z}} + ${$V->{x}} + ${$V->{y}};
$V->{z} = $z; # restore old z
}
sub f2(\%$$) {
my $V = shift;
my $p = shift;
my $z = $V->{z}; # save existing z
$V->{z} = new_scalar shift; # create & initialize new z
my $x = $V->{x}; # save existing x
$V->{x} = new_scalar 5; # create & initialize new x
$p->(${$V->{z}});
$V->{x} = $x; # restore old x
$V->{z} = $z; # restore old z
}
sub f1(\%$) {
my $V = shift;
my $z = $V->{z}; # save existing z
$V->{z} = new_scalar shift; # create & initialize new z
my $y = $V->{y}; # save existing y
$V->{y} = new_scalar ${$V->{z}}; # create & initialize new y
f2(%$V, bind_proc(%$V, \&f3), ${$V->{y}});
$V->{y} = $y; # restore old y
$V->{z} = $z; # restore old z
}
f1(%$V, 4);
print "${$V->{x}}\n";
__END__
我得到了
10
(即4+2+4
)。对于静态范围和浅绑定情况下的动态范围,为什么不尝试一下呢?使用具有静态作用域的Perl:
my $x = 2;
my $y = 1;
sub f3($) {
my $z = shift;
$x = $z + $x + $y;
}
sub f2($$) {
my ($p, $z) = @_;
my $x = 5;
$p->($z);
}
sub f1($) {
my $z = shift;
my $y = $z;
f2(\&f3, $y);
}
f1(4);
print "$x\n";
我得到了7
(即4+2+1
)。将my
s更改为local
s以获得浅绑定的动态范围,我得到了2
,正如您所预测的那样
使用深度绑定测试动态范围更为棘手,因为支持它的语言太少了。在年,我发布了Perl代码,通过传递对标量的引用的散列,“手动”实现深度绑定;使用同样的方法:
#!/usr/bin/perl -w
use warnings;
use strict;
# Create a new scalar, initialize it to the specified value,
# and return a reference to it:
sub new_scalar($)
{ return \(shift); }
# Bind the specified procedure to the specified environment:
sub bind_proc(\%$)
{
my $V = { %{+shift} };
my $f = shift;
return sub { $f->($V, @_); };
}
my $V = {};
$V->{x} = new_scalar 2;
$V->{y} = new_scalar 1;
sub f3(\%$) {
my $V = shift;
my $z = $V->{z}; # save existing z
$V->{z} = new_scalar shift; # create & initialize new z
${$V->{x}} = ${$V->{z}} + ${$V->{x}} + ${$V->{y}};
$V->{z} = $z; # restore old z
}
sub f2(\%$$) {
my $V = shift;
my $p = shift;
my $z = $V->{z}; # save existing z
$V->{z} = new_scalar shift; # create & initialize new z
my $x = $V->{x}; # save existing x
$V->{x} = new_scalar 5; # create & initialize new x
$p->(${$V->{z}});
$V->{x} = $x; # restore old x
$V->{z} = $z; # restore old z
}
sub f1(\%$) {
my $V = shift;
my $z = $V->{z}; # save existing z
$V->{z} = new_scalar shift; # create & initialize new z
my $y = $V->{y}; # save existing y
$V->{y} = new_scalar ${$V->{z}}; # create & initialize new y
f2(%$V, bind_proc(%$V, \&f3), ${$V->{y}});
$V->{y} = $y; # restore old y
$V->{z} = $z; # restore old z
}
f1(%$V, 4);
print "${$V->{x}}\n";
__END__
我得到了
10
(即4+2+4
)。可能的重复不是重复,而是不同的问题。可能的重复不是重复,而是不同的问题。