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, |
In reply to Re^2: Can't call sub within same package in BEGIN block
by Athanasius
in thread Can't call sub within same package in BEGIN block
by exilepanda
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |