1

More heavy perlcritic policies, more careful work with strings/substr

This commit is contained in:
eleksir
2022-04-03 13:45:02 +03:00
parent 5e53450221
commit f18544a798
22 changed files with 271 additions and 227 deletions

View File

@@ -6,7 +6,7 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Encode;
use Encode qw (decode);
use HTML::TokeParser;
use HTTP::Tiny;
use JSON::XS;

View File

@@ -19,7 +19,9 @@ sub LoadConf {
open my $CH, '<', $c or die "[FATA] No conf at $c: $OS_ERROR\n";
my $len = (stat $c) [7];
my $json;
use bytes;
my $readlen = read $CH, $json, $len;
no bytes;
unless ($readlen) {
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls

View File

@@ -6,7 +6,7 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Encode;
use Encode qw (decode);
use CHI;
use CHI::Driver::BerkeleyDB;
use DateTime;
@@ -76,7 +76,7 @@ sub Drink {
my $cache = CHI->new (
driver => 'BerkeleyDB',
root_dir => $c->{cachedir},
namespace => __PACKAGE__
namespace => __PACKAGE__,
);
# Those POSIX assholes just forgot to add unix timestamps without TZ offset, so...
@@ -96,7 +96,7 @@ sub Drink {
day => $mday,
hour => 0,
minute => 0,
second => 0
second => 0,
)->add (days => 1)->strftime ('%s');
if ((substr $offset, 0, 1) eq '+') {
@@ -109,7 +109,7 @@ sub Drink {
my $res = $cache->compute (
'Holiday',
{ expires_at => $expirationDate },
$drinkCallBack
$drinkCallBack,
);
if (defined $res && $res ne '') {

View File

@@ -11,7 +11,6 @@ use Carp qw (croak);
use File::Path qw (make_path);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use MIME::Base64;
use SQLite_File;
use BotLib::Conf qw (LoadConf);
@@ -30,7 +29,7 @@ sub Seed () {
my $backingfile = sprintf '%s/fortune.sqlite', $dir;
if (-f $backingfile) {
if (-e $backingfile) {
unlink $backingfile || croak "Unable to remove $backingfile: $OS_ERROR\n";
}
@@ -40,7 +39,7 @@ sub Seed () {
while (my $fortunefile = readdir $srcdirhandle) {
my $srcfile = sprintf '%s/%s', $srcdir, $fortunefile;
unless (-f $srcfile) {
unless (-e $srcfile) {
next;
}
@@ -53,7 +52,7 @@ sub Seed () {
local $INPUT_RECORD_SEPARATOR = "\n%\n";
while (readline $fh) {
my $phrase = substr $_, 0, -3;
my $phrase = substr $_, 0, length ("\n%\n");
push @fortune, $phrase;
}

View File

@@ -6,7 +6,7 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use JSON::XS;
use JSON::XS qw (decode_json);
use HTTP::Tiny;
use Log::Any qw ($log);

View File

@@ -33,7 +33,7 @@ sub Seed () {
for (0..$#dow) {
my $backingfile = sprintf '%s/%s.sqlite', $dir, $dow[$_];
if (-f $backingfile) {
if (-e $backingfile) {
unlink $backingfile || croak "Unable to remove $backingfile: $OS_ERROR\n";
}

View File

@@ -6,21 +6,19 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (cluck);
use Digest::HMAC_SHA1 qw (hmac_sha1);
use Digest::HMAC_SHA1 ();
use File::Path qw (make_path);
use JSON::XS;
use HTTP::Tiny;
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use MIME::Base64;
use MIME::Base64 qw (encode_base64);
use SQLite_File;
use URI::Encode::XS qw (uri_encode);
use BotLib::Conf qw (LoadConf);
use BotLib::Util qw (urlencode);
use Data::Dumper;
use Data::Dumper qw (Dumper);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -109,7 +107,7 @@ sub flickrRequestToken {
my $oauth_signature = flickrSignReq (
type => 'request_token',
oauth_timestamp => $oauth_timestamp,
oauth_nonce => $oauth_nonce
oauth_nonce => $oauth_nonce,
);
my $url = $flickr_request_token_url . sprintf (
@@ -118,7 +116,7 @@ sub flickrRequestToken {
$oauth_timestamp,
$flickr_consumer_key,
uri_encode ($oauth_signature),
uri_encode ($flickr_callback_url)
uri_encode ($flickr_callback_url),
);
my $http = HTTP::Tiny->new (timeout => 5);
@@ -149,7 +147,7 @@ sub flickrAccessToken {
oauth_nonce => $oauth_nonce,
oauth_token => $flickr_request_token,
oauth_token_secret => $flickr_request_token_secret,
oauth_verifier => $flickr_verifier
oauth_verifier => $flickr_verifier,
);
my $url = $flickr_access_token_url . sprintf (
@@ -159,7 +157,7 @@ sub flickrAccessToken {
$flickr_verifier,
$flickr_consumer_key,
$flickr_request_token,
uri_encode ($oauth_signature)
uri_encode ($oauth_signature),
);
my $http = HTTP::Tiny->new (timeout => 5);
@@ -213,7 +211,7 @@ sub flickrTestLogin {
oauth_token => $flickr_access_token,
oauth_token_secret => $flickr_access_token_secret,
oauth_signature_method => 'HMAC-SHA1',
oauth_version => '1.0'
oauth_version => '1.0',
);
my $url = $flickr_api_url . sprintf (
@@ -222,7 +220,7 @@ sub flickrTestLogin {
$flickr_consumer_key,
$oauth_timestamp,
$flickr_access_token,
uri_encode ($oauth_signature)
uri_encode ($oauth_signature),
);
my $http = HTTP::Tiny->new (timeout => 5);
@@ -270,7 +268,7 @@ sub flickrSearchByText {
oauth_token => $flickr_access_token,
oauth_token_secret => $flickr_access_token_secret,
oauth_signature_method => 'HMAC-SHA1',
oauth_version => '1.0'
oauth_version => '1.0',
);
my $url = $flickr_api_url . sprintf (
@@ -280,7 +278,7 @@ sub flickrSearchByText {
$oauth_timestamp,
$flickr_access_token,
uri_encode ($oauth_signature),
uri_encode ($text)
uri_encode ($text),
);
my $http = HTTP::Tiny->new (timeout => 5);
@@ -313,7 +311,7 @@ sub flickrSearchByText {
oauth_token => $flickr_access_token,
oauth_token_secret => $flickr_access_token_secret,
oauth_signature_method => 'HMAC-SHA1',
oauth_version => '1.0'
oauth_version => '1.0',
);
$url = $flickr_api_url . sprintf (
@@ -324,7 +322,7 @@ sub flickrSearchByText {
$flickr_access_token,
uri_encode ($oauth_signature),
$page,
uri_encode ($text)
uri_encode ($text),
);
$http = HTTP::Tiny->new (timeout => 5);
@@ -379,7 +377,7 @@ sub flickrSearchByTags {
oauth_token => $flickr_access_token,
oauth_token_secret => $flickr_access_token_secret,
oauth_signature_method => 'HMAC-SHA1',
oauth_version => '1.0'
oauth_version => '1.0',
);
my $url = $flickr_api_url . sprintf (
@@ -389,7 +387,7 @@ sub flickrSearchByTags {
$oauth_timestamp,
$flickr_access_token,
uri_encode ($oauth_signature),
uri_encode ($tags)
uri_encode ($tags),
);
my $http = HTTP::Tiny->new (timeout => 5);
@@ -422,7 +420,7 @@ sub flickrSearchByTags {
oauth_token => $flickr_access_token,
oauth_token_secret => $flickr_access_token_secret,
oauth_signature_method => 'HMAC-SHA1',
oauth_version => '1.0'
oauth_version => '1.0',
);
$url = $flickr_api_url . sprintf (
@@ -433,7 +431,7 @@ sub flickrSearchByTags {
$flickr_access_token,
uri_encode ($oauth_signature),
$page,
uri_encode ($tags)
uri_encode ($tags),
);
$http = HTTP::Tiny->new (timeout => 5);
@@ -501,7 +499,7 @@ sub FlickrInit {
'Access token (%s) and access token secret (%s) are in %s',
$accessToken{oauth_token},
$accessToken{oauth_token_secret},
$backingfile
$backingfile,
);
untie %secret;

View File

@@ -6,7 +6,7 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use JSON::XS;
use JSON::XS qw (decode_json);
use HTTP::Tiny;
use Log::Any qw ($log);

View File

@@ -35,7 +35,7 @@ sub Train () {
my $lat = Hailo->new (
brain => $brain,
# in our case mean amount of words in phase is 3
order => 2
order => 2,
);
$lat->train ($srcfile);
@@ -54,7 +54,7 @@ sub Lat () {
return '';
}
unless (-f $brain) {
unless (-e $brain) {
$log->error ("[ERROR] No lat module data: $brain is absent! Train lat first.");
return '';
}
@@ -63,7 +63,7 @@ sub Lat () {
brain => $brain,
# in our case mean amount of words in phase is 3
order => 2,
save_on_exit => 0
save_on_exit => 0,
);
return $lat->reply ();

View File

@@ -7,7 +7,7 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (cluck croak);
use Carp qw (croak);
use File::Path qw (make_path);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
@@ -30,7 +30,7 @@ sub Seed () {
my $backingfile = sprintf '%s/proverb.sqlite', $dir;
if (-f $backingfile) {
if (-e $backingfile) {
unlink $backingfile || croak "Unable to remove $backingfile: $OS_ERROR";
}
@@ -40,7 +40,7 @@ sub Seed () {
while (my $proverbfile = readdir ($srcdirhandle)) {
my $srcfile = sprintf '%s/%s', $srcdir, $proverbfile;
unless (-f $srcfile) {
unless (-e $srcfile) {
next;
}

View File

@@ -8,10 +8,10 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (cluck);
use Digest::SHA qw (sha1_base64);
use Encode;
use MIME::Base64;
use Encode qw (encode_utf8);
use MIME::Base64 qw (encode_base64);
use Text::Fuzzy qw (distance_edits);
use URI::URL;
use URI::URL qw (url);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -29,13 +29,8 @@ sub trim {
return $str;
}
while (substr ($str, 0, 1) =~ /^\s$/xms) {
$str = substr $str, 1;
}
while (substr ($str, -1, 1) =~ /^\s$/xms) {
chop $str;
}
$str =~ s/^\s+//u;
$str =~ s/\s+$//u;
return $str;
}

View File

@@ -6,11 +6,10 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp cluck);
use CHI;
use CHI::Driver::BerkeleyDB;
use HTTP::Tiny;
use JSON::XS;
use JSON::XS qw (decode_json);
use Log::Any qw ($log);
use BotLib::Conf qw (LoadConf);
use BotLib::Util qw (trim urlencode);
@@ -63,7 +62,7 @@ sub Weather {
unless (defined $fc) {
$log->warn ("[WARN] openweathermap returns corrupted json: $EVAL_ERROR");
return undef;
};
}
} else {
$log->warn (sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason});
return undef;
@@ -127,13 +126,13 @@ sub Weather {
my $cache = CHI->new (
driver => 'BerkeleyDB',
root_dir => $c->{cachedir},
namespace => __PACKAGE__
namespace => __PACKAGE__,
);
my $w = $cache->compute (
$city,
'3 hours',
$weatherCallBack
$weatherCallBack,
);
my $reply;
@@ -149,7 +148,7 @@ sub Weather {
$w->{temperature_min},
$w->{temperature_feelslike},
$w->{humidity},
$w->{pressure}
$w->{pressure},
);
} else {
$reply = sprintf ("Погода в городе %s, %s:\n%s, ветер %s %s м/c, температура %s-%s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
@@ -162,7 +161,7 @@ sub Weather {
$w->{temperature_max},
$w->{temperature_feelslike},
$w->{humidity},
$w->{pressure}
$w->{pressure},
);
}
} else {