3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-29 23:39:24 +01:00

Minor style clean-up

This commit is contained in:
Pragmatic Software 2020-01-18 21:41:47 -08:00
parent 8a38dbdcc2
commit 5eaa3591c7
4 changed files with 21 additions and 63 deletions

View File

@ -123,9 +123,9 @@ sub is_active_op {
sub get_meta { sub get_meta {
my ($self, $channel, $key) = @_; my ($self, $channel, $key) = @_;
my $lc_channel = lc $channel; $channel = lc $channel;
return undef if not exists $self->{channels}->{hash}->{$lc_channel}; return undef if not exists $self->{channels}->{hash}->{$channel};
return $self->{channels}->{hash}->{$lc_channel}->{$key}; return $self->{channels}->{hash}->{$channel}->{$key};
} }
sub load_channels { sub load_channels {

View File

@ -176,8 +176,13 @@ sub unset {
return "[$self->{name}] $self->{hash}->{$lc_index}->{_name}: '$key' unset."; return "[$self->{name}] $self->{hash}->{$lc_index}->{_name}: '$key' unset.";
} }
sub exists {
my ($self, $index) = @_;
return exists $self->{hash}->{lc $index};
}
sub add { sub add {
my ($self, $index, $data) = @_; my ($self, $index, $data, $dont_save) = @_;
my $lc_index = lc $index; my $lc_index = lc $index;
if (exists $self->{hash}->{$lc_index}) { if (exists $self->{hash}->{$lc_index}) {
@ -186,7 +191,7 @@ sub add {
$data->{_name} = $index; # preserve case of index $data->{_name} = $index; # preserve case of index
$self->{hash}->{$lc_index} = {%$data}; $self->{hash}->{$lc_index} = {%$data};
$self->save; $self->save unless $dont_save;
return "$index added to $self->{name}."; return "$index added to $self->{name}.";
} }

View File

@ -17,10 +17,7 @@ use feature 'unicode_strings';
use Carp (); use Carp ();
sub new { sub new {
if (ref($_[1]) eq 'HASH') { Carp::croak("Options to Registerable should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH';
Carp::croak("Options to Registerable should be key/value pairs, not hash reference");
}
my ($class, %conf) = @_; my ($class, %conf) = @_;
my $self = bless {}, $class; my $self = bless {}, $class;
$self->initialize(%conf); $self->initialize(%conf);
@ -59,32 +56,17 @@ sub execute {
} }
sub register { sub register {
my $self = shift; my ($self, $subref) = @_;
my $subref; Carp::croak("Must pass subroutine reference to register()") if not defined $subref;
if (@_) {
$subref = shift;
} else {
Carp::croak("Must pass subroutine reference to register()");
}
my $ref = { subref => $subref }; my $ref = { subref => $subref };
push @{ $self->{handlers} }, $ref; push @{ $self->{handlers} }, $ref;
return $ref; return $ref;
} }
sub unregister { sub unregister {
my $self = shift; my ($self, $ref) = @_;
my $ref; Carp::croak("Must pass reference to unregister()") if not defined $ref;
@{ $self->{handlers} } = grep { $_ != $ref } @{ $self->{handlers} };
if (@_) {
$ref = shift;
} else {
Carp::croak("Must pass subroutine reference to unregister()");
}
@{ $self->{handlers} } = grep { $_ != $ref && $_->{subref} != $ref } @{ $self->{handlers} };
} }
1; 1;

View File

@ -37,39 +37,29 @@ $SIG{ALRM} = sub {
}; };
sub new { sub new {
if (ref($_[1]) eq 'HASH') { Carp::croak("Options to Timer should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH';
Carp::croak("Options to Timer should be key/value pairs, not hash reference");
}
my ($class, %conf) = @_; my ($class, %conf) = @_;
my $timeout = delete $conf{timeout}; my $timeout = $conf{timeout} // 10;
$timeout = 10 unless defined $timeout; my $name = $conf{name} // "Unnamed $timeout Second Timer";
my $name = delete $conf{name};
$name = "Unnamed $timeout Second Timer" unless defined $name;
my $self = { my $self = {
handlers => [], handlers => [],
name => $name, name => $name,
timeout => $timeout, timeout => $timeout,
enabled => 0, enabled => 0,
}; };
bless $self, $class; bless $self, $class;
$min_timeout = $timeout if $timeout < $min_timeout; $min_timeout = $timeout if $timeout < $min_timeout;
# alarm signal handler (poor-man's timer) # alarm signal handler (poor-man's timer)
$self->{timer_func} = sub { on_tick_handler($self) }; $self->{timer_func} = sub { on_tick_handler($self) };
return $self; return $self;
} }
sub start { sub start {
my $self = shift; my $self = shift;
# print "Starting Timer $self->{name} $self->{timeout} $self->{enabled}\n";
$self->{enabled} = 1; $self->{enabled} = 1;
push @timer_funcs, $self->{timer_func}; push @timer_funcs, $self->{timer_func};
alarm $min_timeout; alarm $min_timeout;
@ -77,7 +67,6 @@ sub start {
sub stop { sub stop {
my $self = shift; my $self = shift;
# print "Stopping timer $self->{name}\n";
$self->{enabled} = 0; $self->{enabled} = 0;
@timer_funcs = grep { $_ != $self->{timer_func} } @timer_funcs; @timer_funcs = grep { $_ != $self->{timer_func} } @timer_funcs;
} }
@ -86,9 +75,6 @@ sub on_tick_handler {
my $self = shift; my $self = shift;
my $elapsed = 0; my $elapsed = 0;
# print "-----\n";
# print "on tick handler for $self->{name}\n";
if ($self->{enabled}) { if ($self->{enabled}) {
if ($#{ $self->{handlers} } > -1) { if ($#{ $self->{handlers} } > -1) {
# call handlers supplied via register() if timeout for each has elapsed # call handlers supplied via register() if timeout for each has elapsed
@ -113,8 +99,6 @@ sub on_tick_handler {
} else { } else {
# call default overridable handler if timeout has elapsed # call default overridable handler if timeout has elapsed
if (defined $self->{last}) { if (defined $self->{last}) {
# print "$self->{name} last = $self->{last}, seconds: $seconds, timeout: $self->{timeout} " . ($seconds - $self->{last}) . "\n";
$self->{last} -= $max_seconds if $seconds < $self->{last}; # handle wrap-around $self->{last} -= $max_seconds if $seconds < $self->{last}; # handle wrap-around
if ($seconds - $self->{last} >= $self->{timeout}) { if ($seconds - $self->{last} >= $self->{timeout}) {
@ -122,7 +106,6 @@ sub on_tick_handler {
$self->{last} = $seconds; $self->{last} = $seconds;
} }
} else { } else {
# print "New addition for $self->{name}\n";
$elapsed = 1; $elapsed = 1;
$self->{last} = $seconds; $self->{last} = $seconds;
} }
@ -133,13 +116,11 @@ sub on_tick_handler {
} }
} }
} }
# print "-----\n";
} }
# overridable method, executed whenever timeout is triggered # overridable method, executed whenever timeout is triggered
sub on_tick { sub on_tick {
my $self = shift; my $self = shift;
print "Tick! $self->{name} $self->{timeout} $self->{last} $seconds\n"; print "Tick! $self->{name} $self->{timeout} $self->{last} $seconds\n";
} }
@ -150,15 +131,12 @@ sub register {
Carp::croak("Must pass subroutine reference to register()") if not defined $ref; Carp::croak("Must pass subroutine reference to register()") if not defined $ref;
# TODO: Check if subref already exists in handlers? # TODO: Check if subref already exists in handlers?
$timeout = 300 if not defined $timeout; # set default value of 5 minutes if not defined $timeout = 300 if not defined $timeout; # set default value of 5 minutes if not defined
$id = 'timer' if not defined $id; $id = 'timer' if not defined $id;
my $h = { subref => $ref, timeout => $timeout, id => $id }; my $h = { subref => $ref, timeout => $timeout, id => $id };
push @{ $self->{handlers} }, $h; push @{ $self->{handlers} }, $h;
# print "-- Registering timer $ref [$id] at $timeout seconds\n";
if ($timeout < $min_timeout) { if ($timeout < $min_timeout) {
$min_timeout = $timeout; $min_timeout = $timeout;
} }
@ -169,15 +147,8 @@ sub register {
} }
sub unregister { sub unregister {
my $self = shift; my ($self, $id) = @_;
my $id; Carp::croak("Must pass timer id to unregister()") if not defined $id;
if (@_) {
$id = shift;
} else {
Carp::croak("Must pass timer id to unregister()");
}
@{ $self->{handlers} } = grep { $_->{id} ne $id } @{ $self->{handlers} }; @{ $self->{handlers} } = grep { $_->{id} ne $id } @{ $self->{handlers} };
} }