in reply to BEGIN block question

sorry. the above code actually works. it failed when i put the use MyApp::Conf qw/APP_NAME/; inside the BEGIN block.

Replies are listed 'Best First'.
Re^2: BEGIN block question
by kyle (Abbot) on Jun 05, 2007 at 21:55 UTC

    The reason it fails inside the BEGIN block is that the use of MyApp::Conf happens while the BEGIN block is being compiled, but the assignment to %ENV happens after the BEGIN block is compiled (i.e., at the BEGIN block's "run time"). If you put it after the BEGIN block, then the compilation (use) of MyApp::Conf happens after the BEGIN block has executed (and the assignment to %ENV has happened).

      thank you. that's very clear. i totally forgot that assignment happens at runtime and use happens at compiled time. hence the failure in the BEGIN block.
Re^2: BEGIN block question
by merlyn (Sage) on Jun 05, 2007 at 21:54 UTC
Re^2: BEGIN block question
by ikegami (Patriarch) on Jun 05, 2007 at 23:42 UTC

    This came up twice very recently. The problem is that the use statements in the BEGIN block finish compiling before the BEGIN block does (seeing as the BEGIN block finishes later in the sources), so they get executed first.

    In short, you want

    use FindBin qw($RealBin); use lib "$RealBin/lib/"; BEGIN { $ENV{APP_ROOT} = $RealBin; } use MyApp::Conf qw/APP_NAME/;

    Previous thread #1
    Previous thread #2