Tweet

What does the -w mean?

Sometimes at the top of a Perl program you'll see:

    #!/usr/bin/perl

and sometimes you'll see:

    #!/usr/bin/perl -w

What does the -w actually mean?

When you add -w you're asking the Perl compiler to warn you about things in your code that may be errors.

The following rather sloppy little program assumes that $day exists:

    #!/usr/bin/perl

    print "Today is $day\n";
    exit 0;

and it produces the following output:

    Today is

However with the addition of the -w command line flag:

    #!/usr/bin/perl -w

    print "Today is $day\n";
    exit 0;

when we run the program we are duly warned:

    Name "main::day" used only once: possible typo at ./t.pl line 5.
    Use of uninitialized value in concatenation (.) or string at ./t.pl line 5.
    Today is

The warnings pragma

These days, the -w command line flag is usually replaced with the warnings pragma, as the latter is more flexible, and has scope that is limited to the enclosing block, whilst -w is global in effect.

You'll find that the combination of the strict and warnings pragmas will catch lots of errors before they get out of hand.

So make a point of starting any program with:

    #!/usr/bin/perl
    use strict;
    use warnings;

See also

    perldoc perlrun (look for -w)
    perldoc perllexwarn
    perldoc warnings
    perldoc diagnostics
    perldoc perldiag
[Top]