title: Uninitialized Value in String 'eq' Perl
body: <p>I'm getting a warning running one of my Perl scripts. The error is being thrown at a simple <code>if</code> statement where I'm testing if a string in an array is equal to another string.</p>
  
  <p>My coworker and I have tried several scenarios and still haven't been able to resolve the warnings. I've tried to place all my research so far into this thread, so it is a little long, but please stick with it. I'm completely stuck and hoping one of the great minds of Stack&nbsp;Overflow can help me out!</p>
  
  <p>The code generating the problems is:</p>
  
  <pre><code>if ($pieces[0] eq "PromotionNumber")
  </code></pre>
  
  <p>The block of code around that section is:</p>
  
  <pre><code>my @pieces = split(/=/, $var);
  if($pieces[0] eq "PromotionNumber") {
       $promoNumber = $pieces[1];
  } elsif ($pieces[0] eq "Type") {
  # More similar code follows
  </code></pre>
  
  <p>My goal in the above code is to assign all the variables I've found in a text file to the respective Perl variables. I then insert those found variables into a SQL database.</p>
  
  <p>The text file has several fields that can be in different orders, which is why I use the switch style if-elsif... to accomplish assigning values. There are also some fields I don't care about, such as Level, and I just ignore these fields. These fields however are the fields that cause warnings.</p>
  
  <p>The <code>$var</code> is set to the following as it loops through...</p>
  
  <pre><code>PromotionNumber=000
  RecordOffset=0
  Code=0
  SubCode=1
  Level=0
  </code></pre>
  
  <p>When I hit "Level=0", I can pause in the PerlIDE.exe debugger and see that the string is split into Level and 0 and inserted in the array. However, as soon as the code advances to the <code>if</code> statement and tests <code>$pieces[0] eq "PromotionNumber"</code> I get the warning.</p>
  
  <p>I can even print out $pieces[0] right before the <code>if</code> statement, and it will print "Level".</p>
  
  <p>If I change the code to the following the warning goes away...</p>
  
  <pre><code>my @pieces = split(/=/, $var);
  if($pieces[0] eq "Level") {
      #My problematic variable test
  }elsif($pieces[0] eq "PromotionNumber") {
       $promoNumber = $pieces[1];
  } elsif ($pieces[0] eq "Type") {
  #More similar code follows
  </code></pre>
  
  <p>However, if I test for the "Level" string second, the warning comes back. The code below DOES have the warning.</p>
  
  <pre><code>my @pieces = split(/=/, $var);
  if($pieces[0] eq "PromotionNumber") {
      #My problematic variable test
  }elsif($pieces[0] eq "Level") {
       $promoNumber = $pieces[1];
  } elsif ($pieces[0] eq "Type") {
  #More similar code follows
  </code></pre>
  
  <p>Why does Perl care which order I test in? Note, that I am testing SEVERAL other strings that are multiple elsif's down in my if-elsif statements that don't give this warning.</p>
  
  <p>Any ideas? I really need to clear up this warning so that it doesn't flood the console when running. The script is working with the warnings though.</p>
  
  <p>Exact error is:</p>
  
  <blockquote>
    <p>Use of uninitialized value in string eq at japdpmrijob.pl line 250.</p>
  </blockquote>
  
  <p>Exact line of error (determined using Perl's debug utility in PerlIDE.exe) is:</p>
  
  <pre><code>if ($pieces[0] eq "PromotionNumber") {
  </code></pre>
  
  <p>I can print out $pieces[0] and see the value. So I know it is defined with my value. I can also print out $pieces[1] and see my expected value. If I test for $pieces[0] eq "Level" first, the warning goes away and I can access both variables.</p>
  
  <p>I am still confused...</p>
  
  <p>It looks like the error is actually the "eq" being flagged as a variable. Any ideas on that?</p>
  
  <p>Below you'll find a large chunk of the code. I included the entire <code>for</code> loop and several of the variables that I'm working with. Notice the <code>else</code> statement at the end of the if-elsif-else sequence, I added this to attempt stop the warning as noted by the third answer. This <code>else</code> statement prints my expected values every time the warning is called, so I know the values are present.</p>
  
  <pre><code>for my $cond (@conditions) {
      if($debug==1){print $cond."\n";}
  
      # Required database variables
      my $isRecord = 0;
      my $promoNumber;
      my $type;
      my $process;
      my $testValue;
      my $recordOffset;
      my $code;
      my $subcode;
      my $itemType;
      my $itemValue;
  
      # Function test variables
      my $itemTypeVar;
      my $newQualifier = 1;
  
      # Database Configuration
      my $dbApps = new Win32::ODBC("myDatabase") || die "Error: " . Win32::ODBC::Error();
      my @condVars = split(/\|/, $cond);
      for my $var (@condVars) {
          if($debug==1){print $var."\n";}
          my @pieces = split(/=/, $var);
          if( defined($pieces[0]) ){
              print "piece 0 defined!\n";
          } else {
              print "pieces 0 not defined!\n";
          }
          if( defined($pieces[1]) ){
              print "piece 1 defined!\n";
          } else {
              print "piece 1 not defined!\n";
          }
          if($pieces[0] eq "PromotionNumber"){
              $promoNumber = $pieces[1];
          } elsif ($pieces[0] eq "Type"){
              $type = $pieces[1];
          } elsif ($pieces[0] eq "Process"){
              $process = $pieces[1];
          } elsif ($pieces[0] eq "TestValue"){
              $testValue = $pieces[1];
          } elsif ($pieces[0] eq "RecordOffset"){
              $recordOffset = $pieces[1];
              if ($recordOffset == 0) {
                  $newQualifier = 1; }
          } elsif ($pieces[0] eq "Code"){
              $code = $pieces[1];
          } elsif ($pieces[0] eq "SubCode"){
              $subcode = $pieces[1];
          } elsif ($pieces[0] eq "ItemType"){
              $itemType = $pieces[1];
              if($itemType eq "0") {
                  $itemTypeVar = "ItemCode";
              } elsif($itemType eq "1") {
                  $itemTypeVar = "ItemCode";
              } elsif($itemType eq "2") {
                  $itemTypeVar = "Department";
              } elsif($itemType eq "5") {
                  $itemTypeVar = "MixMatchCode";
              } elsif($itemType eq "12") {
                  $itemTypeVar = "GroupCode";
              }
          } elsif ($pieces[0] eq $itemTypeVar){
              $itemValue = $pieces[1];
          } else {
              print "$pieces[0] and $pieces[1] not used.\n";
          }
          print "$var\n";
      }
  }
  </code></pre>