> On Jun 18, 2022, at 10:42 PM, ToddAndMargo via perl6-users 
> <perl6-us...@perl.org> wrote:
> 
> On 6/16/22 10:10, Rick Bychowski wrote:
>> sub MAIN($n = 20) {
>>    .say for factors($n); # Nil
>> }
> 
> 
> I thought `MAIN` was a reserved variable.  Am
> I missing something?

MAIN has a special meaning as a sub name; it declares a CLI (command-line 
interface).

Just like this code declares a subroutine (that you can call within your 
program) that expects two filenames and an optional flag :
    sub do_it ( $file1, $file2, Bool :$dry-run ) {
        ...
    }
, the same signature in a sub named "MAIN" declares that your whole script is 
to be called on the command-line with two filenames and an optional flag :
    sub MAIN ( $file1, $file2, Bool :$dry-run ) {
        ...
    }

If I call that script like this:
    ./myscript.raku a.txt b.txt
, then MAIN gets 'a.txt' in $file1 and 'b.txt' in $file2. If I call it badly:
    ./myscript.raku a.txt b.txt c.txt
, then I get an error, with a usage message auto-generated by Raku:
    myscript.raku [--dry-run] <file1> <file2>

For full details, see:
https://docs.raku.org/language/create-cli#index-entry-MAIN

-- 
Hope this helps,
Bruce Gray (Util of PerlMonks)

Reply via email to