Question

I get a uninitialized value in string eq at *** line xxx warning in my code, which would be easy too fix if there actually was aneq at that line.

But there is a regular expression match on a value inside a hashref.

if ($hashref->{parameters}->{type} =~ m/:/) {

some lines before this I even have this:

$hashref->{parameters} = defined($hashref->{parameters}) ? $hashref->{parameters} : '';
$hashref->{parameters}->{type} = defined($hashref->{parameters}->{type}) ? $hashref->{parameters}->{type} : '';

so the value should be at least initialized.

I'm asking myself and you: why do I still get the warning that the value is uninitialized and moreover why does it say eq instead of pattern match

Edit:

The parameters subhash contains all variabels given via url input (post and/or get). The type value is one of those variables which could be in the url. It does not matter if or if not the type value is in the url, and if it contains a value, I always get an uninitialized value in string eq warning. Even if I control the value of type the line by warning it before the buggy line.

2. edit: As @ikegami supposed there is indeed an elsif which caused the warning

The whole if - elsif statement looks somehow like:

if ($hashref->{parameters}->{type} =~ m/:/) {
    …
elsif ($hashref->{parameters}->{type} eq $somevalue) {
    …
}

and it was $somevalue that was uninitialized.

Was it helpful?

Solution

You only showed half of the statement on that line. The full statement actually looks something like

456: if ($foo =~ /bar/) {
457:    ...
458: }
459: elsif ($baz eq 'qux') {
460:    ...
461: }

Run-time warnings for a statement normally use the line number at which the statement started, so if the regex doesn't match and $baz is undefined, you'll get a warning listing line 456 for the eq on line 459.

It's the same idea here:

$ perl -wE' my $x;  # 1
            say     # 2
            4       # 3
            +       # 4
            $x      # 5
            +       # 6
            5;      # 7 '
Use of uninitialized value $x in addition (+) at -e line 2.
9

Recently*, Perl was changed so that elsif conditions are considered a different statement to avoid this kind of problem. You must have an older version of Perl.

  • — Actually, not so recently. Both 5.10 and 5.12 have been end-of-lifed, yet you appear to be using an even older version that that. If you're going to ask questions about a far-obsolete version of Perl, please mention it.

$ perlbrew use 5.10.1

$ perl -we'
my ($x,$y,$z);
if ($x) {
} elsif ($y eq $z) {
}'
Use of uninitialized value $y in string eq at -e line 4.
Use of uninitialized value $z in string eq at -e line 4.

$ perlbrew use 5.8.9

$ perl -we'
my ($x,$y,$z);
if ($x) {
} elsif ($y eq $z) {
}'
Use of uninitialized value in string eq at -e line 3.
Use of uninitialized value in string eq at -e line 3.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top