Re: MAIN subroutine
In general, it needs to be improved. https://github.com/perl6/doc/issues/2167 Please add to that issue or open another one to comment this specific fact. Cheers JJ
Re: MAIN subroutine
I should mention something less obvious as well: a declaration both manipulates symbols known to the compiler, and usually causes allocation of storage at runtime; and for classes, composition has runtime components. Among other things that must be split between compile-time and runtime. So it's not just explicit initialization that relies on this. On Thu, Jul 19, 2018 at 5:48 PM Brandon Allbery wrote: > Consider that "declarations" are actually executable code. They have > compile-time meaning, but many also have runtime meaning. This is most > obvious when it's a variable with an initializer: the initialization is at > runtime, not compile time. Which means MAIN has to run after all such, or > variables won't be initialized properly. > > Most commonly you would not combine a MAIN sub with non-declaring code. > > On Thu, Jul 19, 2018 at 5:32 PM Laurent Rosenfeld via perl6-users < > perl6-users@perl.org> 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 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. >> > > > -- > brandon s allbery kf8nh sine nomine > associates > allber...@gmail.com > ballb...@sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > -- brandon s allbery kf8nh sine nomine associates allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
Re: MAIN subroutine
Thanks Brandon for your answer. Yes, I agree that it makes sense that declarations (and initializations) outside any sub should run before the MAIN sub. And I agree that we should probably avoid combining a MAIN sub with non-declaring code. (And, as I said, I've never hit the case before because I would normally not do that: to me, if I have a MAIN sub, any executable code should be in MAIN or in subs called (directly or indirectly) from MAIN. But then, perhaps the documentation should be clearer on that. And many thanks for the others who answered meanwhile. Best, Laurent. 2018-07-19 23:48 GMT+02:00 Brandon Allbery : > Consider that "declarations" are actually executable code. They have > compile-time meaning, but many also have runtime meaning. This is most > obvious when it's a variable with an initializer: the initialization is at > runtime, not compile time. Which means MAIN has to run after all such, or > variables won't be initialized properly. > > Most commonly you would not combine a MAIN sub with non-declaring code. > > On Thu, Jul 19, 2018 at 5:32 PM Laurent Rosenfeld via perl6-users < > perl6-users@perl.org> 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 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. >> > > > -- > brandon s allbery kf8nh sine nomine > associates > allber...@gmail.com > ballb...@sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net >
Re: MAIN subroutine
> On 19 Jul 2018, at 23:31, Laurent Rosenfeld via perl6-users > wrote: > 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. This is something I’ve also learned the other day :-( Liz
Re: MAIN subroutine
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; } do-it("1"); multi sub MAIN() { say "This is MAIN so this should print after the mainline"; } do-it("2"); 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 > 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.
Re: MAIN subroutine
Consider that "declarations" are actually executable code. They have compile-time meaning, but many also have runtime meaning. This is most obvious when it's a variable with an initializer: the initialization is at runtime, not compile time. Which means MAIN has to run after all such, or variables won't be initialized properly. Most commonly you would not combine a MAIN sub with non-declaring code. On Thu, Jul 19, 2018 at 5:32 PM Laurent Rosenfeld via perl6-users < perl6-users@perl.org> 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 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. > -- brandon s allbery kf8nh sine nomine associates allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
MAIN subroutine
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.