Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I use for a Perl script's shebang line?

Tags:

shebang

perl

Which of these is better or faster to use as the shebang line for a Perl script?

#! perl  #! perl.exe  #! fullpath/perl(/perl.exe)  #! partialpath/perl(/perl.exe) 

And, when using #!perl, when it works on a particular system, how do I find out in the script which perl interpreter I'm using so I can put that one into the shebang line?


And, if using a /path/path/perl, are * or ... allowed to be used for the folders?

like image 978
Anonymous Avatar asked May 07 '10 22:05

Anonymous


People also ask

What is the use of shebang line in Perl?

This line instructs the operating system running the Perl script on where the executable for Perl and its associated files are located. This line is commonly only required in Linux and Unix variants, users running Perl in Microsoft Windows do not need this line.

When this line will be used in perl #!/ Usr bin perl?

It's called a shebang. On Unix based systems (OSX, Linux, etc...) that line indicates the path to the language interpreter when the script is run from the command line. In the case of perl /usr/bin/perl is the path to the perl interpreter.

What is correct for shebang statement?

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark ( #!) at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.

Does shebang have to be first line?

The shebang must be the first line because it is interpreted by the kernel, which looks at the two bytes at the start of an executable file. If these are #! the rest of the line is interpreted as the executable to run and with the script file available to that program.


1 Answers

If you have to hard code #!, use #!/usr/bin/env perl. Why? What you want is for the Perl program to run with the user's preferred Perl. That's going to be the first on in their PATH. #!perl doesn't do what I mean, it doesn't search the user's PATH, #!/usr/bin/env perl is how you pull that off. /usr/bin/env will always be there on Unix systems.

If the user is using Windows, as others have pointed out, it doesn't matter. Windows doesn't use #! it uses file extension associations. Make sure your program is called foo.pl or something and it'll work. But include the #! line anyway as some utilities and editors make use of it.

If you're shipping code, let the installer take care of it. Both MakeMaker/Makefile.PL and Module::Build/Build.PL will change your #! line to match the perl the user used to install with. They will take care of this problem for you.

If you are installing code for your own production use, you should use the full path to a particular copy of perl. Which copy of perl? One specific to your project. Does this mean you need to compile perl for every project? No, you can make a symlink. Project foo might have /usr/local/bin/fooperl point at /usr/bin/perl5.18. Use #!/usr/local/bin/fooperl. Now if you decide to upgrade perl you can do it per project by changing the symlink.

like image 130
Schwern Avatar answered Sep 18 '22 14:09

Schwern