Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'use warnings' vs. '#!/usr/bin/perl -w' Is there a difference?

Tags:

perl

I read that it is better to use warnings; instead of placing a -w at the end of the shebang.

What is the difference between the two?

like image 683
0112 Avatar asked Apr 25 '14 17:04

0112


People also ask

What are some differences between strict and warnings?

use strict pragma also works in the same way as use warnings but the only difference is that the strict pragma would abort the execution of program if an error is found, while the warning pragma would only provide the warning, it won't abort the execution.

Why use strict and warnings Perl?

Strict and Warning are probably the two most commonly used Perl pragmas, and are frequently used to catch “unsafe code.” When Perl is set up to use these pragmas, the Perl compiler will check for, issue warnings against, and disallow certain programming constructs and techniques.

What does $_ mean in Perl?

There is a strange scalar variable called $_ in Perl, which is the default variable, or in other words the topic. In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used.

What does use strict mean in Perl?

use strict; The use strict pragma forces the developer to code in a manner that enables the code to execute correctly, and it makes the program less error-prone. As an example, the use strict pragma forces the developer to declare variables before using them. We can declare variables with the keyword my in Perl.


1 Answers

The warnings pragma is a replacement for the command line flag -w, but the pragma is limited to the enclosing block, while the flag is global. See perllexwarn for more information and the list of built-in warning categories.

warnings documentation

The advantage of use warnings is that it can be switched off, and only affects the immediate scope.

Consider for example a module that uses operations that would emit warnings:

package Idiotic;
sub stuff {
    1 + undef;
}

Then we get a warning if we do

#!perl -w
use Idiotic; # oops, -w is global

Idiotic::stuff();

but don't get any warning with

#!perl
use warnings;  # pragmas are scoped, yay!
use Idiotic;

Idiotic::stuff();
like image 123
amon Avatar answered Sep 18 '22 19:09

amon