Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Use of uninitialized value in pattern match (m//)

by anaconda_wly (Scribe)
on Oct 31, 2012 at 17:05 UTC ( #1001709=perlquestion: print w/ replies, xml ) Need Help??
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; } } }

Comment on Use of uninitialized value in pattern match (m//)
Download Code
Replies are listed 'Best First'.
Re: Use of uninitialized value in pattern match (m//)
by choroba (Canon) 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 (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001709]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (15)
As of 2015-07-28 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (256 votes), past polls