Friday, April 29, 2011

Handling uninitialized value in perl

In perl* when you find this error "Use of uninitialized value in concatenation (.) or string at".It means you try to use uninitialized(empty valued) variable.


For example you try to print

my $name;

print "Hi $name ";


Then it will print the message "Use of uninitialized value in concatenation ......."


To overcome from this message you need to check either this variable is initialized or not.For this we used "defined"

If we use 

defined EXPR -->Returns true if expr has some value otherwise false

then our line

print "Hi $name ";


now modified to


if(defined $name)

{

print "Hi $name ";

}

else

{

print "Hi ";

}


*I not programmed in perl.This is what i learn in the time of troubleshooting the old scripts.

No comments: