develooper Front page | perl.beginners | Postings from October 2009

Not following the match problem in this simplified example

Thread Next
From:
Harry Putnam
Date:
October 28, 2009 09:37
Subject:
Not following the match problem in this simplified example
Message ID:
871vknlbkc.fsf@newsguy.com

What follows is snippet out of a more complex script and reworked to
simplify but still not seeing what causes the problem.

I'm not sure if I've just stared at it too long or what.  When I run it
I get this error:

    Use of uninitialized value in pattern match (m//) at ./match.pl
    line 14.  No match here

I'm far less than skilled with the perl debugger but walking thru with
it, I tested by way or `print', if both variables have value before
the match, and of course they do.  I've looked hard for a mis-spelling
but don't see one.

Just not seeing the problem... Is it a complete misuse of `=~' ? or
what?

I realize I could forgo the qr// compilation but should that be necessary?
In the actual script its used 20-100 times on each of thousands of
files. (the program scans only the headers of mail messages)

And shouldn't the expressions match? Or does !~ signify an EXACT
match?  I understood, it signifies a regex match in which case
`Date:' should match `Date: some date'... shouldn't it?

-------        ---------       ---=---       ---------      -------- 

#!/usr/local/bin/perl

use strict;
use warnings;

my $date_re = qr/^Date:/;
my $other_re = qr/^Date: some date/;

if (/$date_re/ !~ /$other_re/){
   print "No match here\n"
}else{
   print "It's a match\n";
} 


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About