Confusion in perlrun documentation

K

Krishna Chaitanya

Hi, I've been trying out a2p and found these lines at the top of the
perl output:

#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;

Googled around for this and found several links to perlrun that
attempt to explain what this idiom does exactly but I am confused
about the part where the shell exec's /usr/bin/perl with the script
name and its arguments and then .... "ignores the lines parsed from
the script because the $running_under_some_shell variable is always
false"....

Where is the glue between parsing lines and ignoring them due to this
undefined variable? How is this happening exactly? Could anyone please
explain in a simpler language what is happening?

Thanks...
 
P

Peter Scott

Hi, I've been trying out a2p and found these lines at the top of the
perl output:

#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;

Googled around for this and found several links to perlrun that attempt
to explain what this idiom does exactly but I am confused about the part
where the shell exec's /usr/bin/perl with the script name and its
arguments and then .... "ignores the lines parsed from the script
because the $running_under_some_shell variable is always false"....

Where is the glue between parsing lines and ignoring them due to this
undefined variable? How is this happening exactly? Could anyone please
explain in a simpler language what is happening?

This construct is meant for ancient systems that don't know what #!
means and pass every script to /bin/sh. (Anyone know whether any such
systems are still in use?)

The lines are written so that they do the right thing in both /bin/sh
and perl. When /bin/sh reads the first line, it interprets it as
meaning, "run perl on this file and pass it the arguments I was given",
which is what you want. When perl runs the file - whether because it
was started on it originally due to being on a nonbroken system or whether
because it was started by the eval line - it reads the eval line but being
perl, then reads the next line to see the rest of the statement, and
ignores it because $running_under_some_shell was never set and is
therefore undef, hence false. Then it goes on with the rest of the
program.

HTH.
 
K

Krishna Chaitanya

So...if I get it correctly:

In case of an antique system, /bin/sh executes Perl interpreter
through 'exec' which replaces the /bin/sh process (being an 'exec')
and becomes a Perl program instead. In the Perl program now, it looks
at the eval statement again with the statement modifier having
$running_under_some_shell which is undef (therefore false) and skips
the eval. And continues running the rest of the Perl code in $0?
 
E

Eric Pozharski

This construct is meant for ancient systems that don't know what #!
means and pass every script to /bin/sh. (Anyone know whether any such
systems are still in use?)

The lines are written so that they do the right thing in both /bin/sh
and perl. When /bin/sh reads the first line, it interprets it as
meaning, "run perl on this file and pass it the arguments I was given",
which is what you want. When perl runs the file - whether because it
was started on it originally due to being on a nonbroken system or whether
because it was started by the eval line - it reads the eval line but being
perl, then reads the next line to see the rest of the statement, and
ignores it because $running_under_some_shell was never set and is
therefore undef, hence false. Then it goes on with the rest of the
program.

And here we have that fabulous I<-w> story again. Or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top