Uninitialized value in String 'eq' Perl

I get a warning running one of my Perl scripts. The error is thrown in a simple if , where I test if a string in an array is equal to another string.

My colleague and I tried several scenarios and still have not been able to resolve the warnings. I tried to post all my research so far in this thread, so it is a little long, but please stick to it. I am completely stuck and hope one of the great minds of Qaru can help me!

Code creating the problem:

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

The code block around this section:

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

My goal in the above code is to assign all the variables found in the text file to the appropriate Perl variables. Then I insert the found variables into the SQL database.

The text file has several fields, which can be in different orders, so I use the switch if-elsif style ... to do the value assignment. There are also some fields that don't bother me, such as Level, and I just ignore these fields. These fields, however, are fields that trigger warnings.

$var set as follows:

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

When I press "Level = 0", I can pause the PerlIDE.exe debugger and see that the line is split into level and 0 and inserted into the array. However, as soon as the code goes to the if and checks the $pieces[0] eq "PromotionNumber" , I get a warning.

I can even print $ pieces [0] right before the if , and it prints β€œLevel”.

If I change the code to the following, the warning will disappear ...

 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 

However, if I test the second line "Level", a warning is returned. The code below WARNING has a warning.

 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 

Why does Perl care about the order in which I test? Note that I am testing a FEW other strings that are plural values ​​in my if-elsif statements that do not give this warning.

Any ideas? I really need to clear this warning so that it does not flood the console at startup. However, the script works with warnings.

Exact error:

Using the uninitialized value in the eq line on the japdpmrijob.pl 250 line.

The exact error line (detected using the Perl debugging utility in PerlIDE.exe):

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

I can print $ pieces [0] and see the value. Therefore, I know that this is determined with my value. I can also print $ pieces [1] and see the expected value. If you first check on $ pieces [0] eq "Level", the warning goes away, and I can access both variables.

I'm still confused ...

It seems that the error is actually "eq", marked as a variable. Any ideas on this?

Below you will find a large snippet of code. I have included the entire for loop and several variables that I work with. Pay attention to the else at the end of the if-elsif-else sequence, I added it to try to stop the warning, as indicated in the third answer. This else prints my expected values ​​every time a warning is raised, so I know that the values ​​are present.

 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"; } } 
+6
source share
3 answers

I think you are looking for a mistake in the wrong place. For a warning that is called inside an if-elsif-else or other complex block of code, older versions of Perl may identify the warning as occurring in the first line of the statement.

 ------ 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. 
+9
source

You can check if $pieces[0] exists before doing comparisons. In most cases this will prevent a warning:

 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 
+2
source

@Scrappedcola thanks, this solved the problem for me

0
source

Source: https://habr.com/ru/post/923995/


All Articles