How to put '#!/usr/bin/env perl -w' at the beginning of a perlscript?

Discussion in 'Perl Misc' started by Peng Yu, Dec 29, 2009.

  1. Peng Yu

    Peng Yu Guest

    Since my perl is installed in a nonstandard location, I have to use '/
    usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
    to do it.

    Currently, I have the following error.

    $ head -n 1 ./main.pl
    #!/usr/bin/env perl -w
    $ ./main.pl
    /usr/bin/env: perl -w: No such file or directory
     
    Peng Yu, Dec 29, 2009
    #1
    1. Advertising

  2. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    On 2009-12-29, Peng Yu <> wrote:
    > Since my perl is installed in a nonstandard location, I have to use '/
    > usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
    > to do it.
    >
    > Currently, I have the following error.
    >
    > $ head -n 1 ./main.pl
    > #!/usr/bin/env perl -w
    > $ ./main.pl
    > /usr/bin/env: perl -w: No such file or directory


    Your (Unix) kernel cannot handle more than one argument. A known
    limitation. You need to set the effect -w "by hand":

    #!/usr/bin/env perl
    BEGIN { $^W = 1 } # Can't use -w `with env perl'

    Yours,
    Ilya
     
    Ilya Zakharevich, Dec 29, 2009
    #2
    1. Advertising

  3. On 2009-12-29 21:44, Peng Yu <> wrote:
    > Since my perl is installed in a nonstandard location, I have to use '/
    > usr/bin/env perl'.

    [...]
    > #!/usr/bin/env perl -w


    No. You should use

    #!/path/to/your/perl -w

    instead.

    > I also what to use it with '-w'. I'm wondering how
    > to do it.
    >
    > Currently, I have the following error.
    >
    > $ head -n 1 ./main.pl
    > #!/usr/bin/env perl -w
    > $ ./main.pl
    > /usr/bin/env: perl -w: No such file or directory


    This is because the syntax for the shebang line is extremely restrictive
    on most unixes. In this case (Linux?) everything after the first space
    is passed to /usr/bin/env as a single argument. There may be a
    workaround but The Right Thing(TM) is to use the correct path to perl in
    the first place.

    hp
     
    Peter J. Holzer, Dec 29, 2009
    #3
  4. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    >>>>> "Peng" == Peng Yu <> writes:

    Peng> Since my perl is installed in a nonstandard location, I have to use '/
    Peng> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
    Peng> to do it.

    Don't use "-w". Add "use warnings;" early in your script.

    Also, /usr/bin/env perl is a hack. You should replace it with your
    specific perl path.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
     
    Randal L. Schwartz, Dec 29, 2009
    #4
  5. Peng Yu

    Peng Yu Guest

    On Dec 29, 4:17 pm, (Randal L. Schwartz) wrote:
    > >>>>> "Peng" == Peng Yu <> writes:

    >
    > Peng> Since my perl is installed in a nonstandard location, I have to use'/
    > Peng> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
    > Peng> to do it.
    >
    > Don't use "-w".  Add "use warnings;" early in your script.
    >
    > Also, /usr/bin/env perl is a hack.  You should replace it with your
    > specific perl path.


    If I use my specific perl path, it will not be portable, right? After
    all, if it moves to a different machine, the path has to be fixed.
     
    Peng Yu, Dec 29, 2009
    #5
  6. Peng Yu

    l v Guest

    On 12/29/2009 4:25 PM, Peng Yu wrote:
    > On Dec 29, 4:17 pm, (Randal L. Schwartz) wrote:
    >>>>>>> "Peng" == Peng Yu<> writes:

    >>
    >> Peng> Since my perl is installed in a nonstandard location, I have to use '/
    >> Peng> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
    >> Peng> to do it.
    >>
    >> Don't use "-w". Add "use warnings;" early in your script.
    >>
    >> Also, /usr/bin/env perl is a hack. You should replace it with your
    >> specific perl path.

    >
    > If I use my specific perl path, it will not be portable, right? After
    > all, if it moves to a different machine, the path has to be fixed.


    Create a symbolic link for /usr/bin/perl to your non-standard location
    and you should then be able to use the correct shebang in your Perl scripts.

    --
    Len
     
    l v, Dec 29, 2009
    #6
  7. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    On 2009-12-29, l v <> wrote:
    > On 12/29/2009 4:25 PM, Peng Yu wrote:
    >> On Dec 29, 4:17 pm, (Randal L. Schwartz) wrote:
    >>>>>>>> "Peng" == Peng Yu<> writes:
    >>>
    >>> Peng> Since my perl is installed in a nonstandard location, I have to use '/
    >>> Peng> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
    >>> Peng> to do it.
    >>>
    >>> Don't use "-w". Add "use warnings;" early in your script.
    >>>
    >>> Also, /usr/bin/env perl is a hack. You should replace it with your
    >>> specific perl path.

    >>
    >> If I use my specific perl path, it will not be portable, right? After
    >> all, if it moves to a different machine, the path has to be fixed.

    >
    > Create a symbolic link for /usr/bin/perl to your non-standard location
    > and you should then be able to use the correct shebang in your Perl scripts.


    This assumes you have rights to do that, which is often not the case...

    In a similar situation with one of my clients, where I have access only as a
    "user" not an "admin" I wrote a ksh wrapper script (perlwrap) that does various
    techniques to identify the "correct" perl (based on OS version and
    architecture), and then:

    exec /path/to/perl "$@"

    Then, in my scripts I have:

    #!/mypath/perlwrap -w

    Of course, that assumes you can store the wrapper script in a fixed path... :)

    Kevin
     
    Kevin Collins, Dec 29, 2009
    #7
  8. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    Peng Yu wrote:

    > Since my perl is installed in a nonstandard location, I have to use '/
    > usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
    > to do it.
    >
    > Currently, I have the following error.
    >
    > $ head -n 1 ./main.pl
    > #!/usr/bin/env perl -w
    > $ ./main.pl
    > /usr/bin/env: perl -w: No such file or directory


    just use "use warnings;" which offers some advantages. Keep the env the
    same. Problem solved.
    --
    Not really a wanna-be, but I don't know everything.
     
    Wanna-Be Sys Admin, Dec 30, 2009
    #8
  9. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    Kevin Collins wrote:

    >> Create a symbolic link for /usr/bin/perl to your non-standard
    >> location and you should then be able to use the correct shebang in
    >> your Perl scripts.

    >
    > This assumes you have rights to do that, which is often not the
    > case...
    >


    The Op's first post shows they have shell access, and I find that
    assumption curious, as I've never seen a system prevent a user from
    creating symbolic links (even if it did, it would be unlikely a system
    could prevent the user from executing the ln binary they could upload
    themselves, even if tehy didn't have access to the compilers). But, I
    suppose it's possible. I just think they should call use warnings; at
    the top and not have to mess with anything else, though I never, ever
    use env in a script myself.
    --
    Not really a wanna-be, but I don't know everything.
     
    Wanna-Be Sys Admin, Dec 30, 2009
    #9
  10. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    Ben Morrow wrote:

    >
    > Quoth Tad McClellan <>:
    >> Wanna-Be Sys Admin <> wrote:
    >> > Peng Yu wrote:
    >> >
    >> >> Since my perl is installed in a nonstandard location, I have to use '/

    >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    > Please re-read the solution offered...


    The offered solution was simply wrong, so what Tad wrote is perfectly valid.
    The right solution is to add the "nonstandard" location to $PATH, so that
    env can find perl there.

    The problem has nothing to do with "-w" at all!!!

    Bye...

    Dirk
     
    Dirk Heinrichs, Dec 30, 2009
    #10
  11. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    Ilya Zakharevich wrote:

    > #!/usr/bin/env perl
    > BEGIN { $^W = 1 } # Can't use -w `with env perl'


    Yes, you can!

    Bye...

    Dirk
     
    Dirk Heinrichs, Dec 30, 2009
    #11
  12. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    Dirk Heinrichs wrote:

    > The offered solution was simply wrong, so what Tad wrote is perfectly
    > valid. The right solution is to add the "nonstandard" location to $PATH,
    > so that env can find perl there.
    >
    > The problem has nothing to do with "-w" at all!!!


    Damn, forgot to switch on brain. No, one can not use '#!/usr/bin/env perl -
    w'.

    Sorry for the noise.

    Bye...

    Dirk
     
    Dirk Heinrichs, Dec 30, 2009
    #12
  13. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    Dirk Heinrichs wrote:

    > Ilya Zakharevich wrote:
    >
    >> #!/usr/bin/env perl
    >> BEGIN { $^W = 1 } # Can't use -w `with env perl'

    >
    > Yes, you can!


    Damn, I was so sure that I already did this in the past. You're right, it
    doesn't work.

    Bye...

    Dirk
     
    Dirk Heinrichs, Dec 30, 2009
    #13
  14. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    On 2009-12-30, Sherm Pendley <> wrote:
    > Peng Yu <> writes:
    >
    >> Since my perl is installed in a nonstandard location, I have to use '/
    >> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
    >> to do it.

    >
    > The "warnings" pragma is preferred over the -w switch now anyway:
    >
    > use warnings;


    Wrong. `use warning' is useful in a module. It is mostly useless in
    a script. One should always use -w in scripts.

    Yours,
    Ilya
     
    Ilya Zakharevich, Dec 30, 2009
    #14
  15. Ilya Zakharevich wrote:
    > On 2009-12-30, Sherm Pendley <> wrote:
    >> Peng Yu <> writes:
    >>
    >>> Since my perl is installed in a nonstandard location, I have to use '/
    >>> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
    >>> to do it.

    >> The "warnings" pragma is preferred over the -w switch now anyway:
    >>
    >> use warnings;

    >
    > Wrong. `use warning' is useful in a module. It is mostly useless in
    > a script. One should always use -w in scripts.
    >


    Can anyone provide a reference to where Ilya (or anyone else) provides
    an explanation of this assertion?

    It doesn't seem to be in accord with `perldoc perllexwarn`. Am I right
    to assume there is some disagreement on this point?

    --
    RGB
     
    RedGrittyBrick, Dec 30, 2009
    #15
  16. Peng Yu

    Dr.Ruud Guest

    RedGrittyBrick wrote:
    > Ilya Zakharevich wrote:


    >> `use warning' is useful in a module. It is mostly useless in
    >> a script. One should always use -w in scripts.

    >
    > Can anyone provide a reference to where Ilya (or anyone else) provides
    > an explanation of this assertion?
    >
    > It doesn't seem to be in accord with `perldoc perllexwarn`. Am I right
    > to assume there is some disagreement on this point?


    I prefer -w in scripts too, because it enforces warnings on the included
    code.

    The 'use warnings' is only lexical, that's all there is to it.

    --
    Ruud
     
    Dr.Ruud, Dec 30, 2009
    #16
  17. Re: How to put '#!/usr/bin/env perl -w' at the beginning of a perl script?

    >>>>> "Ruud" == Ruud <> writes:

    Ruud> I prefer -w in scripts too, because it enforces warnings on the included
    Ruud> code.

    So you want to spend time debugging someone else's code? :)

    I put "use warnings" on the code I write while I'm writing it. I'm presuming
    the authors of other modules put use warnings on their code while they're
    writing it.

    Keep in mind that "use warnings" has value *only* while coding. It has no
    practical value in production, so if you're using CPAN modules, you really
    *don't* want warnings enabled on them, because you're not the author of that
    code.

    print "Just another Perl hacker,";

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
     
    Randal L. Schwartz, Dec 30, 2009
    #17
  18. Dr.Ruud wrote:
    > RedGrittyBrick wrote:
    >> Ilya Zakharevich wrote:

    >
    >>> `use warning' is useful in a module. It is mostly useless in
    >>> a script. One should always use -w in scripts.

    >>
    >> Can anyone provide a reference to where Ilya (or anyone else) provides
    >> an explanation of this assertion?
    >>
    >> It doesn't seem to be in accord with `perldoc perllexwarn`. Am I right
    >> to assume there is some disagreement on this point?

    >
    > I prefer -w in scripts too, because it enforces warnings on the included
    > code.


    Then you probably want to use -W instead of -w.



    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway
     
    John W. Krahn, Dec 30, 2009
    #18
  19. On 2009-12-30 11:13, Dr.Ruud <> wrote:
    > RedGrittyBrick wrote:
    >> Ilya Zakharevich wrote:
    >>> `use warning' is useful in a module. It is mostly useless in
    >>> a script. One should always use -w in scripts.

    >>
    >> Can anyone provide a reference to where Ilya (or anyone else) provides
    >> an explanation of this assertion?
    >>
    >> It doesn't seem to be in accord with `perldoc perllexwarn`. Am I right
    >> to assume there is some disagreement on this point?

    >
    > I prefer -w in scripts too, because it enforces warnings on the included
    > code.


    Which is why I disagree with Ilya and you. -w is mostly useless in a
    script and should never be used except maybe as a quick hack during
    debugging.


    > The 'use warnings' is only lexical, that's all there is to it.



    And I think this is a good thing. Getting lots of warnings in code I
    haven't written and cannot change[1][2] is not very helpful. And most
    modules use "use warnings" anyway, so it's usually redundant (and you
    probably should be wary of those that don't use it).

    I do think it was a good thing that -w was invented before "use
    warnings": It forced module maintainers to clean up their code.

    hp

    [1] Well, *I* can change it because I'm sysadmin on most of the systems
    where my code runs. But that's far from universal.

    [2] I remember the bad old days of C programming where you could choose
    between getting lots of warnings from system header files and turning
    off many useful warnings.
     
    Peter J. Holzer, Dec 30, 2009
    #19
  20. On 2009-12-30 08:44, Dirk Heinrichs <> wrote:
    > Ben Morrow wrote:
    >> Quoth Tad McClellan <>:
    >>> Wanna-Be Sys Admin <> wrote:
    >>> > Peng Yu wrote:
    >>> >
    >>> >> Since my perl is installed in a nonstandard location, I have to use '/
    >>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    >> Please re-read the solution offered...

    >
    > The offered solution was simply wrong, so what Tad wrote is perfectly valid.
    > The right solution is to add the "nonstandard" location to $PATH, so that
    > env can find perl there.


    Using /usr/bin/env and relying on the PATH is not a good idea:

    * Perl scripts are often called with a minimal PATH (cron jobs, CGI
    scripts, etc.). Then the perl executable won't be found.
    * The sysadmin may install a new perl executable in a directory which
    appears earlier in the path - then the wrong executable will be
    found.
    * In some situations manipulation of the path may pose a security risk.

    In short, I think a script should be as self-contained as possible.
    Relying on the PATH often leads to trouble and can easily be avoided.

    hp
     
    Peter J. Holzer, Dec 30, 2009
    #20
    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. Parzival
    Replies:
    4
    Views:
    57,697
    Erik Max Francis
    Nov 10, 2003
  2. John Salerno
    Replies:
    30
    Views:
    2,001
    Stephan Kuhagen
    Aug 10, 2006
  3. Yves Dorfsman

    #!/usr/bin/env python vs. #!/usr/bin/python

    Yves Dorfsman, May 2, 2008, in forum: Python
    Replies:
    27
    Views:
    2,042
    Tim Roberts
    May 10, 2008
  4. anne001
    Replies:
    1
    Views:
    520
  5. Gilles
    Replies:
    7
    Views:
    569
    Matej Cepl
    Sep 30, 2012
Loading...

Share This Page