bharatbsharma has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I want

#!/vobs/ims_tools/sol/perl/bin/perl w
path to be selected for Solaris machine and
#!/vobs/ims_tools/rhlinux/perl/bin/perl -w
path to be selected for Linux machine.

Is it possible without any wrapper script?

Replies are listed 'Best First'.
Re: different exe path for different OS
by ikegami (Patriarch) on Aug 31, 2010 at 05:41 UTC
    Specify the path to a symlink that exists on both machines.
Re: different exe path for different OS
by salva (Canon) on Aug 31, 2010 at 06:48 UTC
    #!/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...
      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.
Re: different exe path for different OS
by Anonymous Monk on Aug 31, 2010 at 06:46 UTC
        No, actually I didn't :) I find installing a program much simpler than mucking about with shebang
Re: different exe path for different OS
by djp (Hermit) on Sep 01, 2010 at 03:20 UTC
Re: different exe path for different OS
by DrHyde (Prior) on Sep 01, 2010 at 07:43 UTC
    Given that you're using a package management system (because to not do so would be crazy!) then can't whatever you use to create the packages for the two platforms re-write the #! line for you?
      At least in our case, our scripts are shared via NFS across multiple platforms, so that doesn't make sense, because the requirement is that the exact same script must execute with the platform-specific interpreter when run on different platforms.

        In that case, Brother Salva's answer is the one you want. In this case, using $^X will work, but in general it's good practice to use Config; and then use $Config{perlpath} instead. $^X can sometimes get confused if your original script is invoked with a relative path to perl (eg as ../myperl/bin/perl and then does a chdir before you try to execute $^X, and on some more exotic platforms.

        Strictly speaking, you should also pay attention to $Config{exe_ext} as well, sticking it on the end of $Config{perlpath} if it's not already there, but given that you're only on Unix you don't need to care about that.