"defined $r_libs->{$name} " triggers warning "Use of uninitialized value"

L

Liang

Hi,

I use "defined $r_libs->{$name}" to check first if a key exists in a hash
table. But Perl gives a warning WHENEVER the key exists: "Use of
uninitialized value".

Would u please help to check the script, and let me know the reason? Thanks
in advance.

Liang

----------------------------------
#!/usr/local/bin/perl -w

my %libraries;

#$obj_name='obj1';
#$sect_name='sect1';
#$len='001a2';

print "KKK\n";


add_one_libobj(\%libraries, 'obj1', 'sect1', '001a2');
add_one_libobj(\%libraries, 'obj2', 'sect2', '002a2');

add_one_libobj(\%libraries, 'obj1', 'sect2', '002a2');

add_one_libobj(\%libraries, 'obj3', 'sect3', '003a2');

add_one_libobj(\%libraries, 'obj1', 'sect3', '003a2');

print "@@@\n";

print_lib_res (\%libraries);

print "HHH\n";


#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# put the result into hash table
#----------------------------------------
sub add_one_libobj
{
my ($r_libs, $name, $sect, $len) = @_;

print "add_one_libobj:: %$r_libs, $name\n";

#
# the following line will trigger an Perl warning:
# Use of uninitialized value at map_reader.pl
# do u know what's wrong? thanks. Liang
#
if ( defined $r_libs->{$name} ){
#print " the library exists:: $r_libs->{$name}\n";
$r_libs->{$name}->{$sect} += hex "0x$len";
}else{
my %rec;
$rec{$sect}= hex "0x$len";
$r_libs->{$name}=\%rec;

# print " the library is new:: %$r_libs->{$name}\n";
}
}

sub print_lib_res
{
my ($r_libs) = @_;
my ($name, $sect, $len) ;

#print"print_lib_res:: $r_libs\n";

foreach $name (sort keys %$r_libs){
my $r_sect = %$r_libs->{$name};

#print "name=$name, sect = $r_sect\n";
foreach $sect ( sort keys %$r_sect ){

print <<MSG_1
lib/obj=$name, sect=$sect, len=$r_sect->{$sect};
MSG_1
;

}
}

}
-----------------------------------
output:
-----------------------------------
KKK
add_one_libobj:: %HASH(0x825c68), obj1
add_one_libobj:: %HASH(0x825c68), obj2
add_one_libobj:: %HASH(0x825c68), obj1
Use of uninitialized value at tt.pl line 46.
add_one_libobj:: %HASH(0x825c68), obj3
add_one_libobj:: %HASH(0x825c68), obj1
Use of uninitialized value at tt.pl line 46.
@@@
lib/obj=obj1, sect=sect1, len=418;
lib/obj=obj1, sect=sect2, len=674;
lib/obj=obj1, sect=sect3, len=930;
lib/obj=obj2, sect=sect2, len=674;
lib/obj=obj3, sect=sect3, len=930;
HHH
 
J

Joe Smith

Liang said:
Would u please help to check the script, and let me know the reason? Thanks
in advance.

Your script works with perl-5.8.3 after changing one line to
my $r_sect = $r_libs->{$name};
by removing the extraneous % sign.

Try with other versions of perl, and post your problem to
comp.lang.perl.misc not comp.lang.perl .
-Joe
 
L

Liang

Joe Smith said:
Your script works with perl-5.8.3 after changing one line to

Thanks Joe. You're right. It works on 5.6.0 as well. The problem occuses on
5.001.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top