Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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

by troyhughes (Initiate)
on May 23, 2012 at 22:29 UTC ( #972117=perlquestion: print w/replies, xml ) Need Help??
troyhughes has asked for the wisdom of the Perl Monks concerning the following question:

Hi there. I am attempting to test a script that worked without error on my windows box to a Red Hat Linux distro. I get the error 'Use of uninitialized value in pattern match (m//)' on the first line of the if statement from the function below. I've found examples of this error online but fail to see how they apply to my situation. How can I fix this error? Thanks.

sub isUndefOrWhitespace { my $str = ''; $str = shift; print "$str\n"; if (!defined $str) { return 0; }elsif (/^\s*$/) { return 0; } return 1; }

Replies are listed 'Best First'.
Re: Use of uninitialized value in pattern match (m//)
by AnomalousMonk (Chancellor) on May 23, 2012 at 22:37 UTC

    The regex  /^\s*$/ matches by default against the  $_ scalar, which is nowhere initialized in the code you show.
    Perhaps  $str =~ /^\*$/ was what you meant?

Re: Use of uninitialized value in pattern match (m//)
by tobyink (Abbot) on May 23, 2012 at 22:38 UTC

    It's the elsif condition that's triggering it. (The line numbers reported in Perl's error messages... well, there is a logic to them, but it can seem impenetrable at times. In multi-line constructs the line number reported is sometimes several lines away from where a human being would judge the error to have occurred.)

    When you use a pattern match (like /^\s*$/) you need to tell it what variable to test. e.g.:

    $str =~ /^\s*$/

    If you do not tell it what variable to test, then Perl will test the special variable called $_. In your case, $_ appears to be undefined. Hence the message.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Use of uninitialized value in pattern match (m//)
by sauoq (Abbot) on May 23, 2012 at 22:39 UTC

    If this is the exact code you had on your windows box, you likely had a bug there too, even if you never noticed it.

    The problem looks like you are matching against $_ in your eslif condition when you want to be matching against $str. Change that line to:

    }elsif ($str =~/^\s*$/) {

    "My two cents aren't worth a dime.";
      hi, I am also getting same error for below code if ($response->{'Name'} =~ /Approval/) Could you pleas help me. Thanks, sushma

        If you get that error, looking at perldiag usually tells you where the error comes from. In your case, $response->{'Name'} is not what you think it is.

        I recommend looking at $response, maybe by using Data::Dumper.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://972117]
Approved by sauoq
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-07-20 01:27 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (422 votes). Check out past polls.