Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves op declaration to new page closes #2162
- Loading branch information
Showing
3 changed files
with
39 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
=begin pod | ||
=TITLE Creating operators | ||
=SUBTITLE A short tutorial on how to declare operators and create new ones. | ||
Operators are declared by C<sub> | ||
followed by C<prefix>, C<infix>, C<postfix>, C<circumfix>, or C<postcircumfix>; | ||
then a colon and the operator name in a quote construct. For (post-)circumfix | ||
operators separate the two parts by white space. | ||
sub hello { | ||
say "Hello, world!"; | ||
} | ||
say &hello.^name; # OUTPUT: «Sub» | ||
hello; # OUTPUT: «Hello, world!» | ||
my $s = sub ($a, $b) { $a + $b }; | ||
say $s.^name; # OUTPUT: «Sub» | ||
say $s(2, 5); # OUTPUT: «7» | ||
sub postfix:<♥>($a){ say „I love $a!“ } | ||
42♥; | ||
# OUTPUT: «I love 42!» | ||
sub postcircumfix:<⸨ ⸩>(Positional $a, Whatever){ say $a[0], '…', $a[*-1] } | ||
[1,2,3,4]⸨*⸩; | ||
# OUTPUT: «1…4» | ||
constant term:<♥> = "♥"; # We don't want to quote "love", do we? | ||
sub circumfix:<α ω>($a){ say „$a is the beginning and the end.“ }; | ||
α♥ω; | ||
# OUTPUT: «♥ is the beginning and the end.» | ||
=end pod | ||
|
||
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters