Dear monks, brother
xiper has posted a question (
329204) on using Tk's waitVariable with (MMF) tied variables. I will post the code here again.
use strict;
use Tk;
use Win32::MMF::Shareable;
print "Process starting...\n";
my $ns = tie my $wait, 'Win32::MMF::Shareable', 'wait' or die;
$wait = 0;
my $pid = fork;
if (!$pid)
{
$SIG{HUP} = \&stop;
while( 1 ) { print "tick()\n"; sleep 1 }
}
my $mw = MainWindow->new;
$mw->Button( -text => '$wait', -command => sub { pri
+nt "\$wait is $wait\n" } )->pack;
$mw->Button( -text => 'waitVariable( \\$wait )', -command => \&start )
+->pack;
$mw->Button( -text => 'local $wait++', -command => \&stop )
+->pack;
$mw->Button( -text => 'remote $wait++', -command => sub{ kill
+( 'HUP', $pid ) } )->pack;
MainLoop;
kill( 'TERM', $pid );
sub start
{
print "waiting for \$wait (was $wait)\n";
$mw->waitVariable( \$wait );
print "finished waiting for \$wait (is now $wait)\n";
}
sub stop
{
print "\$wait++ by pid $$\n";
$wait++;
}
Basically the code does a waitVariable on $wait, which should be updated by the child process upon receiving a 'HUP' signal. When I ran the code, the value of $wait was actually incremented by the child process, however the Tk's waitVariable didn't catch the updated variable, eventhough the value of the variable did change.
After hours of going through the Tk source, I couldn't figure out a sensible explanation to why this is happenning.
I suspect it's because Tk's wait function goes too low level that it 'bypassed' the perl tie() handler and watches a local copy of the variable instead?
Can someone please give an explanation to this strangeness, and a possible cure? Thanks!