perl version: 5.8.8
Operating System: i486-linux-gnu-thread-multi
{ package printHook;
sub hook {
my $myout;
# Redirect EXAMPLE filehandle to the variable.
open EXAMPLE,'>',\$myout or die "Can't open EXAMPLE: $!\n";
# And tie the variable to this package
tie $myout, "printHook", "";
# Store to the variable directly
$myout = "direct store";
print "\$myout is: \"$myout\"\n";
# Store to the variable through the filehandle
print EXAMPLE "write to myout variable..";
# We didn't see our STORE method called?
print "\$myout is: \"$myout\"\n";
}
sub TIESCALAR {
my ($pkg) = @_;
my $obj = [];
bless $obj, $pkg;
return $obj;
}
sub STORE {
my ($obj, $val) = @_;
print STDERR "Wrote var <- $val\n";
$obj->[0] = $val;
return $val;
}
sub FETCH { return $_[0][0]; }
}
printHook->hook();
The output is:
Wrote var <- direct store $myout is: "direct store" $myout is: "write to myout variable.."Expected output:
Wrote var <- direct store $myout is: "direct store" Wrote var <- write to myout variable.. $myout is: "write to myout variable.."