Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to use dynamic execution path instead of absolut.

by RuneK (Sexton)
on Jan 14, 2003 at 09:53 UTC ( [id://226773]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,
Usually when I create a perl script I add the absolut path to the perl binary in the top like this:

#!/usr/bin/perl

But lately I have been working with multible sites using VERY different path's for the perl binary and you can in no-way-what-so-ever get them to unify the path's. So my question is:

Is there a way to use the perl binary path as defined in the PATH variable in the perl script and thereby making the path in the script dynamic?

Thanks in advance,
Rune
  • Comment on How to use dynamic execution path instead of absolut.

Replies are listed 'Best First'.
Re: How to use dynamic execution path instead of absolut.
by Aristotle (Chancellor) on Jan 14, 2003 at 10:09 UTC
    If these are Unix machines, then you might have more luck with #!/usr/bin/env perl which will scan the systemwide PATH. "Might", because env has occasionally been known to reside in different directories as well..

    Makeshifts last the longest.

Re: How to use dynamic execution path instead of absolut.
by IlyaM (Parson) on Jan 14, 2003 at 10:40 UTC
Re: How to use dynamic execution path instead of absolut.
by busunsl (Vicar) on Jan 14, 2003 at 10:14 UTC
    On Unix system the following should work:
    #!/bin/sh `which perl` -x $0 $* exit #!perl print "hello perl\n";
    which finds (hopefully) your perl.
    The backticks execute the found perl.
    The -x switch forces perl to skip everything before #!perl
    $0 is the scriptname.
    $* are all commandline parameters.
      `which perl` -x $0 $*
      That's a useless use of backticks. which will only find something which is in the PATH, and then shell can find it as well, so you can just rephrase that as perl -x $0 $* That reminds me - perlrun has the following snippet:
      #!/bin/sh eval 'exec perl -S $0 ${1+"$@"}' if $running_under_some_shell; # regular perl code follows here
      If you really want, you can also put switches for Perl at the end of the shebang line if it looks like so: #!/bin/sh -- # -*- perl -*- -wT

      Makeshifts last the longest.

        That's a useless use of backticks. which will only find something which is in the PATH, and then shell can find it as well, so you can just rephrase that as

        perl -x $0 $*

        Arrgh! You're absolutly right!

      Besides the useless use of backticks pointed out in another subthread, this is also dangerous code:
      `which perl` -x $0 $*
      What if I invoked this script as:
      yourscript "randal l." schwartz
      I've just passed two parameters, but your reinvocation is going to give three parameters to the Perl program.

      Quoting matters. The example in perlrun is very carefully constructed to avoid quoting problems.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

Re: How to use dynamic execution path instead of absolut.
by mce (Curate) on Jan 14, 2003 at 10:12 UTC
    I am sure I have seen this question before.

    The classic way is to use:

    #!/usr/bin/env perl
    But this changes the question to: env is sometimes in /usr/bin or /bin, .... .
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://226773]
Approved by Tanalis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-18 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found