Mass and length calculator using Perl 6 custom operator I am studying chemistry in the university, and then I try to write all the things in the textbook with Perl6 or Perl, like balancing the chemical formula or other process!

Then I encountered the problem is on perl6 custom operator.
I feel I have been repeating my code and myself when i use the feature.
It is hard to read and write, and what is the way to deal with such problems?

#!/usr/bin/env perl6
use v6;
#basic SI(International System of Units) type 


role MetricPrefix {
    method baseOn ( Str $base , Numeric $input ) {
        given $base {
            when 'pico' { return $input * 10**-12 }
            when 'namo' { return $input * 10**-9 }
            when 'micro' { return $input * 10**-6}
            when 'milli' { return $input * 10**-3 }
            when 'centi' { return $input * 10**-2 }
            when 'hecto' { return $input * 10**2 }
            when 'kilo' { return $input * 10**3 }
            when 'mega' { return $input * 10**6 }
            when 'giga' { return $input * 10**9 }
            when 'tera' { return $input * 10**12 }
            default { fail "you must input a metric prefix which allow pico to tera" }
        }
    }
}



class Mass does MetricPrefix {
    #basic Mass is g is different form si statda
    has $.g;

    submethod BUILD ( :$!g  ) {
    }

}

class Length does MetricPrefix {
    has $.Length ;

    submethod BUILD ( :$!Length  ) {
    }
}



multi postfix:&lt;(kg)&gt;( $input ) {
    return Mass.new( g =&gt; Mass.baseOn("kilo",$input) ) or fail "you Must input a number";
}

multi postfix:&lt;(g)&gt;( $input ) {
    return Mass.new( g =&gt; $input ) or fail "you Must input a number";
}

multi infix:&lt;+&gt;( Mass $inputOne , Mass $inputTwo ) is assoc&lt;right&gt; {
    return Mass.new( g =&gt; $inputOne.g + $inputTwo.g) or fail "error in there ";
}

multi infix:&lt;-&gt;( Mass $inputOne , Mass $inputTwo ) is assoc&lt;right&gt; {
    return Mass.new( g =&gt; $inputOne.g - $inputTwo.g) or fail "error in there ";
}

multi infix:&lt;*&gt;( Mass $inputOne , Mass $inputTwo ) is assoc&lt;right&gt; is tighter( &amp;infix:&lt;+&gt; ) is tighter( &amp;infix:&lt;-&gt; ) is tighter( &amp;infix:&lt;/&gt;) {
    return Mass.new( g =&gt; $inputOne.g * $inputTwo.g) or fail "error in there ";
}

multi infix:&lt;/&gt;( Mass $inputOne , Mass $inputTwo ) is assoc&lt;right&gt; is tighter( &amp;infix:&lt;+&gt; ) is tighter( &amp;infix:&lt;-&gt; )  {
    return Mass.new( g =&gt; $inputOne.g / $inputTwo.g) or fail "error in there ";
}





#the meterLeng
multi postfix:&lt;(km)&gt;( $input ) {
    return Length.new( Length =&gt; Length.baseOn("kilo",$input) ) or fail "you Must input a number";
}

multi postfix:&lt;(m)&gt;( $input ) {
    return Length.new( Length =&gt; $input ) or fail "you Must input a number";
}

multi infix:&lt;+&gt;( Length $inputOne , Length $inputTwo ) is assoc&lt;right&gt; {
    return Length.new( Length =&gt; $inputOne.Length + $inputTwo.Length) or fail "error in there ";
}

multi infix:&lt;-&gt;( Length $inputOne , Length $inputTwo ) is assoc&lt;right&gt; {
    return Length.new( Length =&gt; $inputOne.Length - $inputTwo.Length) or fail "error in there ";
}

multi infix:&lt;*&gt;( Length $inputOne , Length $inputTwo ) is assoc&lt;right&gt; is tighter( &amp;infix:&lt;+&gt; ) is tighter( &amp;infix:&lt;-&gt; ) is tighter( &amp;infix:&lt;/&gt;) {
    return Length.new( Length =&gt; $inputOne.Length * $inputTwo.Length) or fail "error in there ";
}

