Perl Tk error "Invalid value for shared scalar"

478 views Asked by At

I got problem with scalars in my program.

I got code like this:

use threads;
use threads::shared;
use Tk;
$mw = new MainWindow;
my $label = undef;
share($label) my $ok = undef;
share($ok)

HERE IS BUTTON WITH OPTION -command => \&sub1

threads->create('sub2');

sub sub1 {
    $top   = $mw->TopLevel();
    $label = $top->Label( -text => 'something' )->pack();
    $ok    = 1;
}

sub sub2 {
    while (1) {
        if ($ok) {
            $label->configure( -text => 'i need this' );
            $label->update;
        }
    }
}

I got error in $label->configure(-text => 'i need this'); like this :

Invalid value for shared scalar at xxx.pl

I need to update my label text only from threads and i can't do this.

Thanks for advices.

1

There are 1 answers

0
pilcrow On

That error suggests that Tk Label objects simply weren't written to support being shared under ithreads, a circumstance which I'd guess is very arduous to remedy.

I'd suggest instead you make a thread responsible for updating the UI widgets and have that thread receive update instructions from other threads. Awkward, but workable.