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?
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.
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.
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".
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With