Showing posts with label stringification. Show all posts
Showing posts with label stringification. Show all posts

Perl Module(s) Of The Week - 2019 Week 01 - Data::Money

Since this item was on my to-do list for quite some time and this is the first week of the new year, might as well proceed ahead and do it. Inspired by Python Module of the Week (PyMOTW), I will start a series of weekly blog posts to review and discuss any interesting Perl module(s) that caught my attention. Write up mostly will be on some code example, issue encounter, and what can we learn from the code, as in code reading.

Our Perl module of this week is Data::Money, which allows us to perform math operations on different currency in an object manner and let us understand how operator overloading works in Perl. Furthermore, this is a good example to showcase the actual implementation of PoEAA's Money class design (there are actual four Money class designs exists), which is a rather simple approach which does not allows multiple currencies operation. Nevertheless, an actual production ready implementation.

Installation and quick check of the module.
$ cpanm Data::Money

It seemed in latest Perl version (v5.28.0), the dependant modules, Locale::Currency and Locale::Codes will be removed from Perl core distribution.
$ perl -e "use Data::Money"
Locale::Currency will be removed from the Perl core distribution in the next major release. 
Please install it from CPAN. It is being used at /usr/local/lib/perl5/site_perl/5.28.0/Data/Money.pm, line 27.
Locale::Codes will be removed from the Perl core distribution in the next major release. 
Please install it from CPAN. It is being used at /usr/local/lib/perl5/5.28.0/Locale/Currency.pm, line 22.

To remove the warnings, we have to install Locale::Currency manually.
$ cpanm Locale::Currency
$ perl -e "use Data::Money"

Let write some code (save as file `ex1.pl`) that create the new Money object or data type. `MYR` is the currency code where `MY` is two digits country code for `MalaYsia` and `R` is the name of the currency, in this case, `Ringgit`.
use strict;
use warnings;
use feature 'say';

use Data::Money;

my $price = Data::Money->new(value => 1.2, code => 'MYR');
say $price->as_string;
say $price->stringify;

Running the code will give us the same result twice. `RM` is the currency symbol that replaced our previous used `$` symbol.
$ perl ex1.pl
RM1.20
RM1.20

Let's look at these two subroutines, `as_string` and `stringify`. First, `as_string` is an alias to `stringify` subroutine. To create an subroutine alias in Perl, we will need to use typeglob (hence, the asterisk notation) to manipulate the Symbol Table, which stores all the variables in a package.
*as_string = \&stringify;

Second, as the name implied, `stringify` is typical subroutine naming convention for stringification in Perl, to convert an object into string or context of string. This is done through `overload` package as shown below, where it's implemented through anonymous subroutine. The subroutine will be invoked whenever the blessed package was called in a string context. You can read this tutorial for a sample working stringification using classical OOP way.
use overload
    '""' => sub { shift->stringify };