While developing a debugging aid, I stumbled across some untie behavior that seemed odd until pondered for a bit (leading to a "d'oh!" moment). Here is a stripped-down example. Can you predict what this script will print?

use strict; package Object; sub new { bless {}, "Object" } sub foo { "ObjectFoo\n" } package Tied; sub TIESCALAR { my $class = shift; my $o = shift; bless \$o, $class; } sub FETCH { my $self = shift; print "<fetch>\n"; $self } sub foo { "TiedFoo\n" } package main; my $o = new Object(); print $o->foo(); tie $o, 'Tied'; print $o->foo(); untie $o; eval { print $o->foo() }; print "NoFoo\n" if $@;

Replies are listed 'Best First'.
Re: The aftermath of an untie
by herveus (Prior) on Oct 02, 2001 at 07:30 UTC
    Howdy!

    Under 5.004_04, I was able to strip this down even further and get the same output from the untie.

    #!/bin/perl -w use strict; package Tied; sub TIESCALAR { my $class = shift; my $o = shift; bless \$o, $class; } sub FETCH { my $self = shift; print "<fetch>\n"; $self; } sub foo {"TiedFoo\n";} package main; my $o; tie $o, 'Tied'; print $o->foo(); untie $o; eval { print $o->foo() }; print "NoFoo\n" if $@;

    In the end, $o was still tied.

    yours,
    Herveus

      In the end, $o was still tied.

      Ah, but it isn't. $o is still associated with Tied, but the tie itself has been undone, as evidenced by the absense of a <fetch> message from the final invocation of $o->foo().

      That's the oddity I've noticed about untie -- the association with the Tied class is still there (i.e., the variable is still blessed), even though the variable is no longer tied. I have yet to find this behavior documented.