Guest User

Untitled

a guest
Sep 22nd, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. The ability to define your own operators, like in haskell, is a
  2. very nice language feature. Perl 6 allows that too but it goes
  3. too far. It allows you to define all kinds of weird operators (e.g., infix, postfix, prefix, postcircumfix, metaoperators ... ).
  4.  
  5. For instance there's the '=' metaoperator. It acts like the well
  6. known '+=', '*=' etc. except it (theoretically) works for all
  7. operators.
  8.  
  9. Some examples:
  10.  
  11. my $a = "foo "; # $a => "foo "
  12. $a.chop; # $a => "foo "
  13. $a.=chop; # $a => "foo"
  14.  
  15. my $b = "bar"; # $b => "bar"
  16. $b => 1; # $b => "bar"
  17. $b =>= 1; # $b => ("bar" => 1)
  18.  
  19. So far that's quite nice but now it gets ugly:
  20.  
  21. my $c = 1; # $c => 1
  22. $c >=; # $c => 1
  23. Here '>=' is itself an operator and thus is given priority over the '=' postfix operator.
  24.  
  25. $c >== 2; # $c => False
  26. Here it works again.
  27.  
  28.  
  29. $c = 1; $c <== 2; # throws an error
  30. You'd probably expect this to work too but '<==' is the feed operator and takes priority.
  31.  
  32. You can also use letters in your operators:
  33.  
  34. sub postfix:<foobar> ($x) {$x + 1}
  35. 2foobar; # => 4
  36. $c = 2; $cfoobar; # error, no such variable
  37. postfix:<foobar> $c; # => 4
  38.  
  39. 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