PERL annoying scope problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL annoying scope problem
# 1  
Old 02-03-2013
PERL annoying scope problem

Hello,
I met a problem with following code:
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 "$field\n";
    }
        print "\n\n";
}

which looks fine and was running successfully except an warning message:
Code:
"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!
# 2  
Old 02-03-2013
There seem to be null value/s in some column/s of the fetched record/s (undef values/elements in the dereferenced array reference).

You may circumvent this by replacing
Code:
print "$field\n";

with
Code:
print defined $field?$field:'NULL', "\n";

.

Last edited by elixir_sinari; 02-03-2013 at 08:53 PM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 02-03-2013
Thanks so much! That is exactly what you predicted, as I have some columns empty. After I used the way you suggested, the warning disappeared!
Thank you again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Variable Scope in Perl

I have to admit that i have not used Perl at all and this is a singular occasion where i have to patch an existing Perl script. I dearly hope i do not have to do it again for the next 15 years and therefore try to avoid having to learn the programming language in earnest. The OS is AIX 7.1, the... (2 Replies)
Discussion started by: bakunin
2 Replies

2. Programming

C++ Bug probably scope problem

Hello, I have some difficulty to understand the scope of this program adapted from C++ book of D.S. Malik I am reading. I put the header and the main program as attachment to save space. My problem is the output:$ ./prog1009_die Line 4: Not yet rolled, die1 gets default number: 1 Line 5: Not... (5 Replies)
Discussion started by: yifangt
5 Replies

3. Shell Programming and Scripting

How do I get out of the annoying > in bash???

Occasionally I make a mistake in my shell that results in there being a > for the prompt instead of the normal $. Today I accidentally left off a " in a sed command, sed s/\"//g" infile > outfile and then I get $ sed s/\"//g" infile > outfile > > I have never figured out how to get... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

4. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

5. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

6. Shell Programming and Scripting

[SOLVED] Scope of KSH Variable in Perl?

cat test.ksh #!/usr/bin/ksh VAR="Dear Friends \n How are you? \n Have a nice day \n" export VAR echo "Inside test.ksh"; ./test.pl cat test.pl #!/usr/bin/perl print "Inside test.pl \n"; print "$VAR"; Output: ./test.ksh Inside test.ksh Inside test.plWhat I want to achieve is, I... (1 Reply)
Discussion started by: dahlia84
1 Replies

7. Shell Programming and Scripting

problem with shell variable's scope

Hi, I am stuck while developing a shell sub-routine which checks the log file for "success" or "failure". The subroutine reads the log file and checks for key word "success", if found it set the variable (found=1). It returns success or failure based on this variable. My problem is, I can... (2 Replies)
Discussion started by: cjjoy
2 Replies

8. Solaris

annoying problem with nis

This is my home set up I have 2 solaris boxes at home. One is a nis server and one is client. everytime I start the client without server, it will hang permanently looking for for nis server. is there a way to get around this? Can you set timeout the nis client? I use nis becuase my... (4 Replies)
Discussion started by: congngo
4 Replies

9. Shell Programming and Scripting

Very ANNOYING Problem - Please Help

Hey Guys I have an extremely annoying problem with regular expressions! At this point i believe the command 'read' is causing the problem due to the carriage return it places once its done. I have an continuous loop until the input is correct: (After initial read statement) while ... (7 Replies)
Discussion started by: shadow0001
7 Replies

10. UNIX for Dummies Questions & Answers

a very annoying problem

hi i got fbsd here,when i try to start my X server as an user I got hte following error. Fatal server error: xf86OpenConsole: Server must be running with root permissions You should be usig Xwrapper to start the server or xdm. We strongly advise against making the server SUID root! But... (2 Replies)
Discussion started by: Stormpie
2 Replies
Login or Register to Ask a Question