0

Can I write a Perl program where my first line is not #!/path/?

Thank you.

Axeman
  • 29,660
  • 2
  • 47
  • 102
infinitloop
  • 2,863
  • 7
  • 38
  • 55

4 Answers4

8

The shebang (#!)is only necessary if you want to invoke the script directly at a shell prompt, e.g. ./yourscript. You can always do perl yourscript and skip the shebang.

Marc B
  • 356,200
  • 43
  • 426
  • 500
6

If your concern is hard-coding a constant path (e.g. #!/usr/bin/perl as opposed to #!/usr/local/bin/perl), then use:

#!/usr/bin/env perl

This allows the Perl interpreter to be sought in your PATH, making your scripts a bit more portable (Windows aside).

JRFerguson
  • 7,426
  • 2
  • 32
  • 36
4

Yes, from perldoc perlrun (under the -S switch):

#!/bin/sh
eval 'exec /usr/bin/perl -wS $0 ${1+"$@"}'
    if $running_under_some_shell;

See that documentation for the complete story.

ErikR
  • 51,541
  • 9
  • 73
  • 124
  • nice! thanks, what is this ${1+"$@"} for? Is it some error evaluation? – infinitloop Nov 18 '11 at 21:13
  • 1
    It's equivalent to `"$@"` on modern implementations of the Bourne shell. Older implementations of `sh` need to use that more complex expression. – ErikR Nov 18 '11 at 21:37
  • If $1 is defined, then expand the parameter to "$@", which is a list of all the parameters, each quoted. If $1 is not defined then nothing is expanded. Was necessary under old shell implementations because just "$@" (with the quotes) would expand to a single argument of an empty string. See http://www.in-ulm.de/~mascheck/various/bourne_args/ – runrig Nov 18 '11 at 23:43
  • 1
    http://stackoverflow.com/questions/2308874/explain-the-deviousness-of-the-perl-preamble – daxim Nov 21 '11 at 13:02
0

If you do that then you'll need to invoke Perl explicitly. If that line is there, then the system knows that it is a Perl script so when you make the script executable you can just write ./script

perreal
  • 94,503
  • 21
  • 155
  • 181