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

BEGIN blocks are executed during compilation, as soon as they are seen. Since the compiler hasn't seen the body of _init, it has nothing to execute. Declare your sub before the BEGIN and it should work. Otherwise, I think you might have an XY Problem: could you describe in more detail what your BEGIN is going to be doing, or even post the actual code? We can probably find a good solution that does't require some hackery. (also I'm a little confused by what you mean with "BEGIN can't export", since actually BEGIN is crucial in the export/import process)
  • Comment on Re: Can't call sub within same package in BEGIN block

Replies are listed 'Best First'.
Re^2: Can't call sub within same package in BEGIN block
by Athanasius (Archbishop) on Aug 14, 2014 at 08:47 UTC
    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,

      Hadn't had coffee yet, thanks for the correction :-)
      ... 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