1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/perl -w use strict; use Tk; my $counter = 0; my $test = new Test(); my $mw = tkinit(); my $button1 = $mw->Button(-textvariable => \$counter)->pack(); my $button2 = $mw->Button(-textvariable => \$test->{'counter'} )->pack(); $mw->repeat(100 => sub { fetch_data($test) }); MainLoop; sub fetch_data { my $obj = shift; $counter++; $obj->counter( $obj->counter()+1 ); } package Test; sub new { bless {counter => 0}, shift; } sub counter { $_[1] ? $_[0]->{'counter'} = $_[1] : $_[0]->{'counter'}; } 1;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/usr/bin/perl -w use strict; use Tk; my $counter = 0; my $test = new Test(); my $mw = tkinit(); print $test->counter(); my $button1 = $mw->Button(-textvariable => \$counter)->pack(); my $button2 = $mw->Button(-textvariable => $test->counter() )->pack(); $mw->repeat(100 => sub { fetch_data($test) }); MainLoop; sub fetch_data { my $obj = shift; $counter++; $obj->counter( $obj->counter()+1 ); } package Test; sub new { bless {counter => 0}, shift; } sub counter { $_[1] ? $_[0]->{'counter'} = $_[1] : \$_[0]->{'counter'}; } 1;
1 2 3 4 5 6 7 8 9 10 11
# instanz erstellen my $m = bless{counter => 99}; # gibt referenz zurück sub counter{ my $self = shift; \$self->{counter}; } # dereferenzieren print ${$m->counter};
2017-07-25T15:31:36 styx-ccHast du was zur Problemstellung beizutragen oder nicht?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#!/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;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#!/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 get_counter_ref { my ( $self, ) = @_; return \$self->{counter}; } 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->get_counter_ref() )->pack(); $mw->repeat(100 => sub { fetch_data($test) }); MainLoop; sub fetch_data { my $obj = shift; $counter++; $obj->add_to_counter( 1 ); } 1;