Skip to content
This repository
Browse code

[mmd] MAIN example

  • Loading branch information...
commit e0c859e73b7ed011ae8ac90895c4d9689ce8f0be 1 parent 4507df3
Moritz Lenz authored April 10, 2011

Showing 1 changed file with 35 additions and 0 deletions. Show diff stats Hide diff stats

  1. 35  src/multi-dispatch.pod
35  src/multi-dispatch.pod
Source Rendered
@@ -541,3 +541,38 @@ argument list.  This delegation is common in object destructors, where each
541 541
 subclass may perform some cleanup for its own particular data.  After it
542 542
 finishes its work, it can delegate to its parent class meethod by calling
543 543
 C<nextsame>.
  544
+
  545
+=head1 Multiple MAIN subs
  546
+
  547
+C<MAIN> routines can also be multis. That way it is easy to add separate code
  548
+for each action that the script can perform.
  549
+
  550
+=begin programlisting
  551
+
  552
+    # file calc.pl
  553
+    multi MAIN('add', $x, $y)  { say $x + $y }
  554
+    multi MAIN('div', $x, $y)  { say $x / $y }
  555
+    multi MAIN('mult', $x, $y) { say $x * $y }
  556
+
  557
+=end programlisting
  558
+
  559
+Example calls to this script:
  560
+
  561
+=begin screen
  562
+
  563
+    $ perl6 calc.pl add 3 5
  564
+    8
  565
+    $ perl6 calc.pl mult 3 5
  566
+    15
  567
+    $ perl6 calc.pl
  568
+    Usage:
  569
+    calc.pl add x y
  570
+    or
  571
+    calc.pl div x y
  572
+    or
  573
+    calc.pl mult x y
  574
+
  575
+=end screen
  576
+
  577
+Here the multi dispatcher selects the routine based on the value of the first
  578
+argument.

0 notes on commit e0c859e

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