1

Use check for defined value after eval(), style fixes, fix typo in sub name and var name

This commit is contained in:
eleksir
2021-03-29 00:19:51 +03:00
parent a406c7c717
commit eb12b23a3d
11 changed files with 38 additions and 36 deletions

View File

@@ -13,13 +13,13 @@ my $workdir;
# before we run, change working dir # before we run, change working dir
BEGIN { BEGIN {
use Cwd qw (chdir abs_path); use Cwd qw (chdir abs_path);
my @CWD = split (/\//, abs_path ($PROGRAM_NAME)); my @CWD = split /\//, abs_path ($PROGRAM_NAME);
if ($#CWD > 1) { if ($#CWD > 1) {
$#CWD = $#CWD - 2; $#CWD = $#CWD - 2;
} }
$workdir = join ('/', @CWD); $workdir = join '/', @CWD;
chdir $workdir; chdir $workdir;
} }
@@ -87,8 +87,10 @@ if ($UID == 0) {
} }
if (defined $logfile) { if (defined $logfile) {
Log::Any::Adapter->set ('File', $logfile, 'log_level'=>'warning'); # fatal # loglevel warning suppresses logging from CHI.
Log::Any::Adapter->set ('File', $logfile, 'log_level'=>'warning');
} else { } else {
# no log defined so set loglevel to the least verbose
Log::Any::Adapter->set ('File', '/dev/null', 'log_level'=>'emergency'); Log::Any::Adapter->set ('File', '/dev/null', 'log_level'=>'emergency');
} }

View File

@@ -12,7 +12,7 @@ use JSON::PP;
use version; our $VERSION = qw (1.0); use version; our $VERSION = qw (1.0);
my $c = 'data/config.json'; my $c = 'data/config.json';
open (my $C, '<', $c) || die "No conf at $c: $OS_ERROR\n"; ## no critic (InputOutput::RequireBriefOpen) open (my $C, '<', $c) || die "No conf at $c: $OS_ERROR\n";
my $len = (stat $c) [7]; my $len = (stat $c) [7];
my $json; my $json;
my $readlen = read ($C, $json, $len); my $readlen = read ($C, $json, $len);

View File