multi infix:&lt;/&gt;( Length $inputOne , Length $inputTwo ) is assoc&lt;right&gt; is tighter( &amp;infix:&lt;+&gt; ) is tighter( &amp;infix:&lt;-&gt; )  {
    return Length.new( Length =&gt; $inputOne.Length / $inputTwo.Length) or fail "error in there ";
}


#just a test
say 10(kg) + 1(g);
say 10(m) + 1(m);
This is an interesting use case of postfixes.  Here's how I would go about it.  Unfortunately, because postfixes can't take regexen, you need to specify each one individually, but it's not horrible, especially if you throw things into a module. 

Let's first create a postfix operator for each value type:

multi postfix:&lt;kg&gt; (Numeric() $m) { Mass.new: $m, 'k' }
multi postfix:&lt;g&gt;  (Numeric() $m) { Mass.new: $m      }
multi postfix:&lt;mg&gt; (Numeric() $m) { Mass.new: $m, 'm' }


Basically, we're saying we want to attach g onto something.  We'd like it to be a number type, so we make have it only act on Numeric types, but if someone passes us a string, the () tells it to coerce it for us.  And then we make a Mass object from those the value and the prefix.

Now let's take a look at the Mass class.  All we really need to have in it is a weight and probably store its prefix so if we create a kg if we want to display it we can keep it in kg and not switch to mg or something.  We'll store things in grams, so on creation we use the prefix to shrink/expand the value. 

class Mass {
  has $.grams;
  has $.prefix;
  method new  ($value, $prefix) {
    self.bless: 
      :grams($value * si-prefix{$prefix}),
      :$prefix
  }
}


The si-prefix is just a map of values: 

my constant \si-prefix := Map.new( 'k', 1000, '', 1, 'm', 0.001 );


By making it independent of the gram, we can use it later for a length / etc class.  For that reason, I'd define it outside of the class so that it can be used by others easily.  Believe it or not, it is possible to have an empty string as a key.  Kind of useful in our case.

At this point, we can start to use our values:

say 500g; # --&gt; Weight.new(grams =&gt; 500, prefix =&gt; "")
say 2kg;  # --&gt; Weight.new(grams =&gt; 2000, prefix =&gt; "kg")


Okay, not the most useful output, but at least we know everything is working.  Let's make a Str and gist method that makes more sense:

  method Str {
    $!grams / si-prefix{$!prefix}
    ~ $!prefix                     
    ~ 'g'
  }
  method gist { self.Str }


Now all we really have to do is handle the addition:

multi infix:&lt;+&gt; (Mass \a, Mass \b) { Weight.new: a.grams + b.grams } 
multi infix:&lt;-&gt; (Mass \a, Mass \b) { Weight.new: a.grams - b.grams } 


This works great, but does automatically convert everything back to being a g unit even if both were kg, etc.  At this point, it's a bit of taste.  You could decide that the first or second unit has precedence, or implement some more complex logic (larger or smaller, or the one that results in no more than three decimal places or no more than three whole units, etc).  I'll leave that for you to implement.  

As I said, this would work great as a module.  You can do that like this:

unit module SI-Operators;

my constant \si-prefix := Map.new(
  'Y',  10 **  24,   'y',  10 ** -24,
  'Z',  10 **  21,   'z',  10 ** -21,
  'E',  10 **  18,   'a',  10 ** -18,
  'P',  10 **  15,   'f',  10 ** -15,
  'T',  10 **  12,   'p',  10 ** -12,
  'G',  10 **   9,   'n',  10 **  -9,
  'M',  10 **   6,   'μ',  10 **  -6,
  'k',  10 **   3,   'm',  10 **  -3,
  'h',  10 **   2,   'c',  10 **  -2,
  'da', 10 **   1,   'd',  10 **  -1,
  '',   1  
);

# MASSES

