in reply to Re^2: sub BEGIN
in thread sub BEGIN

From my perspective, BEGIN blocks are a form of code smell. In this case, a variation on 'Inappropriate intimacy'.

Several possibilities for a fix stand out:

  1. Making that hash a constant.
  2. Using inheritance rather than poking stuff into remote namespaces.
  3. If the use of the BEGIN block is required to make the code work as need, then a better solution would be to move the require.
  4. If the BEGIN block is a precautionary prophylactic, you might as well use cling film, because it is full of holes.

    What if the required module calls the overridden method within its own BEGIN block?

    Once you start the arms race of every programmer putting the bits of their code they think are of paramount importance in BEGIN blocks, soon everybody will be putting everything in BEGIN blocks, and you're back to square one with no solution.

But the most interesting question here I think would be: why are you poking around in there anyway?

That is, what led you in there? What are you trying to do that you're failing to achieve? What error situation are you trying to avoid? What is it that you're really asking for help with?

If this is just an academic exercise of trying to improve your Perl by reading and understanding the internals of some examples, on the basis of what you've posted in isolation, you'd better pick some better examples.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy