in reply to BEGIN { } block

because BEGING{} is an implicitly called subroutine in Perl, it can hold any initialization code, You would probably need to use BEGIN{} as a package constructor
package foo; BEGIN{ $text = "Hello\n"; } sub subroutine {print $text} return 1;
now after having initialized $text you can use it in code that calls foo code
require foo; foo::subroutine(); #will print Hello
Other features of BEGIN{}:
Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

Replies are listed 'Best First'.
Re^2: BEGIN { } block
by JadeNB (Chaplain) on Aug 07, 2009 at 16:34 UTC
    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.

      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