Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex : Use of unintialized value $_ in pattern match

Status
Not open for further replies.

Cambridge10

Programmer
May 5, 2008
18
NL
Hi,

I'm new to Perl using regex.
I wrote a simple program which search the userinput for a match.
When I start the program it works fine but I get the error message below.
I'm doing something wrong but what exactly?

The error message :
Use of unintialized value $_ in pattern match (m//) at test.pl line 8, <STDIN> line 1.


The code :
Code:
#!/usr/bin/perl -w

print "enter any word: ";
$userinput=<STDIN>;
chomp $userinput;


if ($userinput = /[A-Z]/) 
  {
    print " matched the pattern $userinput\n";
  }
else
  {
    print "No match";
  }
 
if ($userinput = /[A-Z]/)

should be

if ($userinput =~ /[A-Z]/);
 
max1x showed you the problem. Your syntax is actually a shortcut for this:

$userinput = $_ =~ /[A-Z]/;

hench the error about $_ not being initialized. Remember, "=" is the assingment operator, regexp binding operators include "~":

=~ (equals)
!~ (not equals)

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I also noticed it's sensitive for spaces but now I understand.
Thanks for the explanation guys [thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top