The stupid question is the question not asked | |
PerlMonks |
Re: Running Under Some Shellby rubasov (Friar) |
on Feb 24, 2010 at 22:51 UTC ( [id://825214]=note: print w/replies, xml ) | Need Help?? |
These "gubbins" are needed only when you encounter a platform which does not handle shebang lines (#!...). Those platforms generally will pass anything to the sh interpreter what does not look like a binary executable file (== does not have the format of an a.out or ELF ...). Therefore if you're sure you or your users won't execute those programs on such platforms then you can replace them safely with the simple shebang.
Let's investigate this (the first case coming from perlrun):
This code will be passed first to sh. sh will interpret the shebang line as a comment, then will look at the second line as a command terminated by the newline, so the if line is not relevant in this case. If only sh would interpret this code, then the eval would not be needed and this would suffice:
However later this line will be seen by perl also, that's why we should find a construct that is syntactically valid both in sh and in perl. The eval '...' construct fulfills this. At this point sh is interpreting the eval line, not looking ahead what will follow in the source file, and never will encounter the if line because it is replacing itself with a perl process by using exec. perl is called with two options (-w and -S), you already know the meaning of those. Then in the perl command line follows the name of the actual script ($0) and all the command line arguments passed to this script (${1+"$@"}). "$@" expands to the positional parameters in sh terminology, so it is equivalent to "$1" "$2" "$3" ... The ${parameter:+word} syntax is explained in the man page of bash (in the Parameter Expansion section): ${parameter:+word}And omitting the colon from the construct above is also explained there: ... bash tests for a parameter that is unset or null; omitting the colon results in a test only for a parameter that is unset As I see ${1+"$@"} can be simplified to "$@". The only difference is that in the first case only $1 will be checked whether it is set. Okay, if the exec succeeded then now we have a running perl processing the same file getting the same arguments. The shebang line is a comment for perl also, but the newline is not a statement separator in perl, so the first statemant is eval '...' if $running_under_some_shell;. As this variable will be always false, this is semantically a no-op, so perl will happily skip it, and execute the rest of the file. If you write eval '...' if 0; it has the same meaning, but you won't get the warning $running_under_some_shell used only once. I'm not really knowledgeable in the c shell, so I can't explain now the gubbin designed for sh and csh at the same time. I hope this helps understanding what is going on.
In Section
Seekers of Perl Wisdom
|
|