Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: different exe path for different OS

by salva (Canon)
on Aug 31, 2010 at 06:48 UTC ( [id://858127] : note . print w/replies, xml ) Need Help??


in reply to different exe path for different OS

untested:
#!/usr/bin/perl -w BEGIN { my %perl = (linux => '/vobs/ims_tools/rhlinux/perl/bin/perl', sunos => '/vobs/ims_tools/sol/perl/bin/perl'); my $perl = $perl{lc $^O} or die "unsupported operative system $^O"; $perl eq $^X or exec $perl, '-w', $0, @ARGV; } # Your code here...

Replies are listed 'Best First'.
Re^2: different exe path for different OS
by bergbrains (Acolyte) on Aug 31, 2010 at 18:33 UTC
    Curiously, I'm wrestling with this very issue right now. This approach just isn't any good unless you can be sure that the Perl in the shebang line has all the same mods installed as the custom installs you're trying to execute. We have a Perl script that we require in a BEGIN block for each script we run, and we'd have to both make sure that the #!/usr/bin/perl has all of the mods that we're using in the script otherwise it won't compile, and we'd also have to evaluate the require once for the #!/usr/bin/perl and then again for the script running under the correct Perl.
      I am pretty sure that if you put the code from my previous post at the beginning of your script inside a BEGIN block and before any use statement, it would work even if you don't have all the modules required by the script installed for the default perl.

        You're correct, Salva. If you exec before you exit the BEGIN block, you're safe from the problems I noted regarding missing modules.

        #!/usr/bin/perl BEGIN {exec ("echo", "Blast it, Jim! I'm a doctor, not a magician!");} use Some::Bogus::Mod;

        ...does not throw the "Can't locate..." exception, whereas if you get beyond the BEGIN block, the use line will barf.

        So, we now have two viable options:

        1. There's Moritz's solution in which you replace your bang line with '#!/usr/bin/env perl' and make sure your PATH has the correct directory prepended to it; and
        2. And Salva's recommended method, based on a hash of paths to platform-specific Perl binaries.

        I looked at using $^O to get the key to identify the right Perl for the platform, but I've got two Linux installs -- one for 32-bit and one for 64-bit, and $^O only shows 'Linux'. I took a quick look at this: http://search.cpan.org/perldoc?perlport#PLATFORMS. There's probably something available in the Config module that would provide the granularity I'm looking for.

        I want to make sure that I'm able to derive the path to my executables from both Perl and shell scripts -- I'm now using `uname -p`.

        Thank you both for these solutions. Currently, our processes run in multiple environments (mainly DEV, STAGE and PROD), and we source a profile for each to pick up a number of variables that dictate various behaviors including directories and to which db we connect. I think we're going to go with Mortiz's env-on-the-bang-line solution, because it fits in better with our existing process.