Number::Fraction

Dave Cross dave at dave.org.uk
Wed May 1 16:30:01 BST 2013


Quoting "Th. J. van Hoesel" <th.j.v.hoesel at gmail.com>:

> Hello,
>
> As agreed upon with Dave Cross, I would make some really cool  
> changes to his module Number::Fraction - I as former math teacher  
> love fraction.
>
> However, can someone point me out what is happening between lines  
> 132 and 144 of Fraction.pm ? This part is the neatest part of the  
> module, where it enables the module to use constants in your Perl  
> programs.

Here's the code on github:

https://github.com/davorg/number-fraction/blob/master/lib/Number/Fraction.pm#L132

And here's the relevant section of the overload.pm documentation:

http://perldoc.perl.org/overload.html#Overloading-Constants

> my %_const_handlers =
>   (q => sub { return __PACKAGE__->new($_[0]) || $_[1] });

This defines a subroutine that Perl will use to parse any quoted  
strings. If Number::Fraction->new can construct an object out of them,  
it will. Otherwise new() returns false and the original string is used  
instead.

> =head2 import
>
> Called when module is C<use>d. Use to optionally install constant
> handler.
>
> =cut
>
> sub import {
>   overload::constant %_const_handlers if $_[1] and $_[1] eq ':constants';
> }

And this installs the constant handler if the module is used with the  
correct argument (use Number::Fraction ':constants').

Should really give users the option to uninstall that handler too, I guess.

Cheers,

Dave...




More information about the london.pm mailing list