class Mass is export (:mass) {
  has $.grams;
  has $.prefix;
  method new  ($number, $prefix = '') {
    self.bless: 
      :grams($number * si-prefix{$prefix}),
      :$prefix
  }
  method Str {
    $!grams / si-prefix{$!prefix}
    ~ $!prefix
    ~ 'g'
  }
  method gist { self.Str }
}

multi infix:&lt;+&gt; (Mass \a, Mass \b) is export (:mass) { Mass.new: a.grams + b.grams } 
multi infix:&lt;-&gt; (Mass \a, Mass \b) is export (:mass) { Mass.new: a.grams - b.grams } 

multi postfix:&lt;Yg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'Y'  }
multi postfix:&lt;Zg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'Z'  }
multi postfix:&lt;Eg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'E'  }
multi postfix:&lt;Pg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'P'  }
multi postfix:&lt;Tg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'T'  }
multi postfix:&lt;Gg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'G'  }
multi postfix:&lt;Mg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'M'  }
multi postfix:&lt;kg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'k'  }
multi postfix:&lt;hg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'h'  }
multi postfix:&lt;dag&gt; (Numeric() $m) is export (:mass) { Mass.new: $m, 'da' }
multi postfix:&lt;g&gt;   (Numeric() $m) is export (:mass) { Mass.new: $m       }
multi postfix:&lt;dg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'd'  }
multi postfix:&lt;cg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'c'  }
multi postfix:&lt;mg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'm'  }
multi postfix:&lt;μg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'μ'  }
multi postfix:&lt;ng&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'n'  }
multi postfix:&lt;pg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'p'  }
multi postfix:&lt;fg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'f'  }
multi postfix:&lt;ag&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'a'  }
multi postfix:&lt;zg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'z'  }
multi postfix:&lt;yg&gt;  (Numeric() $m) is export (:mass) { Mass.new: $m, 'y'  }



# LENGTH

class Length is export (:length) {
  has $.meters;
  has $.prefix;
  method new  ($number, $prefix = '') {
    self.bless: 
      :meters($number * si-prefix{$prefix}),
      :$prefix
  }
  method Str {
    $!meters / si-prefix{$!prefix}
    ~ $!prefix
    ~ 'm'
  }
  method gist { self.Str }
}

multi infix:&lt;+&gt; (Length \a, Length \b) is export (:length) { Length.new: a.meters + b.meters } 
multi infix:&lt;-&gt; (Length \a, Length \b) is export (:length) { Length.new: a.meters - b.meters } 

multi postfix:&lt;Ym&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'Y'  }
multi postfix:&lt;Zm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'Z'  }
multi postfix:&lt;Em&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'E'  }
multi postfix:&lt;Pm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'P'  }
multi postfix:&lt;Tm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'T'  }
multi postfix:&lt;Gm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'G'  }
multi postfix:&lt;Mm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'M'  }
multi postfix:&lt;km&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'k'  }
multi postfix:&lt;hm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'h'  }
multi postfix:&lt;dam&gt; (Numeric() $l) is export (:mass) { Length.new: $l, 'da' }
multi postfix:&lt;m&gt;   (Numeric() $l) is export (:mass) { Length.new: $l       }
multi postfix:&lt;dm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'd'  }
multi postfix:&lt;cm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'c'  }
multi postfix:&lt;mm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'm'  }
multi postfix:&lt;μm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'μ'  }
multi postfix:&lt;nm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'n'  }
multi postfix:&lt;pm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'p'  }
multi postfix:&lt;fm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'f'  }
multi postfix:&lt;am&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'a'  }
multi postfix:&lt;zm&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'z'  }
multi postfix:&lt;ym&gt;  (Numeric() $l) is export (:mass) { Length.new: $l, 'y'  }


What's great about doing it as a module is you can only import what you need.  For example, if in most of your program you're not using SI units, there's no reason to polute the operator namespace with lots of postfixes.  But when you do:

sub stuff-with-lengths {
  use SI-Operators :length;
  say 25mm + 78km # 78000.025m
}


Note that multiplying, etc, would need you to implement more math operator subs.  But could also lead to interesting things like a Length * Length generating an Area etc, and Area * Length getting Volume