rir has asked for the wisdom of the Perl Monks concerning the following question:

In the last few years, I've started to notice code like this:
package Something; BEGIN { use Exporter (); use vars qw($VERSION @ISA @EXPORT_OK); $VERSION = '0.05'; @ISA = qw(Exporter); @EXPORT_OK = qw(&errstr); }
To me, the BEGIN block around the Exporter stuff seems extraneous. What danger is this construction meant to avoid?

Be well,
rir

Replies are listed 'Best First'.
Re: BEGIN { use Exporter; ...
by tilly (Archbishop) on Dec 22, 2008 at 18:08 UTC
    Some of it is cargo culting, but the marginal benefit comes if you have circular dependencies. If A uses B uses C uses A and you use A then when C calls A->import, A has only been compiled, not executed. This construct guarantees that the import will still work as expected.

    Avoiding the circular dependency is the better solution, of course.

    Update: My bad, I incorrectly started A's state when C calls A->import. At that point A has been compiled up to the use B; line and not executed. So you want the BEGIN block to be before that. The purpose of the construct is the same, though.

    An alternate solution is to have A:

    require B; B->import(qw(some stuff if desired));
    This is in many ways superior because when C calls A you will have A completely compiled, if not yet executed.