Confusion in perlrun documentation

Discussion in 'Perl Misc' started by Krishna Chaitanya, Mar 20, 2009.

  1. 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...
     
    Krishna Chaitanya, Mar 20, 2009
    #1
    1. Advertising

  2. Krishna Chaitanya

    Peter Scott Guest

    On Fri, 20 Mar 2009 04:49:20 -0700, Krishna Chaitanya wrote:

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

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
    http://www.informit.com/store/product.aspx?isbn=0137001274
     
    Peter Scott, Mar 20, 2009
    #2
    1. Advertising

  3. 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?
     
    Krishna Chaitanya, Mar 20, 2009
    #3
  4. On 2009-03-20, Peter Scott <> wrote:
    > On Fri, 20 Mar 2009 04:49:20 -0700, Krishna Chaitanya wrote:
    >
    >> 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.


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

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
     
    Eric Pozharski, Mar 20, 2009
    #4
  5. >
    > And here we have that fabulous I<-w> story again.  Or not.
    >


    Excuse me, I didn't get it. What is the story about? :)
     
    Krishna Chaitanya, Mar 21, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. martin f. krafft

    confusion when resetting registers

    martin f. krafft, Aug 18, 2004, in forum: VHDL
    Replies:
    2
    Views:
    488
    Paul Sereno
    Aug 19, 2004
  2. Cameron Laird
    Replies:
    1
    Views:
    691
    Josiah Carlson
    Apr 3, 2004
  3. Kenneth McDonald
    Replies:
    2
    Views:
    789
  4. Replies:
    1
    Views:
    1,263
    Andy Dingley
    Sep 16, 2006
  5. Brian Wakem

    Prototype mismatch under ModPerl::PerlRun

    Brian Wakem, Sep 6, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    446
    Brian Wakem
    Sep 6, 2005
Loading...

Share This Page