develooper Front page | perl.perl6.users | Postings from July 2018

MAIN subroutine

Thread Next
From:
Laurent Rosenfeld via perl6-users
Date:
July 19, 2018 21:31
Subject:
MAIN subroutine
Message ID:
CAO=jjnqw1Mc2vf2v7+BaBTENxi4R+zOukvQDZmsWs-PC=6B2tA@mail.gmail.com
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 MAIN.

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.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About