|
Revision 15655, 1.0 kB
(checked in by audreyt, 21 months ago)
|
|
* overloading.pl: Put explicit $_ in signature as it's required now.
|
-
Property svn:mime-type set to
text/plain; charset=UTF-8
-
Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 | # plays with some fun operator overloading. |
|---|
| 2 | # |
|---|
| 3 | # Please remember to update t/examples/examples.t and rename |
|---|
| 4 | # examples/output/overloading if you rename/move this file. |
|---|
| 5 | |
|---|
| 6 | use v6-alpha; |
|---|
| 7 | |
|---|
| 8 | multi postfix:<!> ($x) { [*] 1..$x }; |
|---|
| 9 | multi postfix:<!> (@x) { [*] @x }; |
|---|
| 10 | |
|---|
| 11 | multi infix:<z> (@x, @y) { each(@x;@y) }; |
|---|
| 12 | multi infix:<z> (Str $x, Str $y) { $x ~ $y }; |
|---|
| 13 | |
|---|
| 14 | my @x = 1..5; |
|---|
| 15 | my @y = 6..10; |
|---|
| 16 | |
|---|
| 17 | (@x z @y).perl.say; |
|---|
| 18 | my $test = "hello" z "goodbye"; |
|---|
| 19 | $test.perl.say; |
|---|
| 20 | |
|---|
| 21 | $test = 10!; $test.perl.say; |
|---|
| 22 | |
|---|
| 23 | my @test = (1..5); |
|---|
| 24 | $test = @test!; |
|---|
| 25 | $test.perl.say; |
|---|
| 26 | |
|---|
| 27 | multi sub postfix:<<%%>> ($_) { $_ / 100 }; #since overloading % breaks it in infix |
|---|
| 28 | multi sub infix:<<of>> ($x,$y) {$x * $y}; |
|---|
| 29 | say 50%% of 100; |
|---|
| 30 | |
|---|
| 31 | sub base (Int $M, Int $N) { |
|---|
| 32 | return $M if ($M < $N); |
|---|
| 33 | my $t = $M % $N; |
|---|
| 34 | return base(int($M/$N),$N) ~ $t; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | multi sub infix:<<base>> ($x,$y) {base($x,$y)}; |
|---|
| 38 | say $_ base 2 for (1..5); |
|---|
| 39 | |
|---|
| 40 | # Commented so this file can be used in example.t. |
|---|
| 41 | # multi sub infix:<<.?.>> ($low,$high) { int( rand($high - $low) + $low ) + 1; }; |
|---|
| 42 | # say 1 .?. 5; |
|---|
| 43 | # say 10 .?. 20; |
|---|