root/examples/overloading.pl

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
6use v6-alpha;
7
8multi postfix:<!> ($x) { [*] 1..$x };
9multi postfix:<!> (@x) { [*] @x };
10
11multi infix:<z> (@x, @y) { each(@x;@y) };
12multi infix:<z> (Str $x, Str $y) { $x ~ $y };
13
14my @x = 1..5;
15my @y = 6..10;
16
17(@x z @y).perl.say;
18my $test = "hello" z "goodbye";
19$test.perl.say;
20
21$test = 10!; $test.perl.say;
22
23my @test = (1..5);
24$test = @test!;
25$test.perl.say;
26
27multi sub postfix:<<%%>> ($_) { $_ / 100 }; #since overloading % breaks it in infix
28multi sub infix:<<of>> ($x,$y) {$x * $y};
29say 50%% of 100;
30
31sub 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
37multi sub infix:<<base>> ($x,$y) {base($x,$y)};
38say $_ 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;
Note: See TracBrowser for help on using the browser.