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

Monks, I have a slight problem that I think should be easy to overcome. I have the need to run my scripts with #!perl instead of #!/usr/bin/perl. Problem is that even though I have perl in my path I always get a file not found error. Am I missing something? Collins

Replies are listed 'Best First'.
Re: #!perl Question
by ikegami (Patriarch) on Oct 03, 2005 at 18:12 UTC

    You need to specifyt the full path to perl, since $ENV{PATH} isn't used in this circumstance. You could use #!/usr/bin/env perl to search using $ENV{PATH}. (Note: I hear that path to env (/usr/bin) is not the same on all systems, rendering it somewhat useless.)

    Update: Clarified to avoid confusion of between a file's path and the environment variable.

      Then we just need a program called "envfinder" to find the path to "env":

      #!/usr/bin/envfinder env perl

      And if you want to make sure that it works even in systems that put envfinder in nonstandard locations:

      #!/usr/bin/envfinderfinder envfinder env perl

      That's what I use and it has worked on Red Hat and Debian systems with no trouble.
      Note: I hear that env's path is not the same on all systems, rendering it somewhat useless.
      From what I understand, env uses your own environment, so if your $PATH is different between systems, env's notion of it will be different, too.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        I was refering to the path to env (/usr/bin), not the environment variable.
Re: #!perl Question
by philcrow (Priest) on Oct 03, 2005 at 18:10 UTC
    On Unix the kernel handles the #! line. It expects to see the path to an executable file there. Unless you have one called 'perl' in the current directory, it will complain (on my system: perl: bad interpreter: No such file or directory).

    You can do this:

    perl your_script_name
    But that probably defeats the purpose. So, what were you trying to do? Why did you need to leave the path unspecified?

    Phil

Re: #!perl Question
by ickyb0d (Monk) on Oct 03, 2005 at 18:09 UTC
    it should normally be... #! /path/to/perl

    this tells the system where to find perl, whether it's in your path or not. but windows should use the PATH environment variable... so make sure it's in your path in windows (which can sometimes be weird) if you're using windows.

    but if you're using apache for some CGI or whatever, then i believe that you can use just use: #! perl so maybe apache isn't setup correctly if you're going that route.
      No windows , apache, etc. just a clean Linux AS3 build. So, you are saying the unix will not pick up perl from the $PATH?
Re: #!perl Question
by QM (Parson) on Oct 03, 2005 at 19:44 UTC
    Not sure if this answers your question, but the following will find Perl in your path, wherever it is (as long as it's not Windows). (I'm not sure who the original author was.)
    eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' && eval 'exec perl -w -S $0 $argv:q' if 0; # The above invocation finds perl in the path, wherever it may be #(as + long as it's not Windows) # # For Windows, use something like: # # ASSOC .pl=PerlScript # FTYPE PerlScript=perl.exe %1 %* #
    I haven't come across a completely generic shebang that works on both Windows and *nix.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      Interesting, but I have to know where perl is to run eval...... and that defeates the purpose.
        I have to know where perl is to run eval...... and that defeates the purpose.
        Yes and no. If you don't know, but the system does, this works. Which leads to:

        1) The sysadmin should know where Perl is on your system.
        2) If you're the sysadmin, then you know where Perl is on your system.

        (1) can fail because the sysadmin doesn't know (or has some other problem I won't go into). (2) can fail for similar reasons -- only the perspective changes.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Re: #!perl Question
by rinceWind (Monsignor) on Oct 04, 2005 at 15:49 UTC

    As others have pointed out, having #!perl will not work as Unix systems need the full path to the executable.

    There is a way to achieve this, using part of the mechanism that is ExtUtils::MakeMaker, uses to handle scripts (exe_files):

    perl -MExtUtils::MY -e 'MY->fixin(@ARGV)' *.pl

    See my use.perl journal on the subject.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)