File Coverage

blib/lib/Unicode/Fraction.pm
Criterion Covered Total %
statement 25 25 100.0
branch 5 6 83.3
condition 1 3 33.3
subroutine 8 8 100.0
pod 1 1 100.0
total 40 43 93.0


line stmt bran cond sub pod time code
1 1     1   24333 use strict;
  1         3  
  1         40  
2 1     1   5 use warnings;
  1         2  
  1         39  
3             package Unicode::Fraction;
4             # ABSTRACT: easy generation of UTF-8 fractions
5              
6 1     1   26 use Carp;
  1         3  
  1         114  
7 1     1   850 use Unicode::Subscript qw(subscript superscript);
  1         8908  
  1         93  
8              
9             BEGIN {
10 1     1   11 use Exporter;
  1         2  
  1         58  
11 1     1   17 our @ISA = qw(Exporter);
12 1         374 our @EXPORT_OK = qw(fraction);
13             }
14              
15             my $SOLIDUS = "\x{2044}";
16             my $ONE_OVER = "\x{215f}";
17              
18             my %FRACTION_CHAR = (
19             '1/2' => "\x{00bd}",
20             '0/3' => "\x{2189}",
21             '1/3' => "\x{2153}",
22             '2/3' => "\x{2154}",
23             '1/4' => "\x{00bc}",
24             '3/4' => "\x{00be}",
25             '1/5' => "\x{2155}",
26             '2/5' => "\x{2156}",
27             '3/5' => "\x{2157}",
28             '4/5' => "\x{2158}",
29             '1/6' => "\x{2159}",
30             '5/6' => "\x{215a}",
31             '1/7' => "\x{2150}",
32             '1/8' => "\x{215b}",
33             '3/8' => "\x{215c}",
34             '5/8' => "\x{215d}",
35             '7/8' => "\x{215e}",
36             '1/9' => "\x{2151}",
37             '1/10' => "\x{2152}",
38             );
39              
40              
41             sub fraction {
42 24     24 1 8783 my ($num, $denom) = @_;
43 24 50 33     114 defined $num && defined $denom or croak 'usage: fraction($num, $denom)';
44              
45 24 100       43 if (my $frac = _fraction_char($num, $denom)) {
    100          
46 19         68 return $frac;
47             }
48             elsif ($num == 1) {
49 2         8 return $ONE_OVER . subscript($denom);
50             }
51             else {
52 3         10 return superscript($num) . $SOLIDUS . subscript($denom);
53             }
54             }
55              
56             # Return the special Unicode char for selected fractions
57             # e.g. 1/2, 3/8 etc. or return undef
58             sub _fraction_char {
59 24     24   36 my ($num, $denom) = @_;
60 24         94 return $FRACTION_CHAR{"$num/$denom"};
61             }
62              
63              
64             1;
65              
66              
67             __END__