use strict;
This is the error we're going to focus on fixing.Global symbol "$xxx" requires explicit package name at ./tst line 5.
# Change this: $string = "hello world"; @array = qw(ABC DEF); %hash = (A=>1, B=>2); # To this: my $string = "hello world"; my @array = qw(ABC DEF); my %hash = (A=>1, B=>2); # Change this: # '$name' is global here @names = qw(Bob Fred); foreach $name (@names) { print "Name: $name\n"; } # To this: my @names = qw(Bob Fred); foreach my $name (@names) { # Now '$name' only exists in this block print "Name: $name\n"; } # Change this: # Likewise, '$digit' is global here @digits = (5,3,4); foreach $digit (@digits) { $number = 10*$number + $digit; } print "Number: $number\n"; # To this (variables used in an outer scope ('$number') # will have to be declared in an outer scope): my @digits = (5,3,4); my $number = 0; foreach my $digit (@digits) # Now '$digit' only exists in this block $number = 10*$number + $digit; } print "Number: $number\n"; # Change this: sub my_sub { ($arg1, $arg2) = @_; print "Arg1: $arg1 Arg2: $arg2\n"; } # To this: sub my_sub { my ($arg1, $arg2) = @_; print "Arg1: $arg1 Arg2: $arg2\n"; } # Using DBI? You can change this: $sth->bind_columns(\$field1, \$field2); while ($sth->fetch) { print "F1: $field1 F2: $field2\n"; } # To this (the '\' is distributed over a list of values): $sth->bind_columns(\my ($field1, $field2)); while ($sth->fetch) { print "F1: $field1 F2: $field2\n"; }
In perl's before 5.6 (or if you just want to be portable between the versions), you can put '-w' on the 'shebang' line, or set the $^W variable (however, setting $^W will not catch compile time warnings unless its in a BEGIN{} block, so '-w' is usually preferable):use warnings;
If you know you want to disable warnings, you can do it in a limited scope:#!/usr/local/bin/perl -w # Or $^W = 1; # Or BEGIN { $^W = 1 }
Or sometimes you'll have to initialize variables as in the example above that uses '$number'.# Change this: sub add_two_numbers_which_might_be_undef { $_[0] + $_[1]; } # To one of these (depending on perl version): # 1 sub add_two_numbers_which_might_be_undef { # See 'perldoc perllexwarn' for all the categories of warnings # because its better to only disable the warnings you're expecting no warnings "uninitialized"; $_[0] + $_[1]; } # 2 sub add_two_numbers_which_might_be_undef { local $^W; $_[0] + $_[1]; }
Also read Ovid's excellent 'use strict' is not Perl.
And that's it! Now you have no excuse for not using strict or warnings. And it'll make life easier for all of us :)
|
---|
Replies are listed 'Best First'. | |
---|---|
(elbie): Use strict and warnings
by elbie (Curate) on Sep 10, 2001 at 17:03 UTC | |
by Anonymous Monk on Sep 26, 2003 at 00:37 UTC | |
Re: Use strict and warnings
by Qiang (Friar) on May 26, 2005 at 13:41 UTC | |
Re: Use strict and warnings
by ansh batra (Friar) on Nov 02, 2011 at 05:05 UTC | |
Re: Use strict and warnings
by Anonymous Monk on Jul 24, 2012 at 13:37 UTC | |
Re: Use strict and warnings
by Anonymous Monk on May 24, 2007 at 10:11 UTC |