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

Basically what the title says, anyone know how to use a perl script as the shebang line in a unix like system? For example: #!/usr/bin/myscript.pl ? There is an ugly work around that looks like
#!/usr/bin/perl /usr/bin/perl/myscript.pl but .. eh. Anyone know a better way?

Replies are listed 'Best First'.
Re: Perl script as the shebang?
by ambrus (Abbot) on Sep 18, 2004 at 11:43 UTC

    I think #!/usr/bin/perl /usr/bin/perl/myscript.pl is not ugly.

    The other way is to create a small C program that does nothing but execs perl to run that script. Than you can put the name of that binary in the shebang line.

    Yet another way is to use a bash or perl script like

    #!/usr/bin/perl -w *ARGV = *DATA{IO}; do "/usr/bin/perl/myscript.pl"; __DATA__

    or a shell script like this:

    #!/bin/sh perl /usr/bin/perl/myscript.pl "$0"; exit 0
    and make perl script the first lines, or just
    #!/bin/sh perl /usr/bin/perl/myscript <<ENDDATA; exit 0
    if you don't need to seek in the file.
Re: Perl script as the shebang?
by ikegami (Patriarch) on Sep 18, 2004 at 07:30 UTC

    The shebang says to which executable to feed the script, so for a perl script, it's simply:

    #!/usr/bin/perl

    You can add options if you like:

    #!/usr/bin/perl -w

    If you're trying to feed another script to your own script, you have to do #!/usr/bin/perl /usr/bin/perl/myscript.pl instead of #!/usr/bin/perl/myscript.pl because myscript.pl is not an executable.

Re: Perl script as the shebang?
by perrin (Chancellor) on Sep 18, 2004 at 22:23 UTC
Re: Perl script as the shebang?
by Aristotle (Chancellor) on Sep 18, 2004 at 20:59 UTC

    You can't invoke scripts directly from the shebang line, so you'll need that workaround. You can at least make it language agnostic though:

    #!/usr/bin/env /usr/bin/perl/myscript.pl

    Makeshifts last the longest.

Re: Perl script as the shebang?
by meredith (Friar) on Sep 18, 2004 at 20:38 UTC

    What about:

    #!/bin/bash /usr/bin/perl /usr/bin/myscript.pl <<END whatever you wanted to feed to your script blah foo bar END
    I think that would be clearer to more people looking at the file.

    Update: Oops! Didn't notice that this was already covered.

    mhoward - at - hattmoward.org
Re: Perl script as the shebang?
by rjbs (Pilgrim) on Sep 19, 2004 at 01:16 UTC
    While other suggestions are, in my opinion, better, you will find that zsh will allow you to put shebang-powered scripts in the shebang line. The kernel execve call will fail, but zsh will pick up the pieces.

    Just another reason that zsh is the bee's knees!
    rjbs

      But that will work only from within zsh. If you want to run such a script from somewhere else (like as a CGI script, or maybe during the init scripts which are usually executed by a more lightweight sh), you still need one of the other solutions.

      Makeshifts last the longest.