in reply to (tye)Re: Local tied FILEHANDLE
in thread Local tied FILEHANDLE

Well then, were it is:

Script:

use strict;

my $bar = tie(*STDOUT, 'Bar');

print "Before\n";
test();
print "After\n";

untie(*STDOUT);

sub test {
        local(*STDOUT);

        my $foo = tie(*STDOUT, 'Foo');
        print "In medias res\n";
        untie(*STDOUT);
}

package Foo;

sub TIEHANDLE {
        my ($class) = @_;
        return bless({},$class);
}

sub PRINT {
        my ($self) = shift;
        print STDERR ("Foo::PRINT: ",@_);
}

package Bar;

sub TIEHANDLE {
        my ($class) = @_;
        return bless({},$class);
}

sub PRINT {
        my ($self) = shift;
        print STDERR ("Bar::PRINT: ",@_);
}
Output:
Bar::PRINT: Before
Foo::PRINT: In medias res
After
Expected output:
Bar::PRINT: Before
Foo::PRINT: In medias res
Bar::After

It seems like STDOUT is retied to Foo and then untied. local(*STDOUT) has no effect here. Why?

Replies are listed 'Best First'.
I think this is a tie bug
by tilly (Archbishop) on Jan 06, 2001 at 20:23 UTC
    If it isn't could someone explain the following short program to me? My expectation is that after calling local the original handle should be untouched by anything that you do. This emphatically is not happening:
    test(1); test(2); sub test { my $iter = shift; my $fh = \*STDOUT; print $fh "Before local $iter\n"; local *STDOUT; print $fh "Before tying $iter\n"; tie (*STDOUT, 'Foo', $iter); print $fh "After tying $iter\n"; } package Foo; sub TIEHANDLE { my ($class) = shift; return bless([shift],$class); } sub PRINT { my ($self) = shift; print STDERR ("Foo::PRINT (tie $self->[0]): ",@_); }
Re: Re: (tye)Re: Local tied FILEHANDLE
by tilly (Archbishop) on Jan 05, 2001 at 20:19 UTC
    Please use code tags. They are not just for formatting, they also allow people to easily download your code and run it.

    Given an IE bug on the machine I am on right now, this is inconvenient for me, so I am unable to test my guesses as to what your problem might be...