Dear monks, I need some guidance on the special code blocks BEGIN/END/INIT/et al, especially INIT.

1. Does Perl provide a simple way to find out in which phase a code is running in, or do I have to resort to some trickery like analyzing special vars (because IIANM perlvar mentions that some vars are only available at compile-time), or inserting our own BEGIN/END/INIT blocks to set some flags?

2. Is it possible to insert an INIT block conditionally? That is, insert it if it's not too late, otherwise don't bother?

The situation I'm facing ATM: I have a module (M1) which is usually loaded via use() (thus, compile-time). This module inserts an INIT block to run its init(). The reasons for this: 1) to let other modules load first and possibly alter M1's configuration before init() is run; 2) to run init() automagically without user needing to call it, thus giving convenience; 3) to avoid running init() under 'perl -c'.

However, for a modular command-line application that I'm writing right now, where to reduce startup overhead I load selected modules depending on which subcommand is given on the command line, M1 is now loaded via require() in the runtime phase. This results in "Too late for INIT block" error message. To shut up this error message, I can instead run everything in the BEGIN block (e.g. by wrapping BEGIN { ... } for the whole application) but then M1's INIT block will never be called in time anyway. And upon error Perl also emits an annoying extra message: "BEGIN failed--compilation aborted" since every error is a compile-time error now.

So my choice now is to remove M1's INIT block, thus requiring M1's user to init() manually. Sadly this breaks all the other use cases. But suppose M1 can insert INIT block conditionally, that'd be much nicer. I'm open to other suggestions.


In reply to More about the INIT code block by dgaramond2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.