in reply to unless condition not working

See quotemeta, Regex::PreSuf, and read How do I post a question effectively?
my $line = 'fee fi fo fum'; my $pat = qr/i smell the blood/; unless( grep /$pat/, $line ){ print "of an englishman\n"; } __END__

Prints "of an englishman", because $line doesn't match $pat

That condition is more commonly written as one of these

unless( $line =~ /$pat/ ) { ... if( not $line =~ /$pat/ ) { ... if( $line !~ /$pat/ ) { ...

Replies are listed 'Best First'.
Re^2: unless condition not working
by perlavi (Initiate) on Dec 05, 2011 at 06:53 UTC

    Thanks for the reply. I used your recommendation to use qr but unfortunately it still has issues concerning to quotemeta related lines. My txt file is a combination of normal as well as quotemeta related lines. So at a time I am able to get output either for normal lines or quotemeta lines. Please correct me if I doing something wrong.

    unless($line =~ qr/$pattern/){ ... #works only for normal lines unless($line =~ qr/\Q$pattern\E/){... #both normal as well as quotemet +a related lines are failing and everything is getting list in output.

    Could you please let me know what is wrong step I am doing?

      Could you please let me know what is wrong step I am doing?

      You're using the qr operator as if it were the m operator (match), but they're not the same. See Regexp Quote-Like Operators in perlop

      And you only quotemeta arbitrary text you wish to match verbatim, not regex patterns.

      You can build an efficient regex pattern out of a list of arbitrary text with Regex::PreSuf.