in reply to Re: BEGIN { } block
in thread BEGIN { } block

  1. You mean just BEGIN, not sub BEGIN. You're also missing a semicolon after package foo.
  2. You should be naming your package files with extension .pm, not .pl. You also don't need to specify the extension when require- or use-ing.
  3. You define subroutine in package foo, but try to use the subroutine from package1.
  4. The biggest one: The behaviour you describe has nothing to do with BEGIN. It works just the same if you remove the BEGIN {} block.

UPDATE: AnomalousMonk points out (indirectly) that my point (4) is “only just” correct—the very early initialisation provided by BEGIN {} can certainly be useful if you might call a subroutine before some variables in it would be initialised by the normal flow of code.

Replies are listed 'Best First'.
Re^3: BEGIN { } block
by biohisham (Priest) on Aug 07, 2009 at 16:55 UTC
    Thanks for the tips, I have taken due action, and your clarification is appreciated, as for note number 4 , I was seeking simplicity by showing a behavior that is relatively easier to correlate with and that is general, What I have presented has to do more with Packages, just to show one side of the many places BEGIN{} can come in handy and it just shows where BEGIN{} block can feature in a Package...
    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind