Why is the variable still bound

Duration:

$t =  3;
{
    tie $t, 'Yep';
} # Expect $t to become untied here.
print $t;

package Yep;

sub TIESCALAR {
   bless {}, 'Yep';
}

sub UNTIE {
   print "UNTIE\n";
}

sub DESTROY {
   print "DESTROY\n";
}

Output:

Can't locate object method "FETCH" via package "Yep" at a.pl line 5.
DESTROY

Output EXPECTED:

DESTROY
3

I want the tievariable $ t only for the duration of the area it is in tie. Out of scope, he should behave the same as before a tie. Therefore, I transfer tieto the block and expect it to untiebe called when the end of the block is reached (for example, "local", where the value is restored at the end of the block, but for the bound variable, I expect the behavior to be restored ( untie $t)). Please note that $tis not beyond the scope.

+4
source share
4 answers

Regarding a completely new question,

, ,

my $t = 3;

{
   $t = 4;
}

print "$t\n";  # 4, not 3.

, -. untie , , .

my $t = 3;

{
   tie my $t, 'Yep';
} # Tied variable destroyed here.

print "$t\n";  # 3.
+1

: UNTIE , ?

UNTIE , UNTIE. DESTROY , DESTROY.

,

  • sub UNTIE   { &_destructor; } # Perl 4 style, which passes the current
    sub DESTROY { &_destructor; } # @_ to the called procedure.
    
  • goto

    sub UNTIE   { goto &_destructor; } # does not return to this sub
    sub DESTROY { goto &_destructor; } # does not return to this sub
    
  • :

    *UNTIE = *DESTROY{CODE};
    
+4

UNTIE , ?

, UNTIE , , , , UNTIE , DESTROY. , . , UNTIE, UNTIE.

, UNTIE , .

sub UNTIE   { shift->_destructor(@_) }
sub DESTROY { shift->_destructor(@_) }
+4

. local.

my $t = 3;

{
   tie local $t, 'Yep';
} # Tied variable destroyed here.

print "$t\n";  # 3.
0

Source: https://habr.com/ru/post/1610269/


All Articles