in reply to Re: Strange issues with mod_perl and use lib
in thread Strange issues with mod_perl and use lib

That doesn't fix the problem. The assignment is done earlier than it would have without the BEGIN, but it still happens after the use statement is executed.

While the BEGIN block is executed as soon as it finishes compiling, the same goes for the use statement. Since the use statement is fully compiled before the BEGIN block is fully compiled (i.e. it ends first), the use statement is executed before the BEGIN block is executed, and therefore before the assignment is executed.

Solutions:

my $path; BEGIN { # Code that determines the path and stores it in $path. $path = "/www/domain/cgi-bin"; } use lib $path;

or

use lib do { # Code that determines the path and returns it. "/www/domain/cgi-bin" };

or

sub get_lib_path { # Code that determines the path and returns it. return "/www/domain/cgi-bin"; } use lib get_lib_path();

or

# Hardcoded use lib "/www/domain/cgi-bin";

Update: Clarified the wording.

Replies are listed 'Best First'.
Re^3: Strange issues with mod_perl and use lib
by Joost (Canon) on May 17, 2007 at 22:08 UTC

      It's really not unintuitive. It's just like nested for loops. It's just like recursion. The most nested item always completes first.

      Nested BEGIN blocks and use statements are only executed in reverse order if you look at the start of the statement. They're executed in-order if you look at the whole statement or the end of the statement. How can you execute something before it's compiled?