use strict;
####
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";
}
##
##
use warnings;
##
##
#!/usr/local/bin/perl -w
# Or
$^W = 1;
# Or
BEGIN { $^W = 1 }
##
##
# 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];
}