| | 137 | Note that C<()> as a subscript is now a sub call, so instead of C<qw(a b)> you |
| | 138 | would write C<< qw<a b> >> or C<qw[a b]> (if you don't like plain C<< <a b> |
| | 139 | >>), that is). |
| | 140 | |
| | 141 | =head2 Other important operator changes |
| | 142 | |
| | 143 | String concatenation is now done with C<~>. |
| | 144 | |
| | 145 | Regex match is done with the smart match operator C<~~>, the perl 5 match |
| | 146 | operator C<=~> is gone. |
| | 147 | |
| | 148 | if "abc" ~~ m/a/ { ... } |
| | 149 | |
| | 150 | C<|> and C<&> as infix operators now construct junctions. The binary AND and |
| | 151 | binary OR operators are split into string and numeric operators, that is C<~&> |
| | 152 | is binary string AND, C<+&> is binary numeric AND, C<~|> is binary string OR |
| | 153 | etc. |
| | 154 | |
| | 155 | Parenthesis don't construct lists any more, they merely group. Lists are |
| | 156 | constructed with the comma operator. It has looser precedence than the list |
| | 157 | assignment operator, which allows you to write lists on the right hand side |
| | 158 | wihtout parens: |
| | 159 | |
| | 160 | my @list = 1, 2, 3; # @list really has three elements |
| | 161 | |
| | 162 | The arrow operator C<< -> >> for dereferncing is gone. Since (nearly) |
| | 163 | everything is a reference, you can directly use the apropriate pair of |
| | 164 | parenthesis for either indexing or method calls: |
| | 165 | |
| | 166 | my $lol = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; |
| | 167 | say $lol[1][0]; # 4 |
| | 168 | |
| | 169 | my $s = sub { say "hi" }; |
| | 170 | $s(); |
| | 171 | # or |
| | 172 | $s.(); |
| | 173 | $lol.[1][0] |
| | 174 | |
| | 342 | Since both builtin functions and operators are multi subs and methods, |
| | 343 | changing their behaviour for particular types is a simple as adding the |
| | 344 | appropriate multi subs and methods. If you want these to be globally |
| | 345 | available, you have to but them into the C<GLOBAL> namespace, which is |
| | 346 | indicated by an asterisk. |
| | 347 | |
| | 348 | multi sub *uc(TurkishStr $str) { ... } |
| | 349 | |
| | 350 | # "overload" the string concatenation: |
| | 351 | multi sub infix:<~>(TurkishStr $us, TurkishStr $them) { ... } |
| | 352 | |
| | 353 | If you want to offer a type cast to a particular type, just provide a method |
| | 354 | with the same name as the type you want to cast to. |
| | 355 | |
| | 356 | sub needs_bar(Bar $x) { ... } |
| | 357 | class Foo { |
| | 358 | ... |
| | 359 | # coercion to type Bar: |
| | 360 | method Bar { ... } |
| | 361 | } |
| | 362 | |
| | 363 | needs_bar(Foo.new); # coerces to Bar |
| | 364 | |
| | 365 | =head2 Offering Hash and List semantics |
| | 366 | |
| | 367 | If you want to write a class whose objects can be assigned to a variable with |
| | 368 | the C<@> sigil, you have to implement the C<Positional> roles. Likewise for |
| | 369 | the C<%> sigil you need to do the C<Associative> role. The C<&> sigil implies |
| | 370 | C<Callable>. |
| | 371 | |
| | 372 | The roles provides the operators C<< postcircumfix:<[ ]> >> (Positional; for |
| | 373 | array indexing), C<< postcircumfix:<{ }> >> |
| | 374 | (Associative) and C<< postcircumfix:<()> >> (Callable). The are technically |
| | 375 | just methods with a fancy syntax. |
| | 376 | You should override these to provide meaningful semantics. |
| | 377 | |
| | 378 | class OrderedHash does Associative { |
| | 379 | multi method postcircumfix:<{ }>(Int $index) { |
| | 380 | # code for accessing single hash elements here |
| | 381 | } |
| | 382 | multi method postcircumfix:<{ }>(*@@slice) { |
| | 383 | # code for accessing hash slices here |
| | 384 | } |
| | 385 | ... |
| | 386 | } |
| | 387 | |
| | 388 | my %orderedHash = OrderedHash.new(); |
| | 389 | say %orderedHash{'a'}; |
| | 390 | |
| | 391 | See L<S13> for all the gory details. |
| | 392 | |