in reply to use depending on environment

How about:

if ( $online == 1 ) { require POSIX; POSIX->import( 'locale_h' ); # blah blah }
?

the lowliest monk

Replies are listed 'Best First'.
Re^2: use depending on environment
by Codon (Friar) on Jun 22, 2005 at 16:32 UTC
    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.

      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

      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)

      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.
      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