#!/usr/bin/perl use strict; use warnings; use Tk; package Test; sub new { my $class = shift; my $self = { counter => 0, }; bless $self, ref($class)||$class; return $self; } sub add_to_counter { my ( $self, $value, ) = @_; $self->{counter} += $value if $value; return \$self->{counter}; } package main; my $counter = 0; my $test = Test->new(); my $mw = tkinit(); my $button1 = $mw->Button(-textvariable => \$counter)->pack(); my $button2 = $mw->Button(-textvariable => $test->add_to_counter() )->pack(); $mw->repeat(100 => sub { fetch_data($test) }); MainLoop; sub fetch_data { my $obj = shift; $counter++; $obj->add_to_counter( 1 ); } 1;