Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by wolis (Scribe)
on Feb 10, 2004 at 03:10 UTC ( [id://327799]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I have search PerlMonks and found questions and answers on all things Use of uninitialized value.. but none refer to pattern matching.

Now this warning logically makes sence, I have not initialise a variable in my regex.

then you look at the if regex it complained about:

if($tmp2 =~ m/\'/) { $tmp2 =~ s/\'/\\\'/g; # escape the single quotes.. }

How can I initialise the single quote? Should I be definding a var and using that in my regex? is this important?

Thanks,

___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com

Replies are listed 'Best First'.
Re: Use of uninitialized value in pattern match (m//)
by Mr. Muskrat (Canon) on Feb 10, 2004 at 03:14 UTC

    Did you give $tmp2 a value? If so, how did $tmp2 get defined?

    Update: What are you trying to do? You don't need to backwhack the single quote inside of a regex.

      Did you give $tmp2 a value? If so, how did $tmp2 get defined?
      Exactly. I often find that this happens to me if the variable was assigned as the result of a split. If there werent enough things to split, you get some undefined values. (Hope that makes sense - I'm tired).

      Cheers!

      Ah yes.. $tmp2 is defined with a:
      my $tmp2 = $2;
      So maybe because $2 from my previous regex is undef, $tmp2 becomes undef resulting in the warning.. this makes sense.

      So to fix this would the 'best' solution to wrap my code in an if block:

      if($tmp2){ if($tmp2 =~ m/'/){ ...

      I thought 'in pattern match' literally meant within the m//

      As for me escaping a single quote.. I get paranoid and end up escaping everything in my regex that is not an Alphanumeric just in case :-)

      ___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com
        So maybe because $2 from my previous regex is undef, $tmp2 becomes undef resulting in the warning.. this makes sense.

        So to fix this would the 'best' solution to wrap my code in an if block:

        if($tmp2){ if($tmp2 =~ m/'/){ ...

        You shouldn't be even using $2 if the match that was supposed to give it a value failed. You must rework your script's logic so that $2 is only being used if the match that loaded it succeeded.

        In fact, it's dangerous to simply assume that if $2 is undefined if the previous match failed. Such is not always the case. It's even dangerous to assume anything about $2 unless you KNOW that the previous match succeeded. Take a look at the following contrived snippet:

        use strict; use warnings; my $string = "Hello world!"; $string =~ m/(H\w+)\s(\w+)/; # This match succeeds. print $2, "\n"; $string =~ /(\d)(\d)/; # This match fails. print $2, "\n";

        And the output is:

        world world

        This tells us that $2, which gains a value during the first match, retains that value even though the second match failed. In other words, even though you gave the possibility for $2 to gain a new value in the second match, it retained the original value because the second match failed.

        That means that it is unreliable to just assume $2 is going to be undef if the most recent match failed, because it could possibly be holding some value from a previous pattern match.

        Go back to the drawing board with regards to your program's logic. ;)

        Now in regards to how to avoid an "undefined" warning, just check for definedness before doing anything with it. Please note, this solution is a stopgap measure, and won't fix the underlying bug in your script. I am providing this only because you essentially asked how to do something only if a scalar holds a defined value:

        my $tmp2 = $var; # Because of bad logic we don't know # if $var is defined, thus don't know if # $tmp2 is defined. if ( defined( $tmp2 ) and $tmp2 =~ m/regex/ ) { # Do your stuff. }

        Enjoy! I hope all this helps...


        Dave

Re: Use of uninitialized value in pattern match (m//)
by Trimbach (Curate) on Feb 10, 2004 at 04:15 UTC
    If the 'undefined' warning bothers you you can fix it by making sure $tmp2 is always defined. This works wonders:
    my $tmp2 = ""; $tmp2 = $2;
    ...which gives you the added bonus of scoping $tmp2. Plus, you don't have to check whether a character is present in your var before substituting it out.
    if($tmp2 =~ m/\'/) { $tmp2 =~ s/\'/\\\'/g; # escape the single quotes.. }
    is better written as:
    $tmp2 =~ s/'/\\'/g; # escape the single quotes..
    If there's a single quote in $tmp2 it'll get escaped. Otherwise nothing happens. Note that you don't have to escape single-quotes within a regex.

    Lastly if you're paranoid about non-Alphanumerics in your regex's check out the \Q modifier. This does what you want automatically within a regular expression without you having to do anything else.

    Gary Blackburn
    Trained Killer

    Edited:Added my $tmp2=""; to really make $tmp2 defined. :-P

      my $tmp2 = $2;

      This does not make $tmp2 defined if $2 is undefined. Don't you mean something like:

      my $tmp2 = length $2 ? $2 : '';
      Or, if you have the defined-or patch installed, something like:
      my $tmp2 = $2 // '';

      Liz

        Hi, I am getting the error "Use of unintialized value in pattern match(m//) in the below line. if ($line =~ /^element \s*(\S+)\s* \S+\s* -mkbranch\s* (\S+)/) { If anyone knows, please tell me how to proceed on this. Thanks in advance, Kavitha
      Thanks, I will cleanup my code and also see what the wonderful world of \Q gives me.
      ___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-03-19 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found