2012-08-28 29 views
6

我收到了运行我的Perl脚本之一的警告。我正在测试一个简单的if语句,我正在测试一个数组中的字符串是否与另一个字符串相同。字符串'eq'中未初始化的值Perl

我的同事和我已经尝试了几种情况,仍然无法解决警告。我试图把我所有的研究都放到这个主题中,所以它有点长,但请坚持下去。我完全卡住了,希望堆栈的一个伟大的思想 溢出可以帮助我!

的代码产生的问题是:

if ($pieces[0] eq "PromotionNumber") 

的代码围绕该部分的块是:

my @pieces = split(/=/, $var); 
if($pieces[0] eq "PromotionNumber") { 
    $promoNumber = $pieces[1]; 
} elsif ($pieces[0] eq "Type") { 
# More similar code follows 

我在上面的代码的目标是分配所有我发现变量在一个文本文件中添加到各自的Perl变量中。然后,我将这些找到的变量插入到SQL数据库中。

该文本文件有几个字段可以以不同的顺序,这就是为什么我使用开关样式if-elsif ...来完成赋值。还有一些我不关心的领域,比如Level,我只是忽略了这些领域。但是,这些字段是导致警告的字段。

$var因为它遍历被设置为以下...

PromotionNumber=000 
RecordOffset=0 
Code=0 
SubCode=1 
Level=0 

当我打“级别= 0”,我可以在PerlIDE.exe调试器暂停并看到该字符串分割进入Level和0并插入到数组中。但是,只要代码前进到if声明并测试$pieces[0] eq "PromotionNumber",我就会收到警告。

我甚至可以在if声明之前打印$ pieces [0],它将打印出“Level”。

如果我的代码更改为警告消失以下...

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 

但是,如果我测试了“级别”的字符串第二,警告回来。下面的代码有警告。

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 

为什么Perl关心我测试的顺序?请注意,我正在测试几个其他字符串,这些字符串在我的if-elsif语句中是多个elsif的,并没有给出这个警告。

任何想法?我真的需要清除这个警告,以便在运行时不会溢出控制台。该脚本正在处理警告。

精确的错误是:在japdpmrijob.pl路线错误的250

确切的行(使用PerlIDE Perl的调试工具来确定字符串EQ

使用未初始化值。exe)是:

if ($pieces[0] eq "PromotionNumber") { 

我可以打印出$ pieces [0]并查看值。所以我知道它是用我的价值定义的。我还可以打印出$件[1]并查看我的预期值。如果我先测试$ pieces [0] eq“Level”,警告消失,我可以访问这两个变量。

我仍然困惑...

它看起来像错误实际上是“EQ”被标记为一个变量。有关于此的任何想法?

下面你会发现一大块代码。我包括了整个for循环以及我正在使用的几个变量。注意if-elsif-else序列末尾的else声明,我添加了这个以尝试停止第三个答案所指出的警告。这个else语句会在每次调用警告时打印我的预期值,所以我知道这些值是存在的。

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"; 
    } 
} 
+0

几乎可以肯定的问题是'$ pieces [1]'表达式,而不是'$ pieces [0]'。如果你有一个没有'='的'$ var'的值(例如一个空行)'split'将返回一个单一项目的列表。您对“类型”和“级别”的测试仅查看第一个元素并且可以工作,但“PromotionNumber”下的块试图查看不存在的第二个元素并生成警告。 –

+1

请发布您正在获取的确切错误。目前还不清楚是哪条线路导致问题。 – chepner

+0

另外,是否有可能向我们展示更多的代码(至少是设置'$ var'的值的for循环)。这是一个非常奇怪的问题。 – chepner

回答

9

我想你是在错误的地方寻找错误。对于在if-elsif-else语句或其他复杂代码块内触发的警告,旧版本的Perl可能会将警告标识为语句第一行中出现的警告。

------ warn.pl ------ 
my $x = 0; 
my $y; 
if ($x == 1) {   # line 3 
} elsif ($x == 2) { 
} elsif ($x == 3) { 
} elsif ($y == 4) { # line 7 
} 

$ perl5.14.2 -w warn.pl 
Use of uninitialized value $y in numeric eq (==) at warn.pl line 7. 

$ perl5.8.6 -w warn.pl 
Use of uninitialized value in numeric eq (==) at line 3. 
+0

我们有一个其他的应该是“包罗万象”。我应该暂时发布代码。但是,我的同事和我认为代码可能在其他地方,但我们相当积极的是它是在“if-elsif”组合测试中。 – Kyle

+1

根据您的输入,在您遇到“ItemType”行之前,您似乎遇到了“Level = 0”行。这意味着当你敲最后的'elsif'子句时,'$ itemTypeVar'尚未定义。正如@mob指出的那样,这个警告可能会归因于您的大型if-elsif-else语句的初始条款。 – chepner

+0

@chepner非常感谢!我继续前进,标记暴民的答案是正确的,但我可能会愚蠢地发现它是一个完全不同的变量,导致我的错误。我不知道我为什么不考虑对这个变量进行测试。通过验证$ itemTypeVar是在测试之前定义的,所有错误都消失了。非常感谢大家! – Kyle

2

您可能会检查在比较之前是否定义了$pieces[0]。大多数情况下,这将防止警告:

my @pieces = split(/=/, $var); 
my $label = defined($pieces[0]) ? $pieces[0] : ""; #if not defined init to "" 
my $value = defined($pieces[1]) ? $pieces[1] : ""; 
if($label eq "PromotionNumber") { 
    $promoNumber = $value; 
} elsif ($label eq "Type") { 
#More similar code follows 
+0

我试着添加'if(defined($ pieces [0])){print“DEFINED!”; } else {print“NOT DEFINED!”; }就在有问题的if语句之前。它为每个单独的测试打印输出,包括我的问题测试,然后仍然给我一个'if($ pieces [0] eq“PromotionNumber”){'代码行的警告。我完全被困在Perl抱怨的事情上! – Kyle

+2

从Perl 5.10开始,'//'运算符会替换'defined ...? ...:'你有的建筑 - '我的$标签= $件[0] //''' – mob

+0

@mob很好知道。我坚持使用5.8.6,所以我在这些类型的细节上稍微落后了一些。 – scrappedcola