in reply to Re^2: Object Oriented reference in -textvariable
in thread Object Oriented reference in -textvariable

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.

Replies are listed 'Best First'.
Re^4: Object Oriented reference in -textvariable
by JOption (Novice) on Aug 21, 2017 at 13:26 UTC

    I found a solution!

    Before

    -textvariable =>$mockOb->search sub search{ my $self = shift(); if(@_) { $self->{search} = shift(); $self->{search} =~ s/^\s+|\s+$//g; } return $self->{search}; }

    After

    -textvariable =>$mockOb->searchRef sub searchRef{ my $self = shift(); if(@_) { $self->{search} = shift(); $self->{search} =~ s/^\s+|\s+$//g; } return \$self->{handelspartnerSuche}; }

    I do not understand the solution completely, but it seems to work just fine.

      Yes, that's the other approach I mentioned, returning a reference directly from the object.

      Note that the (Java) OO approach would have been to use getters/setters in all cases for writing into the object. If your case is mostly restricted to testing, then that won't make much difference.

      I assume that ->{search} and ->{handelspartnerSuche} should be the same word.