Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there ever a reason to include the shebang line in Perl packages?

Tags:

module

perl

I realize that I should include the shebang line only in scripts we want to run directly in the shell. I also do not see the shebang line included in any of the big module packages I have installed locally.

However, I was still curious if people might include it for some other reason I'm not yet aware of. Or if there perhaps might be something like a (e.g.) ~'historical' reason for including it. Or should the shebang line never ever be included in .pm files(period) for fear of my house exploding?

like image 519
CODEBLACK Avatar asked Sep 24 '12 12:09

CODEBLACK


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.

Why is the shebang line important?

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.

What is #! In Perl?

The "#!" is a shell scripting command used to say that the file should be considered an executable file and that it should be executed using the specified interpreter. Typically, the Perl interpreter will be located in either "/usr/local/bin/" or "/usr/bin".


2 Answers

No. A shebang marks a file as executable, and a module is usually not executable.

In general, there are exceptions. Python has an idiom to detect whether the current file is the one invoked by the interpreter, and quite some modules use this to run their unit tests when invoked standalone. This is not common in the Perl community, where you have extra unit test files bundled with the module on CPAN.

So even with this possible exception in mind, I would not consider it good style to include a shebang in a module file which is not meant to be executed directly.

like image 85
Stefan Majewsky Avatar answered Oct 06 '22 00:10

Stefan Majewsky


The short answer is NO, but there is no right or wrong answer.

The purpose of the shebang is to let the OS know where the interpreter is. There is no requirement to add it to any Perl file (script or module). And under Windows it is ignored anyway**.

More specifically, in most cases there is no reason to add it to a module, unless there is some reason why you would want to execute that module directly. A Perl module is really just a Perl script anyway, with a 'package' command at the top.

Either way, it won't hurt if it is there, but in most cases it doesn't make sense.

** Regardless of how Perl is started it will always look at the shebang for command line switches. This even goes for Windows, where the OS doesn't look for the shebang line, but when Perl scans the file it will. See http://perldoc.perl.org/perlrun.html.

like image 45
Robert Hanson Avatar answered Oct 05 '22 23:10

Robert Hanson