Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The ability to define your own operators, like in haskell, is a
- very nice language feature. Perl 6 allows that too but it goes
- too far. It allows you to define all kinds of weird operators (e.g., infix, postfix, prefix, postcircumfix, metaoperators ... ).
- For instance there's the '=' metaoperator. It acts like the well
- known '+=', '*=' etc. except it (theoretically) works for all
- operators.
- Some examples:
- my $a = "foo "; # $a => "foo "
- $a.chop; # $a => "foo "
- $a.=chop; # $a => "foo"
- my $b = "bar"; # $b => "bar"
- $b => 1; # $b => "bar"
- $b =>= 1; # $b => ("bar" => 1)
- So far that's quite nice but now it gets ugly:
- my $c = 1; # $c => 1
- $c >=; # $c => 1
- Here '>=' is itself an operator and thus is given priority over the '=' postfix operator.
- $c >== 2; # $c => False
- Here it works again.
- $c = 1; $c <== 2; # throws an error
- You'd probably expect this to work too but '<==' is the feed operator and takes priority.
- You can also use letters in your operators:
- sub postfix:<foobar> ($x) {$x + 1}
- 2foobar; # => 4
- $c = 2; $cfoobar; # error, no such variable
- postfix:<foobar> $c; # => 4
- Now whenever you introduce a new operator you have to check that you don't break any of the myriad combinations of the operators you already have (e.g., Zmax, xx=, X=>, >>== ... ).
Advertisement
Add Comment
Please, Sign In to add comment