By yifangt
I met a problem with following code:
#!/usr/bin/perl -w
# test.pl
use strict;
use diagnostics;
use DBI;
my $dbh = DBI->connect(
"DBI:mysql:BibleBook","yifangt","password")
or die("Cannot connect: $DBI::errstr");
my $sql = qq(SELECT * FROM library WHERE isbn = '1234512345');
my $sth = $dbh->prepare($sql);
$sth->execute;
while(my $record = $sth->fetch){
for my $field (@$record){
print "$fieldn";
}
print "nn";
}
which looks fine and was running successfully except an warning message:
"Use of uninitialized value $field in concatenation (.) or string at test.pl line 20. "
The diagnostics tells me the problem is with line 20 and I googled around and, found it is a very popular scope problem with $field, which actually should be related to @$record. But,
1)$record was declared, so was $field;
2)how the scope change within the while and for loops, or/and outside the while loop? which is what I am not sure about;
3) the interesting thing is not every line of the loop issued the same warning. Why?
Can anybody tell me exactly what was wrong in my case and the correct way to eliminate this warning?
Thanks a lot!

Add to favorites 