http://www.perlmonks.org?node_id=585156


in reply to Re^3: trouble parsing log file...
in thread trouble parsing log file...

You need to look at my example more closely.

With regular expressions, you can't just set a string to $error=(/DOWN/), and then check whether your line matches it with if ($line eq $error) {.

You need to use the regular expression match operator =~ to see whether your string matches the pattern:

if ($line =~ /DOWN/) { # ... } elsif ($line =~ /PROBLEM/) { # ... }

Now, it's true that you can abstract some of this.  For example, you could assign string variables, and then use pattern matching on those variables:

my $error = "DOWN"; my $warn = "PROBLEM"; # ... and later ... if ($line =~ /$error/) { # ... } elsif ($line =~ /$warn/) { # ... }

Which in the above case should work fine.

But be aware that, if your variable contains any characters which have special meaning within regular expressions, those meanings will continue to apply.

For example, if the string you're trying to match is literally "Server ... DOWN", and you attempt to assign and match against that string with:

my $error = "Server ... DOWN"; if ($line =~ /$error/) { # conditional clause }

then you'll be using the special meaning of "." within a regular expression (that of matching any character other than end-of-line), and your conditional will thus also match strings like the hypothetical "Server Not DOWN".


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/