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

MAIN subroutine

Thread Next
Laurent Rosenfeld via perl6-users
July 19, 2018 21:31
MAIN subroutine
Message ID:
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?";

sub MAIN() {
    say "This is MAIN so this should print first";

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.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About