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


In reply to Re: Re: Re: Use of uninitialized value in pattern match (m//) by davido
in thread Use of uninitialized value in pattern match (m//) by wolis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.