Operating System - HP-UX
1837895 Members
3587 Online
110122 Solutions
New Discussion

Re: Script detect location of perl

 
hpuxrox
Respected Contributor

Script detect location of perl

How would one write a script that will run on multiple system but have perl is different locations. I start the script with,
#!/usr/bin/perl -w
However, some boxes have perl in /opt/perl/bin and these error. Any ideas?
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: Script detect location of perl

Generally the least evil way to do this is to keep your "shbang" line at "#!/usr/bin/perl" as /usr/bin is a universally available directory on a UNIX box. You then create a symbolic link from /opt/perl/bin/perl (or whatever) to /usr/bin/perl. You essentially have to do this at most one-time on a box and then you are done. You can even leave the shbang line unchanged on a Windows box and it will still work (in that case, it will be a comment) because Windows allows the file suffix (.pl) association with an executable (perl.exe). Thus if you name your scripts xxx.pl and use #!/usr/bin/perl as the shebang line, you can generally easily make your code portable. I generally have a small shell script that will create the symbolic link if it's not there.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Script detect location of perl

Hi:

Think UNIX. Create a symbolic link ---'/usr/bin/perl'.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Script detect location of perl

Hi (again):

Another trick is to do:

#!/usr/bin/env perl

This will cause 'env' to search your PATH variable.

By the way, don't use the '-w' switch for warnings unless you are writing a commandline script. Using 'use warnings' provides much more robust warning detection.

Regards!

...JRF...
VK2COT
Honored Contributor

Re: Script detect location of perl

Hello,

Our colleagues already told you about env(1) command as interpreter in Perl scripts.

I use env(1) method for Perl scripts in
on Linux and HP-UX servers and the following one
on Solaris:

#!/bin/sh -- # Really perl
eval 'exec perl -S $0 ${1+"$@"} 2>/dev/null'
if 0;

Cheers,

VK2COT
VK2COT - Dusan Baljevic
Robert Fritz
Regular Advisor

Re: Script detect location of perl

One other thing I've done is forgo the #!, and create a wrapper (sh) script, that detects OS versions and then calls the script with the correct Perl. That helps when you don't own the destination system, and the destinations systems vary widely (say HP-UX, to Linux, to OSX)
Those Who Would Sacrifice Liberty for Security Deserve Neither." - Benjamin Franklin