Re: Launching perl scripts on systems which do not support #!



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?

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

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.

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;

Even if perl gets to see it, the eval won't happen because of the 'if 0'
statement modifier. In addition:

$@ 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.




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).

- 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.

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.

'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.

This is either black magic, using shell tricks that I don't know about, or it is
simply wrong.

Please tell where in Programming Perl you have read this.

Rob
.