TheNoob TheNoob - 4 years ago 99
Perl Question

Used strict and warning but still no error messages when there is supposed to be a mistake somewhere

I have a program that basically opens and reads the syslog in ubuntu and use a keyword text file to search for specific words in the syslog and have that to be outputted into an output text file. Problem is, it was all going well until I can't seem to get any errors anymore which doesn't really help me as there is a mistake somewhere which makes me not see any output at all. I ran my regex using a perl online testing tool and it works but doesnt seem so when I try to run it on the terminal. I would be glad if someone could help me with this.

Here is my code:


use strict;
use warnings;

my $syslogFile = 'syslog';
open (my $syslogInfo, '<', $syslogFile) or die "Could not open $syslogFile";

my $keywordFile = 'keyword';
open (my $keywordInfo, '<', $keywordFile) or die "Could not open $keywordFile";

while (my $line = <$syslogInfo>)

if($line =~ m/[a-zA-Z]\s(\d{1-31})\s(\d{1-24}):(\d{1-59}):(\d{1-59})\s[a-zA-Z]\s($keywordInfo).\ni/)
open(outputFile, ">>output");

flock(outputFile, 2);

print outputFile "$line\n";

Edit: Here is a sample of what is in the syslog

Dec 29 22:02:28 osboxes NetworkManager[686]: plen 24 (

Answer Source

You include $keywordInfo in your regex. But $keywordInfo is a filehandle, which means that, when stringified, it will look like "GLOB(0x1819f30)". I suspect that's not the string that you're looking for.

You know how to read data from a filehandle. You're doing that with your other filehandle, $syslogInfo. So you just need to do that with this filehandle too. Something like this:

my $string_to_find = <$keywordInfo>;
chomp $keywordInfo;

# Now use $string_to_find in your regex.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download