From 5eaa3591c7d48afd7c5e6110516a71c15a021798 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sat, 18 Jan 2020 21:41:47 -0800 Subject: [PATCH] Minor style clean-up --- PBot/Channels.pm | 6 +++--- PBot/HashObject.pm | 9 +++++++-- PBot/Registerable.pm | 30 ++++++------------------------ PBot/Timer.pm | 39 +++++---------------------------------- 4 files changed, 21 insertions(+), 63 deletions(-) diff --git a/PBot/Channels.pm b/PBot/Channels.pm index d0d53b1b..c40b4640 100644 --- a/PBot/Channels.pm +++ b/PBot/Channels.pm @@ -123,9 +123,9 @@ sub is_active_op { sub get_meta { my ($self, $channel, $key) = @_; - my $lc_channel = lc $channel; - return undef if not exists $self->{channels}->{hash}->{$lc_channel}; - return $self->{channels}->{hash}->{$lc_channel}->{$key}; + $channel = lc $channel; + return undef if not exists $self->{channels}->{hash}->{$channel}; + return $self->{channels}->{hash}->{$channel}->{$key}; } sub load_channels { diff --git a/PBot/HashObject.pm b/PBot/HashObject.pm index 43388d27..f23ad6ec 100644 --- a/PBot/HashObject.pm +++ b/PBot/HashObject.pm @@ -176,8 +176,13 @@ sub unset { return "[$self->{name}] $self->{hash}->{$lc_index}->{_name}: '$key' unset."; } +sub exists { + my ($self, $index) = @_; + return exists $self->{hash}->{lc $index}; +} + sub add { - my ($self, $index, $data) = @_; + my ($self, $index, $data, $dont_save) = @_; my $lc_index = lc $index; if (exists $self->{hash}->{$lc_index}) { @@ -186,7 +191,7 @@ sub add { $data->{_name} = $index; # preserve case of index $self->{hash}->{$lc_index} = {%$data}; - $self->save; + $self->save unless $dont_save; return "$index added to $self->{name}."; } diff --git a/PBot/Registerable.pm b/PBot/Registerable.pm index f2531294..1f6d586f 100644 --- a/PBot/Registerable.pm +++ b/PBot/Registerable.pm @@ -17,10 +17,7 @@ use feature 'unicode_strings'; use Carp (); sub new { - if (ref($_[1]) eq 'HASH') { - Carp::croak("Options to Registerable should be key/value pairs, not hash reference"); - } - + Carp::croak("Options to Registerable should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; my ($class, %conf) = @_; my $self = bless {}, $class; $self->initialize(%conf); @@ -59,32 +56,17 @@ sub execute { } sub register { - my $self = shift; - my $subref; - - if (@_) { - $subref = shift; - } else { - Carp::croak("Must pass subroutine reference to register()"); - } - + my ($self, $subref) = @_; + Carp::croak("Must pass subroutine reference to register()") if not defined $subref; my $ref = { subref => $subref }; push @{ $self->{handlers} }, $ref; - return $ref; } sub unregister { - my $self = shift; - my $ref; - - if (@_) { - $ref = shift; - } else { - Carp::croak("Must pass subroutine reference to unregister()"); - } - - @{ $self->{handlers} } = grep { $_ != $ref && $_->{subref} != $ref } @{ $self->{handlers} }; + my ($self, $ref) = @_; + Carp::croak("Must pass reference to unregister()") if not defined $ref; + @{ $self->{handlers} } = grep { $_ != $ref } @{ $self->{handlers} }; } 1; diff --git a/PBot/Timer.pm b/PBot/Timer.pm index e98f5b37..51c02d75 100644 --- a/PBot/Timer.pm +++ b/PBot/Timer.pm @@ -37,39 +37,29 @@ $SIG{ALRM} = sub { }; sub new { - if (ref($_[1]) eq 'HASH') { - Carp::croak("Options to Timer should be key/value pairs, not hash reference"); - } - + Carp::croak("Options to Timer should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; my ($class, %conf) = @_; - my $timeout = delete $conf{timeout}; - $timeout = 10 unless defined $timeout; - - my $name = delete $conf{name}; - $name = "Unnamed $timeout Second Timer" unless defined $name; + my $timeout = $conf{timeout} // 10; + my $name = $conf{name} // "Unnamed $timeout Second Timer"; my $self = { handlers => [], - name => $name, timeout => $timeout, enabled => 0, }; bless $self, $class; - $min_timeout = $timeout if $timeout < $min_timeout; # alarm signal handler (poor-man's timer) $self->{timer_func} = sub { on_tick_handler($self) }; - return $self; } sub start { my $self = shift; - # print "Starting Timer $self->{name} $self->{timeout} $self->{enabled}\n"; $self->{enabled} = 1; push @timer_funcs, $self->{timer_func}; alarm $min_timeout; @@ -77,7 +67,6 @@ sub start { sub stop { my $self = shift; - # print "Stopping timer $self->{name}\n"; $self->{enabled} = 0; @timer_funcs = grep { $_ != $self->{timer_func} } @timer_funcs; } @@ -86,9 +75,6 @@ sub on_tick_handler { my $self = shift; my $elapsed = 0; - # print "-----\n"; - # print "on tick handler for $self->{name}\n"; - if ($self->{enabled}) { if ($#{ $self->{handlers} } > -1) { # call handlers supplied via register() if timeout for each has elapsed @@ -113,8 +99,6 @@ sub on_tick_handler { } else { # call default overridable handler if timeout has elapsed 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 if ($seconds - $self->{last} >= $self->{timeout}) { @@ -122,7 +106,6 @@ sub on_tick_handler { $self->{last} = $seconds; } } else { - # print "New addition for $self->{name}\n"; $elapsed = 1; $self->{last} = $seconds; } @@ -133,13 +116,11 @@ sub on_tick_handler { } } } - # print "-----\n"; } # overridable method, executed whenever timeout is triggered sub on_tick { my $self = shift; - 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; # TODO: Check if subref already exists in handlers? - $timeout = 300 if not defined $timeout; # set default value of 5 minutes if not defined $id = 'timer' if not defined $id; my $h = { subref => $ref, timeout => $timeout, id => $id }; push @{ $self->{handlers} }, $h; - # print "-- Registering timer $ref [$id] at $timeout seconds\n"; - if ($timeout < $min_timeout) { $min_timeout = $timeout; } @@ -169,15 +147,8 @@ sub register { } sub unregister { - my $self = shift; - my $id; - - if (@_) { - $id = shift; - } else { - Carp::croak("Must pass timer id to unregister()"); - } - + my ($self, $id) = @_; + Carp::croak("Must pass timer id to unregister()") if not defined $id; @{ $self->{handlers} } = grep { $_->{id} ne $id } @{ $self->{handlers} }; }