@@ -37,7 +37,7 @@ sub Buni {
{ {
do { do {
$#a = -1; $#a = -1;
@a = $p->get_tag('meta'); ## no critic (Variables::RequireLocalizedPunctuationVars) @a = $p->get_tag ('meta'); ## no critic (Variables::RequireLocalizedPunctuationVars)
if (defined $a[0][1]->{property} && $a[0][1]->{property} eq 'og:image') { if (defined $a[0][1]->{property} && $a[0][1]->{property} eq 'og:image') {
$ret = $a[0][1]->{content}; $ret = $a[0][1]->{content};

View File

@@ -5,13 +5,13 @@ use 5.018;
use strict; use strict;
use warnings; use warnings;
use utf8; use utf8;
use open qw(:std :utf8); use open qw (:std :utf8);
use English qw( -no_match_vars ); use English qw ( -no_match_vars );
use JSON::XS; use JSON::XS;
use version; our $VERSION = qw (1.0); use version; our $VERSION = qw (1.0);
use Exporter qw(import); use Exporter qw (import);
our @EXPORT_OK = qw(LoadConf); our @EXPORT_OK = qw (LoadConf);
sub LoadConf { sub LoadConf {
my $c = 'data/config.json'; my $c = 'data/config.json';

View File

@@ -43,27 +43,27 @@ sub Drink {
if ($r->{success}) { if ($r->{success}) {
# TODO: Handle unexpected content # TODO: Handle unexpected content
my $p = HTML::TokeParser->new(\$r->{content}); my $p = HTML::TokeParser->new (\$r->{content});
my @a; my @a;
my @holyday; my @holiday;
do { do {
$#a = -1; $#a = -1;
@a = $p->get_tag('span'); ## no critic (Variables::RequireLocalizedPunctuationVars) @a = $p->get_tag ('span'); ## no critic (Variables::RequireLocalizedPunctuationVars)
if ($#{$a[0]} > 2 && defined $a[0][1]->{itemprop} && $a[0][1]->{itemprop} eq 'text') { if ($#{$a[0]} > 2 && defined $a[0][1]->{itemprop} && $a[0][1]->{itemprop} eq 'text') {
push @holyday,'* ' . decode ('UTF-8', $p->get_trimmed_text ('/span')); push @holiday,'* ' . decode ('UTF-8', $p->get_trimmed_text ('/span'));
} }
} while ($#{$a[0]} > 1); } while ($#{$a[0]} > 1);
if ($#holyday > 0) { if ($#holiday > 0) {
# cut off something weird, definely not a "holyday" # cut off something weird, definely not a "holyday"
$#holyday = $#holyday - 1; $#holiday = $#holiday - 1;
} }
if ($#holyday > 0) { if ($#holiday > 0) {
$ret = join "\n", @holyday; $ret = join "\n", @holiday;
} }
} else { } else {
cluck sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}; cluck sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason};

View File

@@ -10,10 +10,9 @@ use Carp qw (carp);
use JSON::XS; use JSON::XS;
use HTTP::Tiny; use HTTP::Tiny;
use vars qw/$VERSION/; use version; our $VERSION = qw (1.0);
use Exporter qw (import); use Exporter qw (import);
our @EXPORT_OK = qw (Fox); our @EXPORT_OK = qw (Fox);
$VERSION = '1.0';
sub Fox { sub Fox {
my $r; my $r;

View File

@@ -43,7 +43,7 @@ sub mknonce {
return $nonce; return $nonce;
} }
sub flickerSignReq { sub flickrSignReq {
my %p = @_; my %p = @_;
my $string; my $string;
my $hmac; my $hmac;
@@ -105,7 +105,7 @@ sub flickrRequestToken {
my $oauth_timestamp = time (); my $oauth_timestamp = time ();
my $oauth_nonce = mknonce (); my $oauth_nonce = mknonce ();
my $oauth_signature = flickerSignReq ( my $oauth_signature = flickrSignReq (
type => 'request_token', type => 'request_token',
oauth_timestamp => $oauth_timestamp, oauth_timestamp => $oauth_timestamp,
oauth_nonce => $oauth_nonce oauth_nonce => $oauth_nonce
@@ -142,7 +142,7 @@ sub flickrAccessToken {
my $oauth_timestamp = time (); my $oauth_timestamp = time ();
my $oauth_nonce = mknonce (); my $oauth_nonce = mknonce ();
my $oauth_signature = flickerSignReq ( my $oauth_signature = flickrSignReq (
type => 'access_token', type => 'access_token',
oauth_timestamp => $oauth_timestamp, oauth_timestamp => $oauth_timestamp,
oauth_nonce => $oauth_nonce, oauth_nonce => $oauth_nonce,
@@ -201,7 +201,7 @@ sub flickrTestLogin {
my $oauth_timestamp = time (); my $oauth_timestamp = time ();
my $oauth_nonce = mknonce (); my $oauth_nonce = mknonce ();
my $oauth_signature = flickerSignReq ( my $oauth_signature = flickrSignReq (
nojsoncallback => 1, nojsoncallback => 1,
oauth_nonce => $oauth_nonce, oauth_nonce => $oauth_nonce,
format => 'json', format => 'json',
@@ -254,7 +254,7 @@ sub flickrSearchByText {
my $oauth_timestamp = time (); my $oauth_timestamp = time ();
my $oauth_nonce = mknonce (); my $oauth_nonce = mknonce ();
my $oauth_signature = flickerSignReq ( my $oauth_signature = flickrSignReq (
nojsoncallback => 1, nojsoncallback => 1,
oauth_nonce => $oauth_nonce, oauth_nonce => $oauth_nonce,
format => 'json', format => 'json',
@@ -293,10 +293,10 @@ sub flickrSearchByText {
if (defined $response) { if (defined $response) {
# lets make another query, this time with random picture # lets make another query, this time with random picture
my $page = irand int ($response->{photos}->{total} / 500); my $page = irand (int $response->{photos}->{total} / 500);
$oauth_timestamp = time (); $oauth_timestamp = time ();
$oauth_nonce = mknonce (); $oauth_nonce = mknonce ();
$oauth_signature = flickerSignReq ( $oauth_signature = flickrSignReq (
nojsoncallback => 1, nojsoncallback => 1,
oauth_nonce => $oauth_nonce, oauth_nonce => $oauth_nonce,
format => 'json', format => 'json',
@@ -363,7 +363,7 @@ sub flickrSearchByTags {
my $oauth_timestamp = time (); my $oauth_timestamp = time ();
my $oauth_nonce = mknonce (); my $oauth_nonce = mknonce ();
my $oauth_signature = flickerSignReq ( my $oauth_signature = flickrSignReq (
nojsoncallback => 1, nojsoncallback => 1,
oauth_nonce => $oauth_nonce, oauth_nonce => $oauth_nonce,
format => 'json', format => 'json',
@@ -405,7 +405,7 @@ sub flickrSearchByTags {
my $page = irand int ($response->{photos}->{total} / 500); my $page = irand int ($response->{photos}->{total} / 500);
$oauth_timestamp = time (); $oauth_timestamp = time ();
$oauth_nonce = mknonce (); $oauth_nonce = mknonce ();
$oauth_signature = flickerSignReq ( $oauth_signature = flickrSignReq (
nojsoncallback => 1, nojsoncallback => 1,
oauth_nonce => $oauth_nonce, oauth_nonce => $oauth_nonce,
format => 'json', format => 'json',
@@ -582,7 +582,7 @@ sub FlickrByTags {
if ($result) { if ($result) {
if (defined ($result->{photos}) && defined ($result->{photos}->{photo})) { if (defined ($result->{photos}) && defined ($result->{photos}->{photo})) {
my $item = irand int(@{$result->{photos}->{photo}}); my $item = irand int @{$result->{photos}->{photo}};
$item = ${$result->{photos}->{photo}}[$item]; $item = ${$result->{photos}->{photo}}[$item];
$item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret}; $item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret};
return $item; return $item;
@@ -623,7 +623,7 @@ sub FlickrByText {
if ($result) { if ($result) {
if (defined ($result->{photos}) && defined ($result->{photos}->{photo})) { if (defined ($result->{photos}) && defined ($result->{photos}->{photo})) {
my $item = irand int(@{$result->{photos}->{photo}}); my $item = irand (int (@{$result->{photos}->{photo}}));
$item = ${$result->{photos}->{photo}}[$item]; $item = ${$result->{photos}->{photo}}[$item];
$item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret}; $item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret};
return $item; return $item;

View File

@@ -34,7 +34,7 @@ sub Kitty {
decode_json ($r->{content}) decode_json ($r->{content})
}; };
if ($EVAL_ERROR) { unless (defined $jcat) {
carp "[ERROR] Unable to decode JSON: $EVAL_ERROR"; carp "[ERROR] Unable to decode JSON: $EVAL_ERROR";
} else { } else {
if ($jcat->[0]->{url}) { if ($jcat->[0]->{url}) {

View File

@@ -37,7 +37,7 @@ sub Monkeyuser {
do { do {
$#a = -1; $#a = -1;
@a = $p->get_tag('a'); ## no critic (Variables::RequireLocalizedPunctuationVars) @a = $p->get_tag ('a'); ## no critic (Variables::RequireLocalizedPunctuationVars)
if (defined $a[0][1]->{class} && $a[0][1]->{class} eq 'lazyload small-image') { if (defined $a[0][1]->{class} && $a[0][1]->{class} eq 'lazyload small-image') {
if (defined $a[0][1]->{'data-src'} && ($a[0][1]->{'data-src'} !~ /adlitteram/)) { if (defined $a[0][1]->{'data-src'} && ($a[0][1]->{'data-src'} !~ /adlitteram/)) {
@@ -48,7 +48,7 @@ sub Monkeyuser {
} while ($#{$a[0]} > 1); } while ($#{$a[0]} > 1);
if ($#link > 0) { if ($#link > 0) {
$ret = sprintf 'https://www.monkeyuser.com%s', $link [irand (1 + $#link)]; $ret = sprintf 'https://www.monkeyuser.com%s', $link[irand (1 + $#link)];
} }
} else { } else {
cluck sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}; cluck sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason};

View File

@@ -59,7 +59,7 @@ sub Weather {
decode_json ($r->{content}); decode_json ($r->{content});
}; };
if ($EVAL_ERROR) { unless (defined $fc) {
carp "[WARN] openweathermap returns corrupted json: $EVAL_ERROR"; carp "[WARN] openweathermap returns corrupted json: $EVAL_ERROR";
return undef; return undef;
}; };

View File

@@ -9,7 +9,7 @@ use warnings;
use utf8; use utf8;
use open qw (:std :utf8); use open qw (:std :utf8);
use English qw ( -no_match_vars ); use English qw ( -no_match_vars );
use Carp qw(carp); use Carp qw (carp);
use File::Path qw (make_path); use File::Path qw (make_path);
use Hailo; use Hailo;
use Net::Jabber::Bot; use Net::Jabber::Bot;
@@ -145,7 +145,8 @@ sub RunJabberBot {
tls => 1, tls => 1,
ssl_verify => 0, ssl_verify => 0,
username => $c->{jabberbot}->{aleesa}->{authname}, username => $c->{jabberbot}->{aleesa}->{authname},
# threre is some nasty bug in module, so we have to set from_full explicitly
# threre is some nasty bug in module, so we have to set from_full explicitly
from_full => sprintf ( from_full => sprintf (
'%s@%s/%s', '%s@%s/%s',
$c->{jabberbot}->{aleesa}->{room}, $c->{jabberbot}->{aleesa}->{room},