multi sub MAIN('send', $filename) {
...
}
multi sub MAIN('fetch', $filename) {
...
}
multi sub MAIN('compare', $file1, $file2) {
...
}
... 是 yadayadayada 占位符
> perl6 "multiple_MAIN.p6"
Usage:
multiple _MAIN.p6 send <filename>
multiple _MAIN.p6 fetch <filename>
multiple _MAIN.p6 compare <file1> <file2>
)
multi postcircumfix:<[ ]> ( Str:D $s, Int:D $n --> Str ) {
$s.substr: $n, 1
}
multi postcircumfix:<[ ]> ( Str:D $s, Range:D $r --> Str ) {
$s.substr: $r.min, $r.max - $r.min + 1
}
multi postcircumfix:<[ ]> ( Str:D $s, List:D $i --> List ) {
map( { $s.substr: $_, 1 }, @$i ).List
}
my $string = 'The quick, purple butterfly';
{
my $single = $string[0];
say $single;
}
{
my $substring = $string[5..9];
say $substring;
}
{
my $substring = $string[1,3,5,7];
say $substring;
}
proto mνlti(|) {*};
multi mνlti(1) { "Yep"};
multi mνlti(2) { "Nay"};
multi mνlti(Int $) { "Welll.."};
say mνlti($_) for ^10;
sub my-single-func (Int $i) { say "You provided an integer: $i"; };
say "my-single-func is a {&my-single-func.perl}";
say "This {&my-single-func.^name} does {&my-single-func.WHY}";
proto my-multi-func (|) {*}
multi my-multi-func (Int $i) { say "You provided an integer $i"; };
multi my-multi-func (Str $s) { say "You provided a string $s"; };
And this is how a multi would work.
You can still access every documentation. Somehos
]
say "my-multi-func is a {&my-multi-func.perl} and does {&my-multi-func.WHY}";
multi sub Screen_get_valid_string($prompt, $accept_empty_string where $_ == True|False, $max_length = 999) { return "abc" }
multi sub Screen_get_valid_string($prompt, $max_length = 999) { return "def" }
my $return = Screen_get_valid_string("enter value for string => ", True);
say 'return is ', $return;
multi sub greet($name) {
say "Ahoj, $name!";
}
multi sub greet($name, $greeting) {
say "$greeting, $name!";
}
greet('Anna');
greet('Лена', 'Привет ');
multi sub double(Int $x) {
return 2 * $x;
}
multi sub double(Str $x) {
return "$x $x";
}
say double(21);
say double("he");
multi foo(Int $x) { 1 }
multi foo(Int $x) is default { 2 }
say foo(1);
class Wide {}
class Middle is Wide {}
class Narrow is Middle {}
multi foo(Narrow $v) { say 'Narrow', $v; 'from Narrow' }
multi foo(Middle $v) {
say 'Middle', $v;
my $result = callwith Narrow;
say "We're back! The return value is $result";
'from Middle'
}
multi foo(Wide $v) { say 'Wide ', $v; 'from Wide' }
foo Middle;
multi foo(Middle $v) {
say 'Middle', $v;
my $result = callsame;
say "We're back! The Return value is $result";
'from Middle'
}
multi foo(Wide $v) { say 'Wide ', $v; 'from Wide' }
foo Middle;