On Sat, Jun 21, 2008 at 06:19:46PM +0100, Rob Dixon wrote: > gowthamgowtham wrote: > > > > I found this in Programming Perl - 3rd edition. Could not understand how > > this works. > > What is it supposed to do? Does it work? Have you tried it? How is it supposed > to be run?
It's a clever little bit of code that is at once both valid perl and shell. If the program is run by the shell it will be execed as perl. If it is run by perl then it will fall though to the real program. As noted in the topic, it is used on systems that don't support shebang lines and is rarely required nowadays. > I've had a quick look through the book and can find nothing like it. It is > either very esoteric or simply wrong > > > #!/bin/sh -- # perl, to stop looping If you have your default shell set to perl, but there is no mention of perl on the shebang line then perl will exec the program with the shell specified on the shebang line. Again, this is rarely required nowadays. > Is this your comment or does it come from the book? If you have changed this > line from > > #!/usr/bin/perl > > then it will fail as what follows is Perl and nothing else will run it. The key is that the second line is also valid shell. > > eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' > > > > if 0; > > This is the same as > > eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; ... in perl, but not in shell. > Even if perl gets to see it, the eval won't happen because of the 'if 0' > statement modifier. In addition: This is important for when it is run as perl. > $@ is a string value. (The error message from the previous call to eval). It > is > odd to enclose it in quotes and even odder to add one to it an use the result > as > a scalar variable name. $@ is only relevant for shell. As you note, it will never be evaluated by perl. > > Questions: > > > > - Who (I mean which program/shell) runs eval 'exec .'? > > Perl. But, as I wrote above, I can't see how perl will ever get either to see > the statement (because of the #! line) or execute it (because of the 'if 0' > modifier). So the real answer is the shell. > > - What is ${1+"$@"}, looks like this makes up the command line > > I explained that before as well. $0 is the command and ${1+"$@"} is its > parameter. For the shell, this evaluates to the program and its arguments. > > arguments to the script? > > > > - What is the purpose of 'if 0;' on a new line? > > The newline has no purpose in Perl apart from layout. ... but it's on a new line to stop the shell from parsing it. > > 'perldoc perlrun' documents that -S switch makes perl search $PATH for the > > script. > > It doesn't say that, it says it searches the PATH environment variable. There > is > no Perl $PATH variable and when you are posting to a Perl group it is > important > to make the distinction. I notice now that it also does a pretty good job of explaining the whole thing, doesn't it? > This is either black magic, using shell tricks that I don't know about, or it > is > simply wrong. I suspect that the second is true, the third is false and the first is a matter of opinion. In any case, the construct is mainly of historical interest now. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/