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


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.


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

- 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

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.


Relevant Pages

  • Re: Problems passing a reference to a hash between functions
    ... M> But as noticed Paul Lalli, we're programming Perl;) ... M> While shorter code is more readable and powerful:) ...
  • Re: reg exp
    ... yes as I read in programming perl as well lead me to this solution. ... and finally found this hint in perldoc perlop: ...
  • Re: Question on download by LWP
    ... Programming Perl 3rd ed, p605 ... "don't use camelcase with Perl" to me over-steps the mark. ... I just passed on the advice given in Programming Perl;-) ... perldoc perlstyle. ...
  • Re: Oreillys "Learning Perl 3rd Edition"
    ... Afterwards I plan on moving upward and onward in perl. ... Oreilly's "Programming Perl" or "Perl Cookbook" would be good to jump ... The old soul mate, which I ...
  • Re: Why Perls representative is a camel?
    ... O'Reilly media, publishers of Programming Perl (as well as Learning, ... have a practice of placing animal drawings on the cover of the ... The Camel is actually a trademark of O'Reilly, ...