Skip to content
Browse files

[mmd] MAIN example

  • Loading branch information...
1 parent 4507df3 commit e0c859e73b7ed011ae8ac90895c4d9689ce8f0be @moritz moritz committed
Showing with 35 additions and 0 deletions.
  1. +35 −0 src/multi-dispatch.pod
View
35 src/multi-dispatch.pod
@@ -541,3 +541,38 @@ argument list. This delegation is common in object destructors, where each
subclass may perform some cleanup for its own particular data. After it
finishes its work, it can delegate to its parent class meethod by calling
C<nextsame>.
+
+=head1 Multiple MAIN subs
+
+C<MAIN> routines can also be multis. That way it is easy to add separate code
+for each action that the script can perform.
+
+=begin programlisting
+
+ # file calc.pl
+ multi MAIN('add', $x, $y) { say $x + $y }
+ multi MAIN('div', $x, $y) { say $x / $y }
+ multi MAIN('mult', $x, $y) { say $x * $y }
+
+=end programlisting
+
+Example calls to this script:
+
+=begin screen
+
+ $ perl6 calc.pl add 3 5
+ 8
+ $ perl6 calc.pl mult 3 5
+ 15
+ $ perl6 calc.pl
+ Usage:
+ calc.pl add x y
+ or
+ calc.pl div x y
+ or
+ calc.pl mult x y
+
+=end screen
+
+Here the multi dispatcher selects the routine based on the value of the first
+argument.

0 comments on commit e0c859e

Please sign in to comment.
Something went wrong with that request. Please try again.