user warnings Vs #!/usr/bin/perl -w

Let me explain the difference with simple example.

I have created three files FooClass.pm,t1.pl  and t2.pl files

FooClass.pm

package FooClass;

my $a=10;

my $a=120;

1;

t1.pl

use strict;

use warnings;

use FooClass;

my $b = 10;

my $b = 200;

 Error message:

“my” variable $b masks earlier declaration in same scope at perl_bp.pl line 6.

t2.pl

#!/usr/bin/perl -w

use strict ;

use warnings;

use FooClass;

my $b = 100;

my $b = 200;

Error message: 

“my” variable $a masks earlier declaration in same scope at FooClass.pm line 3.

“my” variable $b masks earlier declaration in same scope at perl_bp.pl line 7.

So the difference is scope of two statements to find bugs in perl program.

-w turns on warning for the whole Perl program, including any included modules etc; ‘use warnings’ enables them only in the current lexical scope.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s