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:
...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.my $tmp2 = ""; $tmp2 = $2;
is better written as:if($tmp2 =~ m/\'/) { $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.$tmp2 =~ s/'/\\'/g; # escape the single quotes..
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
|
---|
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 | |
by Anonymous Monk on Jun 26, 2009 at 11:59 UTC | |
by davorg (Chancellor) on Jun 26, 2009 at 12:15 UTC | |
Re: Re: Use of uninitialized value in pattern match (m//)
by wolis (Scribe) on Feb 10, 2004 at 04:38 UTC |
In Section
Seekers of Perl Wisdom