htmanning has asked for the wisdom of the Perl Monks concerning the following question:

Sorry for the dumb question. I have a script like many others, but unless I add a -w at the top it returns an Internal Server Error. I find nothing special about this script. It searches a database and returns some html. #!/usr/bin/perl -w Without that -w it won't run. What is that switch?

Replies are listed 'Best First'.
Re: What is -w?
by Athanasius (Archbishop) on Apr 28, 2016 at 07:27 UTC

    Hello htmanning,

    The -w command line switch is like use warnings; except that whereas the latter is lexically scoped, -w is global. That is, -w activates warnings in used or requiredd modules as well as in the current file. So I find it hard to imagine how including it could actually remove an error.

    Can you post the code? Or better yet, can you create a minimal script which exhibits the same behaviour?

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      So I find it hard to imagine how including it could actually remove an error
      It can!
      perl -e "die unless $^W" Died at -e line 1. perl -we "die unless $^W"
      In addition, if understand, OP is running the script in a webserver ("Internal Server Error..") so many things can be involved.

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: What is -w?
by Anonymous Monk on Apr 28, 2016 at 07:18 UTC
    Read about it and all switches in perlrun
      Thanks. It enables warnings, which is what I suspected. The question is why a script wouldn't run without it if all it does is print warnings. I get all kinds of warnings in the logs with this script even though none of them seem to be serious or pan out. Most of them say I have a variable that is only used once, even though it is used in another required file. Thanks for the pointer. I'm still stumped as to why the script would return an error just because I wasn't printing warnings.

        Most likely you are trying to run the script on a Unixish machine but the file uses Windows-style \r\n line endings.

        If that is the case, the kernel will look at the file, see that it starts with #! and it will then read the line and try to execute the first word on that line as the interpreter. Likely, parsing for words only means "space or newline", so the kernel will see:

        /usr/bin/perl\r

        as the program to execute. There is no such program. Once you add -w to the command line, the kernel will see:

        /usr/bin/perl -w\r

        and use /usr/bin/perl as the program and -w\r as the first parameter to it. This starts Perl.

        In any case, it is good to run with the -w switch always as this allows Perl to tell you about problematic things in your code.