JOption has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! I am quite new to Perl (started this month) and encountered a rather strange behavior in Perl.
my $mockOb = SomeClass->new; my $search = $contentFrame->new_ttk__entry(-takefocus => 1, -font => ' +Arial 11', -width => 40, -validate => 'focusout', -textvariable =>\$ +mockOb->search, -validatecommand => [\&searchValidation, Tkx::Ev('%P +')]); sub searchValidation{ ....do crazy stuff } SomeClass sub new { my $class = shift; my $self = { search = "" }; bless $self, $class; return $self; } sub search{ my $self = shift(); if(@_) { $self->{search} = shift(); } return $self->{search}; }
My problem is that whenever $mockOB->search is changed, is is not visible on the screen. It seems that the -textvariabe is not working like it should.

Tank you for your Help!

Replies are listed 'Best First'.
Re: Object Oriented reference in -textvariable
by Corion (Patriarch) on Aug 21, 2017 at 12:43 UTC

    Are you certain that $mockOb->search returns a variable and not a value? Usually, Tk objects require a real variable and not a value to watch. A value never changes.

    Depending on whether you have control over $mockOb, either have ->search return the reference to the variable to watch directly, or alternatively, pass the reference to the variable within $mockOb yourself. Having two things that access internals of $mockOb (Tk and $mockOb itself) certainly is bad design.

      I have complete control over $mockOb. It is a custom class created by me.

      I was trying to create a Form and a custom object to hold the values.

      I am more used to Java an the Object-Oriented design. Maybe my design is flawed.

        It depends on who actually is supposed to change the text. If only the user should change the text, instead of referencing the variable directly I would use the events on the<FocusOut> event to update the value in $mockOb manually:

        my $mockOb = SomeClass->new; my $search = $contentFrame->new_ttk__entry(-takefocus => 1, -font => ' +Arial 11', -width => 40, -validate => 'focusout', -textvariable =>\$ +mockOb->search, -validatecommand => [\&searchValidation, Tkx::Ev('%P +')] ); # Write information back to our $mockOb $search->bind('<FocusOut>', sub { $mockOb->search( $search->value )}); sub searchValidation{ ....do crazy stuff } SomeClass sub new { my $class = shift; my $self = { search = "" }; bless $self, $class; return $self; } sub search{ my $self = shift(); if(@_) { $self->{search} = shift(); } return $self->{search}; }

        But I haven't done Tk for a long time, so I don't know how applicable my idea of handling the focus event is, and if it is actually done the way that I wrote above untested code.