Connecting Tech Pros Worldwide Forums | Help | Site Map

how can I check a variable is initialized are not

Member
 
Join Date: Apr 2007
Posts: 35
#1: Apr 30 '07
Hi All,

I have a uninitialized variabe $tmp. How can I check if the variable is initialized are not.I want to catech the warning without suppressing it using no warnings qw( uninitialized ). Please tell me how can I do it.

Expand|Select|Wrap|Line Numbers
  1. my $tmp;
  2. my $var;
  3. $var = "raghu";
  4. if ( $tmp eq "" )
  5. {
  6. if ($tmp ne $var)
  7. {
  8.         print " XYZ \n";
  9. }
  10. }
  11.  
The above code I used but I cant able to catch the warning.
but I got the output.
output:
Use of uninitialized value in string eq at tmp.txt line 8.
Use of uninitialized value in string ne at tmp.txt line 10.
XYZ

savanm's Avatar
Member
 
Join Date: Oct 2006
Location: chennai
Posts: 86
#2: Apr 30 '07

re: how can I check a variable is initialized are not


Hi ,

if($tmp)
{
print "Initialised";
}
else
{
print "Not Initialised";
}

From the above code u may get ur need...
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#3: Apr 30 '07

re: how can I check a variable is initialized are not


Hi pnsreee,

Use the defined function:
http://perldoc.perl.org/functions/defined.html

Expand|Select|Wrap|Line Numbers
  1. my $tmp;
  2.  
  3. if (defined $tmp) {
    print "tmp is defined";
  4. } else {
    print "tmp is not defined";
  5. }
  6.  
- Miller
Reply