in reply to Re: use depending on environment
in thread use depending on environment

This is the "correct" way to do this as use is a compile-time directive (is that the right term?). Perl will see the use inside the BEGIN { } or the eval { } and go search for that module.

Addmittedly, it is seems odd that

BEGIN { if ($condition) { use Foo; } }
does not do what you expect; but it makes more sense if you think of what it really does. Convert the above code into
BEGIN { if ($condition) { BEGIN { require Foo; Foo::import(); } } }

Then remember that if you have this:

print Bar::new(); print Bar::old(); package Bar; sub new { sub old { return "I'm old"; } return "I'm new"; } 1;
you have created a real subroutine called "old" in package Bar that can be called. The same is true with the use inside the BEGIN { }; you created a real BEGIN { } block that will be executed at compile time in the order in which it was encountered. You will actually execute that code regardless of the if { } block that surrounds it.

Ivan Heffner
Sr. Software Engineer, DAS Lead
WhitePages.com, Inc.

Replies are listed 'Best First'.
Re^3: use depending on environment
by tlm (Prior) on Jun 22, 2005 at 17:18 UTC

    Just a couple of things. According to the docs for use:

    [use] is exactly equivalent to
    BEGIN { require Module; import Module LIST; }
    except that Module must be a bareword.
    In other words, at least according to the docs, if one wants to replicate at runtime what use does, it should be Foo->import, not Foo::import.

    The second point is only a clarification of a potentially misleading detail. The routine Bar::old in your example can be called even if Bar::new has not been called first. In other words, it is not the case that executing Bar::new somehow defines Bar::old. (I'm not saying that you wrote or believe this, but someone reading your post may incorrectly draw this conclusion.)

    the lowliest monk

Re^3: use depending on environment - ok
by Andre_br (Pilgrim) on Jun 22, 2005 at 20:40 UTC
    WOW! GOT IT!

    Sorry, but we in fact have written lots of VALID code. The REAL problem was that I was relying on a sub called from other script to do the lc work. And, I didnīt know, it didnīt get the locale set at the main script!

    But now itīs great. Sorry to make you guys hit heads too! But at least now we have a huge record of "use depending on envirnoment"!

    Cheers

      For when there's someone months from now with the same problem, could you post what your final, working version was?

      (there were lots of versions that you've posted ... if you just want to go back and edit them, to mark which one did, and which ones didn't work, that'd be good, too... of course, it looked like they were all marked as not working)

Re^3: use depending on environment
by Andre_br (Pilgrim) on Jun 22, 2005 at 19:42 UTC
    This one also doesnīt work... I am beggining to think about suicide!
    BEGIN { ## ## use locale; ## ## eval "use POSIX ('locale_h')"; ## eval "setlocale( LC_ALL, 'pt_BR.iso88591' )"; ## ## } ##
    PS: no errors generated, just the incorrect default locale that remains set.
Re^3: use depending on environment
by Andre_br (Pilgrim) on Jun 22, 2005 at 19:50 UTC
    Sorry guys, this is getting a little bit confusing for me, I am not very familiar with this routines. Iīm testing the code below without successs:
    BEGIN { ## ## use locale; ## ## unless ( $ENV{REMOTE_ADDR} eq "127.0.0.1" ) { ## ## use POSIX ('locale_h'); ## setlocale( LC_ALL, "pt_BR.iso88591" ); ## ## } ## ## }
      Have you read tlm's suggestion?

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of