The MAIN sub executes after the mainline.

After all should MAIN be called before or after do-it('2') in the following?

    sub do-it(Str $string) {
        say $string;
    multi sub MAIN() {
        say "This is MAIN so this should print after the mainline";
    multi sub MAIN( $ ) {
        say "This is MAIN so this should print after the mainline";

    # implicitly something like this happens at the end of the file
    MAIN( |@*ARGS );

The only other possibility is that the entire mainline gets run after
MAIN, which doesn't make a lot of sense.
(As in it wouldn't make sense to even have a mainline.)

There is no way to mix the mainline and running MAIN without making it
special case.
One goal of the design of Perl 6 is to reduce special cases.

Running a something like a MAIN subroutine at the end of the file is
actually somewhat common to do in Perl 5.
So there is some precedent for the way it works.

On Thu, Jul 19, 2018 at 4:31 PM Laurent Rosenfeld via perl6-users
<> wrote:
> Hi folks,
> The documentation for the MAIN sub says that "the sub with the special name 
> MAIN is executed after all relevant phasers."
> My understanding is that MAIN automatically executes before any code other 
> than phasers happening at compile time or at the end or right after the end 
> of compilation. It seems that this is not the case. It seems that my 
> perception is wrong and that code not belonging to any sub is executed before 
> For example, take this code:
> sub execute_after_main(Str $string) {
>     say "this $string after MAIN?";
> }
> execute_after_main("1");
> sub MAIN() {
>     say "This is MAIN so this should print first";
> }
> execute_after_main("2");
> This prints the following:
> this 1 after MAIN?
> this 2 after MAIN?
> This is MAIN so this should print first
> I was expecting the MAIN message to be printed first.
> Did I miss something? Is my understanding wrong? I'd never seen that, but I 
> guess all my scripts using MAIN had all the code in subs called from MAIN. So 
> maybe I've never hit that case.
> Cheers,
> Laurent.

Reply via email to