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

I have this code that I took out of Learning Perl. In this example I used the while loop to check that my input with the regular expression. However, why is it that when I take the while loop away and try alternate methods of comparing data to the regular expression it will not compile it gives me a message that says: ---use of uninitialized value in pattern match (m//) What are alternate methods of comparing a value to the regular expression in the if statement???
#!/usr/bin/perl -w use strict; #get the user input first print "type a string: "; while(<>){ chomp(); if(/Wilma.*Fred|Fred.*Wilma/){ print "$_\n"; } else{ print "\n\nyour line was skipped because it did not mention Fred and W +ilma\n\n"; } }

Edit by tye

Replies are listed 'Best First'.
Re: While loop and pattern match (was Re: need help)
by VSarkiss (Monsignor) on Jun 05, 2002 at 22:33 UTC

    Both your while loop and your pattern match are using $_ implicitly. This line: while (<>) {is equivalent to: while (defined($_ = <>)) {Similarly, if (/foo/) {is equivalent to: if ($_ =~ /foo/) {Change to using an explicit variable and you should be OK. Look at perlsyn for more details.

      thanks VSarkiss
        Or you can keep the implicit match, if you add the explicit assignement to $_ before. :-) Which sometimes makes the code briefer and therefor easier to read, and sometimes makes it too short in the wrong place and therefor harder to read. (As a beginner, you should probably stick to the explicit syntax for a while.)

        Makeshifts last the longest.