-I switches on shebang line

Discussion in 'Perl Misc' started by xhoster@gmail.com, Sep 12, 2008.

  1. Guest

    I'm sure something in perldoc perlrun is relevant to my question, but
    I can't figure out what. Please give me a kick in the pants.

    $ cat foo.pl
    #!/home/xho/perl_10/bin/perl -Ifoo -Ibar -l
    print foreach @INC;
    __END__

    $ ./foo.pl
    foo -Ibar -l
    /home/xho/perl_10/lib/5.10.0/x86_64-linux-thread-multi
    /home/xho/perl_10/lib/5.10.0
    /home/xho/perl_10/lib/site_perl/5.10.0/x86_64-linux-thread-multi
    /home/xho/perl_10/lib/site_perl/5.10.0
    ..
    foo
    bar

    So, why are foo and bar put at the end of @INC, instead of at the beginning
    where perlrun says they should?

    And why is "foo -Ibar -l" put at the front of @INC, as if all of the
    shebang line after the first word were put in quotes?

    This behavior doesn't seem specific to 5.10.0, it is seen in various 5.8
    as well. If invoked like "perl foo.pl" then the foo and bar still
    go at the end, rather than beginning, but the weird first entry is omitted.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Sep 12, 2008
    #1
    1. Advertising

  2. Ben Morrow Guest

    Quoth :
    > I'm sure something in perldoc perlrun is relevant to my question, but
    > I can't figure out what. Please give me a kick in the pants.
    >
    > $ cat foo.pl
    > #!/home/xho/perl_10/bin/perl -Ifoo -Ibar -l
    > print foreach @INC;
    > __END__
    >
    > $ ./foo.pl
    > foo -Ibar -l
    > /home/xho/perl_10/lib/5.10.0/x86_64-linux-thread-multi
    > /home/xho/perl_10/lib/5.10.0
    > /home/xho/perl_10/lib/site_perl/5.10.0/x86_64-linux-thread-multi
    > /home/xho/perl_10/lib/site_perl/5.10.0
    > .
    > foo
    > bar
    >
    > So, why are foo and bar put at the end of @INC, instead of at the beginning
    > where perlrun says they should?
    >
    > And why is "foo -Ibar -l" put at the front of @INC, as if all of the
    > shebang line after the first word were put in quotes?
    >
    > This behavior doesn't seem specific to 5.10.0, it is seen in various 5.8
    > as well. If invoked like "perl foo.pl" then the foo and bar still
    > go at the end, rather than beginning, but the weird first entry is omitted.


    This is all to do with the way #! lines are parsed by the OS, and by
    perl. The OS treats everything after the first space as a single
    argument (to avoid needing to parse quoting in the kernel), and
    effectively invokes perl as

    /home/xho/perl_10/bin/perl "-Ifoo -Ibar -l" foo.pl

    perl prepends "foo -Ibar -l" to @INC, as documented, then constructs its
    default @INC. Then it finds the #! line itself, parses it for more
    options, and any -I found then go at the *end* of @INC. I don't know if
    this is documented anywhere, but it happens at least as far back as 5.6.

    In simple terms: #! parsing is highly dodgy, and perl's efforts to make
    it less so don't always help. I would 'use lib', instead.

    Ben

    --
    I touch the fire and it freezes me, []
    I look into it and it's black.
    Why can't I feel? My skin should crack and peel---
    I want the fire back... BtVS, 'Once More With Feeling'
     
    Ben Morrow, Sep 12, 2008
    #2
    1. Advertising

  3. Ben Morrow <> wrote:
    > Quoth :

    *SKIP*
    >> This behavior doesn't seem specific to 5.10.0, it is seen in various
    >> 5.8 as well. If invoked like "perl foo.pl" then the foo and bar
    >> still go at the end, rather than beginning, but the weird first entry
    >> is omitted.

    > This is all to do with the way #! lines are parsed by the OS, and by
    > perl. The OS treats everything after the first space as a single
    > argument (to avoid needing to parse quoting in the kernel), and
    > effectively invokes perl as


    > /home/xho/perl_10/bin/perl "-Ifoo -Ibar -l" foo.pl


    [a bit of explanation for unwary reader] Please note that Ben talks
    about OS. When at command line you type C<perl -Ifoo -Ibar whatever>
    that's shell that parses. In case C<./foo.pl> after some usual
    maintanance shell forks-execs and then that's the kernel that looks what
    would eat that text. The rest you know.

    > perl prepends "foo -Ibar -l" to @INC, as documented, then constructs its
    > default @INC. Then it finds the #! line itself, parses it for more
    > options, and any -I found then go at the *end* of @INC. I don't know if
    > this is documented anywhere, but it happens at least as far back as 5.6.


    > In simple terms: #! parsing is highly dodgy, and perl's efforts to make
    > it less so don't always help. I would 'use lib', instead.


    BTW, it should be a (the?) problem for any interpreter. Thanks for
    explanation. Personally I'll be very careful tinkering with shebang.

    --
    Torvalds' goal for Linux is very simple: World Domination
     
    Eric Pozharski, Sep 13, 2008
    #3
    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. Andres Corrada-Emmanuel

    Optimize flag on shebang line

    Andres Corrada-Emmanuel, Dec 9, 2003, in forum: Python
    Replies:
    0
    Views:
    341
    Andres Corrada-Emmanuel
    Dec 9, 2003
  2. Timothy Madden
    Replies:
    4
    Views:
    2,101
    ryles
    Sep 7, 2009
  3. Blaine
    Replies:
    11
    Views:
    1,571
    Blaine
    Sep 17, 2009
  4. Johnnie Lieske
    Replies:
    6
    Views:
    160
    Johnnie Lieske
    Nov 28, 2008
  5. Ast Jay
    Replies:
    5
    Views:
    150
    Ast Jay
    Jul 27, 2010
Loading...

Share This Page