in reply to Strange issues with mod_perl and use lib

but the following does NOT work:

my $path = "/www/domain/cgi-bin"; use lib $path;

use() is executed at compile time, while assignment is executed at runtime. That means that "use $path;" is run when $path is still undefined.

Try this:

BEGIN { my $path = "/www/domain/cgi-bin"; use lib $path; }
See use.

Replies are listed 'Best First'.
Re^2: Strange issues with mod_perl and use lib
by ikegami (Patriarch) on May 13, 2007 at 01:44 UTC

    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.

        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?

Re^2: Strange issues with mod_perl and use lib
by Farenji (Novice) on May 12, 2007 at 23:38 UTC
    Unfortunately, that doesn't work either...