http://qs1969.pair.com?node_id=538339

Dervish has asked for the wisdom of the Perl Monks concerning the following question:

I have a code snippet that's generating odd errors. Can you tell what I've done wrong?
print "$tmpf\n"; # prints 'farms' print keys( %factories); # 'farms' is one of them. if ( defined $factories{ $tmpf})
This seems to do what I want, but it also generates the error message 'Use of uninitialized value in pattern match (m//)'

Replies are listed 'Best First'.
Re: Why? uninitialized value in pattern match when using if (defined)
by SamCG (Hermit) on Mar 22, 2006 at 00:08 UTC
    Welcome to the monastery, Dervish!

    As suggested, you should give a minimal piece of code that will compile and run and produce the issue you're having. In case you're not aware (though you've likely guessed), the message you're seeing is a non-fatal warning, which means you're invoking the script with -w or putting use warnings; at the top. This is usually (always?) a good thing to do.

    What this particular warning is telling you is that you're attempting to use a regular expression against an undefined variable. Something like the following, for example, would produce a similar warning to what you're seeing.
    use warnings;## good line to have. use strict; is also prudent $x=undef; ## I explicitly set it to undef here] ## even if I commented this, though, I'd have ## the same warning ($s)=$x=~m/regex/; ## $x is undefined, and produces warning print $s; ## presumably, I wanted $s for something
    You should definitely look at the line number suggested by the warning.
Re: Why? uninitialized value in pattern match when using if (defined)
by ikegami (Patriarch) on Mar 21, 2006 at 23:28 UTC

    Run-time warnings and errors in the expression of an elsif condition will bear the line number of the start of the if statement.

Re: Why? uninitialized value in pattern match when using if (defined)
by GrandFather (Saint) on Mar 21, 2006 at 23:30 UTC

    I have a code snippet and it works exactly as expected:

    use strict; use warnings; my $tmpf = 'farms'; my %factories = (farms => "Farmer Brown's"); print "$tmpf\n"; # prints 'farms' print keys( %factories), "\n"; # 'farms' is one of them. print "Is defined" if ( defined $factories{ $tmpf});

    Prints:

    farms farms Is defined

    How is it different from your code snippet that generates an error message that on the face of it has nothing to do with the snippet? Perhaps you could modify my snippet to demonstrate your error? You could also read I know what I mean. Why don't you?.


    DWIM is Perl's answer to Gödel
      or even with a match:
      #!/usr/bin/perl use strict; use warnings; my %factories = ( 'farms' => 'horses'); my $tmpf = 'farms'; my $match = "horses on the farm!"; print "$tmpf\n"; # prints 'farms' print keys( %factories), "\n"; # 'farms' is one of them. if ( defined $factories{$tmpf} ) { print "yay!\n" if $match =~ m/$factories{$tmpf}/; }
      prints:
      farms farms yay!

      to ask a question is a moment of shame
      to remain ignorant is a lifelong shame

Re: Why? uninitialized value in pattern match when using if (defined)
by kwaping (Priest) on Mar 22, 2006 at 00:24 UTC
    The hash key, $tmpf, is defined as "farms". However, the hash value $factories{$tmpf}, aka $factories{'farms'}, appears to be undefined.

    ---
    It's all fine and dandy until someone has to look at the code.

      Maybe so. Though we don't now by now, what the warning actually refers to.

      The OP is not very clear about this, but the consensus in the replies seems to be that the offending operation takes place inside a block guarded with:

      if ($factories{$tmpf})

      If $factories{$tmpf} had been undef, perl would not have reached the inside of that conditional block.

      Of course this is also pure guesswork, unless the OP succeds in posting some code to reproduce the problem. :-D

        Thank you for the reply, you make a good point that I didn't consider.

        ---
        It's all fine and dandy until someone has to look at the code.