Perl Module(s) Of The Week - 2019 Week 05 - API::Google

While there are tons of web service API module in the CPAN, we will look into a rather simple and useful Perl's module, API::Google. As the name implies, this module allows us to connect to Google services via the API. There are not a lot of recent released modules written in non-Moo/Moose-way which allows us to look into using the Perl's plain old Object-oriented (OO) way.

Installation is quite straight forward. Usage wise, you will have to obtain the OAuth2 credential and generate the token file, `config.json` file.
$ cpanm API::Module

Let's move on the study and reading the code.

Firstly, I'm quite surprised that the default user agent or HTTP client module to make web request used is not the typical LWP::UserAgent but instead Mojo::UserAgent. There is nothing wrong with LWP::UserAgent, it's an established and works well. But if you need to make non-blocking I/O or Websocket web request, then Mojo::UserAgent is the preferred module, especially for modern web development. Moreover, the documentation is very comprehensive and helpful.

Next, since the author did not use Moo or Moose, therefor a helper subroutine was created to access the object attribute or the hash value.
sub ua {

Looking through the code, I'm constantly reminded that I forget how to join two hashes in Perl. Basically you have to understand the difference between a list and an array or a hash (an associative array) and numerous examples to illustrate that.
  $payload = { payload => $payload };
  %$params = (%$params, %$payload);

No comments:

Post a Comment