in reply to Re^2: Using tie to return value of anonymous sub
in thread Using tie to return value of anonymous sub
Assuming player.react_before.poisoned (to use a neutral syntax since syntax is the topic) sounds like a way of setting (and getting?) an event handler, how about something like the following:
sub name { my ($self, $name) = @_; $self->{name} = $name if @_ >= 2; return $self->{name}; } sub react_before { my ($self, $event, $handler) = @_; if (@_ >= 2) { $self->{event_handlers}{$event}{before} = $handler; } return $self->{event_handlers}{$event}{before}; } sub poisoned { my ($self, $poisoned) = @_; if (@_ >= 2) { if ($poisoned != $self->{poisoned}) { my $handler= $self->{event_handlers}{poisoned}{before}; $poisoned = $handler->($self, 'poisoned', $val) if $handler; } if ($val != $self->{poisoned}) { $self->{poisoned} = $poisoned; my $handler = $self->{event_handlers}{poisoned}{after}; $handler->($self, 'poisoned', $poisoned) if $handler; } } return $self->{poisoned}; } $char->react_before(poisoned => \&handler); $char->poisoned(1); print( $char->name, ' is ', $char->poisoned ? 'posioned' : 'not poisoned', ".\n" );
|
|---|