Ответ 1
Это связано с анализом подпрограмм времени компиляции и времени выполнения. Как говорится в сообщении diagnostics
,
Когда вызывается внутренняя подпрограмма, она увидит значение переменная внешней подпрограммы, как это было до и во время первого вызов внешней подпрограммы; в этом случае после первого вызова внешняя подпрограмма завершена, внутренняя и внешняя подпрограммы не будут более длинная доля общего значения для переменной. Другими словами, переменная больше не будет использоваться.
Аннотирование кода:
sub outer
{
# 'my' will reallocate memory for the scalar variable $a
# every time the 'outer' function is called. That is, the address of
# '$a' will be different in the second call to 'outer' than the first call.
my $a = 123;
# the construction 'sub NAME BLOCK' defines a subroutine once,
# at compile-time.
sub inner1
{
# since this subroutine is only getting compiled once, the '$a' below
# refers to the '$a' that is allocated the first time 'outer' is called
print "inner1: ",$a, "\t", \$a, "\n";
}
# the construction sub BLOCK defines an anonymous subroutine, at run time
# '$inner2' is redefined in every call to 'outer'
my $inner2 = sub {
# this '$a' now refers to '$a' from the current call to outer
print "inner2: ", $a, "\t", \$a, "\n";
};
# At this point, $a is 123, so this call should always print 123, right?
inner1();
$inner2->();
# if this is the first call to 'outer', the definition of 'inner1' still
# holds a reference to this instance of the variable '$a', and this
# variable memory will not be freed when the subroutine ends.
$a = 456;
}
outer();
outer();
Типичный выход:
inner1: 123 SCALAR(0x80071f50)
inner2: 123 SCALAR(0x80071f50)
inner1: 456 SCALAR(0x80071f50)
inner2: 123 SCALAR(0x8002bcc8)