| 1 | use v6; |
|---|
| 2 | |
|---|
| 3 | use Test; |
|---|
| 4 | |
|---|
| 5 | # L<"http://use.perl.org/~autrijus/journal/25365"> |
|---|
| 6 | # A closure form of but is desugared into a do given block that eliminates the |
|---|
| 7 | # need of returning $_ explicitly. So those two forms are equivalent: |
|---|
| 8 | # |
|---|
| 9 | # my $foo = Cls.new but { |
|---|
| 10 | # .attr = 1; |
|---|
| 11 | # }; |
|---|
| 12 | # |
|---|
| 13 | # my $foo = do given Cls.new { |
|---|
| 14 | # .attr = 1; |
|---|
| 15 | # $_; |
|---|
| 16 | # }; |
|---|
| 17 | |
|---|
| 18 | plan 13; |
|---|
| 19 | |
|---|
| 20 | sub eval_elsewhere($code){ eval($code) } |
|---|
| 21 | |
|---|
| 22 | # Without an own class |
|---|
| 23 | { |
|---|
| 24 | my $was_in_but_block; |
|---|
| 25 | my $topic_in_but_block; |
|---|
| 26 | |
|---|
| 27 | my $num = 3 but { |
|---|
| 28 | # $_ is 3. |
|---|
| 29 | $was_in_but_block++; |
|---|
| 30 | $topic_in_but_block = $_; |
|---|
| 31 | 23; |
|---|
| 32 | # Here is an implicit ($_;) to get 3 back to $num, instead of 23. |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | is $num, 3, "syntax but worked on a literal"; |
|---|
| 36 | ok $was_in_but_block, "syntax but on a literal was executed"; |
|---|
| 37 | is $topic_in_but_block, 3, "topic in syntax but on a literal was correct"; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | # With an own class |
|---|
| 41 | { |
|---|
| 42 | class SampleClass { has $.attr } |
|---|
| 43 | |
|---|
| 44 | my $was_in_but_block; |
|---|
| 45 | my $topic_in_but_block; |
|---|
| 46 | |
|---|
| 47 | my $obj = SampleClass.new but { |
|---|
| 48 | # $_ is the fresh SampleClass.new. |
|---|
| 49 | $was_in_but_block++; |
|---|
| 50 | $topic_in_but_block = $_; |
|---|
| 51 | .attr = 42; |
|---|
| 52 | 23; |
|---|
| 53 | # Here is an implicit ($_;) to get 3 back to $num, instead of 23. |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | ok $was_in_but_block, 'syntax but ($obj but {...}) was executed'; |
|---|
| 57 | cmp_ok $topic_in_but_block, &infix:<===>, $obj, |
|---|
| 58 | 'topic in syntax but ($obj but {...}) was correct'; |
|---|
| 59 | my $attr = try { $obj.attr }; |
|---|
| 60 | is $attr, 42, "attribute setting worked correctly in syntax but"; |
|---|
| 61 | cmp_ok $obj, &infix:<~~>, SampleClass, "syntax but returned the original object"; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | # L<S02/Context/"can override the class definition:"> |
|---|
| 65 | # L<S12/Roles/generalize a particular enumerated value to its role.> |
|---|
| 66 | { |
|---|
| 67 | my $true_zero is context; |
|---|
| 68 | ok eval_elsewhere('$+true_zero = 0 but True; 1'), "0 but True syntax evaluates", :todo<bug>; |
|---|
| 69 | ok ($true_zero == 0), "0 but True is numerically equal to 0"; |
|---|
| 70 | ok ?($true_zero), "0 but True is true", :todo<bug>; |
|---|
| 71 | # TimToady++ says I can test False as well |
|---|
| 72 | my $false_positive is context; |
|---|
| 73 | ok eval_elsewhere('$+false_positive = 3 but False; 1'), "3 but False syntax evaluates", :todo<bug>; |
|---|
| 74 | ok ($false_positive == 3), "3 but False is numerically equal to 3", :todo<bug>; |
|---|
| 75 | ok !($false_positive), "3 but False is false"; |
|---|
| 76 | } |
|---|