CPAN Release of TooMuchCode 0.17

Perl::Critic::TooMuchCode is a set of policy addons that generally checks for dead code or redundant code.

I feel grateful that this small project starts to draw some attention and endorsement and now it is receiving pull-requests from the Perl/CPAN community.

In version 0.17 we improved the policy ProhibitDuplicateLiteral and now it we can whitelist strings and numbers in configurations. If you somehow really need to use number 42 and “forty two” literally in the code many times, you list them in .perlcriticrc:

[TooMuchCode::ProhibitDuplicateLiteral]
whitelist = "forty two" 42

Thanks to @ferki from project Rex!

Rex is an automation framework, or remote-execution framework. In a sense, similar to Ansible. It’s a simple way of telling machine what to do, with some code named Rexfile — a task manifest. which is also perl code. Within which you’d repeat some strings literally such as:

service 'apache2',  ensure => 'started';
service 'mysql',    ensure => 'started';
service 'memcached, ensure => 'started';

… and that is OK. Because those manifests should be direct instead of indirect/abstract with the use of variables / constants to just hold the string 'started'. Doing such is an unnecessary level of indirection since the purpose of task manifests is to tell machine what to do and if human readers needs to read twice before they can understand what it really means, the is a problem.

Well, apparently in the context of coding Rexfile, some repetition are allowed. In general, perhaps repetition are OK in any DSL code. DSL itself already reduce some repetition by hiding the details and DSL keywords tends to be high-level constructs that are designed to be easily understandable for human readers. Repetition of DSL keywords almost never leads to be unreadable or “bad small”.

I might have repeated the word “repetition” too many times.


Originally posted at: gugod’s blog — CPAN Release of TooMuchCode 0.17


Source link

CPAN Release of Time::Verbal module

Time::Verbal 1.1.1 made its way to CPAN.

This module dose one little things, that is to convert the time differece to a verbal format, rounded to some unit that “makes sense”, just as how you would say such thing as a human.

my o = Time::Verbal->new();
say $o->distance( time(), time() - 4567 );
#=> about 1 hour

Besides the default English format, other 67 languages identified by these ISO language codes are supported:

ar bg bn-IN bs ca cy da de-AT de-CH de dsb el en-AU en-GB en-US eo es-AR es-CL es-CO es-MX es-PE es et eu fa fi fr-CA fr-CH fr fur gl-ES gsw-CH he hi-IN hi hr hsb hu id is it ja ko lo lt lv mk mn nb nl nn pl pt-BR pt-PT rm ro ru sk sl sr-Latn sr sv-SE sw tr uk vi zh-CN zh-TW

I don’t know all these languages (nor their language codes), but Locale::Wolowiz does. Or, at least it is the library that does the translation, given a bunch of JSON files that contain the body of translations.

So here’s how you say “about 1 hour” in Vietnamese:

my o = Time::Verbal->new( "locale" => "vi" );
say $o->distance( time(), time() - 4567 );
#=> khoảng 1 giờ

A default version of translations of all the above-mentioned 68 languages are provided with Time::Verbal itself and is ready to use once installed properly — a usual cpan or cpanm or cpm command would suffice. However, in case some customize version is need because, say, you wish to provide better UX to whom spekas Vulcan variant of Kligon, you need to create a file named after the language code — which can be arbitrary such as tlh-Vulcan. You would create tlh-Vulcan.json and put it under /app/i18n, or really any directory of your choice, then do:

my o = Time::Verbal->new(
    "locale" => "tlh-Vulcan",
    "i18n_dir" => "/app/i18n"
);
say $o->distance( time(), time() - 4567 );
#=> Chaq wa' rep

Disclaimer — I don’t know if that’s how Volcanized Kligon speaks. Don’t take my example as if it is authentic.

To prepare the translation, take a look of the json files under the default i18n_dir, which can be printed by running this oneliner:

perl -MTime::Verbal -E 'say Time::Verbal->new()->i18n_dir'

It shouldn’t be difficult to grasp the structure of those translation files and produce a new one. It is however, very difficult to start describing the schema of thoes json file as an indolent developer.

The translations are accquired from rails-i18n project — which is one of the first modules that does things like this.


Originally posted at https://gugod.org/2021/09/cpan-release-of-time-verbal/


Source link