Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

by Trimbach (Curate)
on Feb 10, 2004 at 04:15 UTC ( #327810=note: print w/ replies, xml ) Need Help??

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

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

Comment on Re: Use of uninitialized value in pattern match (m//)
Select or Download Code
Replies are listed 'Best First'.
Re: Re: Use of uninitialized value in pattern match (m//)
by liz (Monsignor) on Feb 10, 2004 at 09:49 UTC
    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 // '';


      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

        You probably want to start by posting a new thread rather than hijacking a five year old discussion.

        The perldiag man page explains what all Perl error and warning messages mean. For your error, it says this:

        Use of uninitialized value%s

        (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.

        To help you figure out what was undefined, perl will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo , and the warning will refer to the concatenation (.) operator, even though there is no . in your program.

        There appears to be only one variable used in your statement. That'll be the one that's undefined.


        See the Copyright notice on my home node.

        Perl training courses

Re: Re: Use of uninitialized value in pattern match (m//)
by wolis (Scribe) on Feb 10, 2004 at 04:38 UTC
    Thanks, I will cleanup my code and also see what the wonderful world of \Q gives me.
    ___ /\__\ "What is the world coming to?" \/__/

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://327810]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2015-11-27 00:41 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (710 votes), past polls