http://www.perlmonks.org?node_id=1001709

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

I don't know why below error message(I add it in the comment)

{ package packageA my %hash_map = ( "name" => "path", ......, ); sub new{ ........ } sub get{ my $class = shift; my $componentName = @_; my $path = $component_path{$componentName}; #error: #Use of uninitialized value $path in pattern match (m//) at below... if($path =~ m/HKEY_LOCAL_MACHINE/){ dosomthing; } } }

Replies are listed 'Best First'.
Re: Use of uninitialized value in pattern match (m//)
by choroba (Cardinal) on Oct 31, 2012 at 17:12 UTC
    The key $componentName does not exist in %component_path or is assigned an undef. The reason might be you assign the number of subroutine arguments to $componentName: instead of
    my $componentName = @_;
    you probably meant
    my ($componentName) = @_; # list context
    or
    my $componentName = $_[0];
    or even
    my $componentName = shift;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Yes, you both are right. I missed the componentName's value when called. Thanks!
Re: Use of uninitialized value in pattern match (m//)
by Corion (Patriarch) on Oct 31, 2012 at 17:10 UTC

    What is the value of $componentName, and what are the values of %component_path? The error message suggests that $component_path{ $componentName } is uninitialized, which likely means that the value of $componentName is not what you expect it to be.

    I recommend printing out the value of $componentName.

Re: Use of uninitialized value in pattern match (m//)
by space_monk (Chaplain) on Oct 31, 2012 at 17:22 UTC
    AFAICT, the hash $component_path is not defined in the example you gave, so $path in the get() method will also be undefined, so you are trying to pattern match against an undefined variable.