in reply to How to instrust Perl to load the right modules based on architecture?

You may be able to use Config's $Config{archname64} value to determine which include path to use. The following (untested) code may get you started:

BEGIN { use Config; if ($Config{archname64}) { use LIB './lib64/perl5/5.8.5/x86_64-linux-thread-multi'; use LIB './lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-mul +ti'; } else { use LIB './lib64/perl5/5.8.5/i386-thread-multi'; use LIB './lib64/perl5/site_perl/5.8.5/i386-thread-multi'; } }

Perl reduces RSI - it saves typing

Replies are listed 'Best First'.
Re^2: How to instrust Perl to load the right modules based on architecture?
by ikegami (Patriarch) on Oct 06, 2008 at 05:12 UTC
    Aside from mispelling "lib", the order of execution is totally wrong. Your code is equivalent to:
    BEGIN { require Config; Config::->import(); require lib; lib::->import('./lib64/perl5/5.8.5/x86_64-linux-thread-multi'); require lib; lib::->import('./lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-m +ulti'); require lib; lib::->import('./lib64/perl5/5.8.5/i386-thread-multi'); require lib; lib::->import('./lib64/perl5/site_perl/5.8.5/i386-thread-multi'); if ($Config{archname64}) { # empty } else { # empty }

    Note that all four directories are added to @INC unconditionally because use statements are executed as soon as they are compiled. Fix:

    BEGIN { use Config; require lib; if ($Config{archname64}) { lib->import(qw( ./lib64/perl5/5.8.5/x86_64-linux-thread-multi ./lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi )); } else { lib->import(qw( ./lib64/perl5/5.8.5/i386-thread-multi ./lib64/perl5/site_perl/5.8.5/i386-thread-multi )); } }

    Update: Markup changes.

      Thanks, it works now.
      But I still a little confused with the "use lib" and the BEGIN block, which one is executed first, the "use lib" or the BEGIN block?
      And, I find that the BEGIN block's location makes difference. If I put it ahead of the script like this:
      use strict; use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use lib "$FindBin::Bin/lib/perl5/site_perl/5.8.5"; BEGIN { use Config; require lib; print "archname:$Config{archname}\n"; if ($Config{archname} =~ /^x86_64/) { print "x86_64\n"; lib->import( "$FindBin::Bin/lib64/perl5/5.8.5/x86_64-linux-thread-multi", "$FindBin::Bin/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread +-multi"); } else { print "i386\n"; lib->import( "$FindBin::Bin/lib/perl5/5.8.5/i386-linux-thread-multi", "$FindBin::Bin/lib/perl5/site_perl/5.8.5/i386-linux-thread-mul +ti"); } } use utf8; use threads; use Carp; use IO::Socket::SSL; use IO::Select; use Log::Log4perl;
      My script works well, but if I put the BEGIN block at the tail like this:
      use strict; use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use lib "$FindBin::Bin/lib/perl5/site_perl/5.8.5"; use utf8; use threads; use Carp; use IO::Socket::SSL; use IO::Select; use Log::Log4perl; BEGIN { use Config; require lib; print "archname:$Config{archname}\n"; if ($Config{archname} =~ /^x86_64/) { print "x86_64\n"; lib->import( "$FindBin::Bin/lib64/perl5/5.8.5/x86_64-linux-thread-multi", "$FindBin::Bin/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread +-multi"); } else { print "i386\n"; lib->import( "$FindBin::Bin/lib/perl5/5.8.5/i386-linux-thread-multi", "$FindBin::Bin/lib/perl5/site_perl/5.8.5/i386-linux-thread-mul +ti"); } }
      It throw the error "Can't locate Net/SSLeay.pm in @INC", and I can't find the right path in @INC.
      Where the BEGIN block exactly should be? The very beginning of the script, even before "use strict; use warnning"?

      I also change the $Config{archname64} to $Config{archname} and, use $Config{archname} =~ /^x86_64/ to decide whether It is a x86_64 host. Because I find that $Config{archname64} is empty(although existed) in both i386 and x86_64 Perl.

        But I still a little confused with the "use lib" and the BEGIN block, which one is executed first, the "use lib" or the BEGIN block?

        BEGIN blocks are executed as soon as they are fully compiled, i.e. when the } is reached.
        use statements are executed as soon as they are fully compiled, i.e. when the ; is reached.
        In other words, whichever ends first gets executed first.

        Where the BEGIN block exactly should be? The very beginning of the script, even before "use strict; use warnning"?

        Before useing any modules that require the changed @INC. If it finds utf8, strict, warnings with the default @INC, I'd load those first. That's just my personal preference.