in reply to Re: Can't call sub within same package in BEGIN block
in thread Can't call sub within same package in BEGIN block

Declare your sub before the BEGIN and it should work.

No, it won’t:

18:37 >perl -Mstrict -wE "use subs 'TestMe'; BEGIN { TestMe(); } sub T +estMe { say 'Hi!'; }" Undefined subroutine &main::TestMe called at -e line 1. BEGIN failed--compilation aborted at -e line 1. 18:37 >

As Anonymous Monk pointed out above, you have to define the sub before the BEGIN block:

18:37 >perl -Mstrict -wE "sub TestMe { say 'Hi!'; } BEGIN { TestMe(); +}" Hi! 18:41 >

In Perl, as in C, it is important to keep in mind the distinction between declaration and definition.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^3: Can't call sub within same package in BEGIN block
by Anonymous Monk on Aug 14, 2014 at 09:04 UTC
    Hadn't had coffee yet, thanks for the correction :-)
Re^3: Can't call sub within same package in BEGIN block
by exilepanda (Friar) on Aug 14, 2014 at 23:21 UTC
    ... the distinction between declaration and definition.
    Hey I really love this one!! A very neat comparison. New lesson learned!!

    Anyway I can get what Anonymous Monk means in the another reply that said "you can't call a subroutine until it is defined "

    Cheers