📘 Optional parameters in Perl 6 subs

📘 Optional parameters in Raku subs

N. B. Perl 6 has been renamed to Raku. Click to read more.


Optional parameters are marked with a question mark after their names. The defined built-in function helps to tell if the parameter was really passed:

sub send-mail(Str $to, Str $bcc?) {
    if defined $bcc {
        # . . .
        say "Sent to $to with a bcc to $bcc.";
    }
    else {
        # . . .
        say "Sent to $to.";
    }
}

send-mail('mail@example.com');
send-mail('mail@example.com', 'copy@example.com');

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code