how can I check a variable is initialized are not

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pnsreee
    New Member
    • Apr 2007
    • 34

    how can I check a variable is initialized are not

    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.

    Code:
    my $tmp;
    my $var;
    $var = "raghu";
    if ( $tmp eq "" )
    {
    if ($tmp ne $var)
    {
            print " XYZ \n";
    }
    }
    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
    New Member
    • Oct 2006
    • 85

    #2
    Hi ,

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

    From the above code u may get ur need...

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      Hi pnsreee,

      Use the defined function:


      Code:
      my $tmp;
      
      if (defined $tmp) {[indent]print "tmp is defined";
      [/indent]} else {[indent]print "tmp is not defined";
      [/indent]}
      - Miller

      Comment

      Working...