The -n flag in your code creates an implied while loop which reads from STDIN. For the purpose of this post, I am using an explicit while loop. I have redirected STDIN to a memory file which contains the example data from
SyslogScan::SyslogEntry. I saved your regex in a variable to separate the matching and processing issues. You had the right idea about saving the match values, but you should use an array. It is both easier and clearer to use 'eq' rather than an regex to match a constant pattern. Note that at the end of the loop, $_ still contains the original line and @m contains all the original matches. I recommend that you write and debug your complete logic in this form. If necessary, you can rewrite it as the terse one-liner.
use strict;
use warnings;
use feature 'state';
my $syslog_names =
\"Jun 13 02:32:27 satellife in.identd[25994]: connect from mail.mi
+ssouri.edu\n";
close STDIN;
open STDIN, '<', $syslog_names or die "$!";
while (<>) {
state $systemname = 'in.identd';
state $regex =
qr/^
(
([a-zA-Z]+\s+[0-9]+ \s+ [0-9]+[:][0-9]+[:][0-9]+) \s+
([0-9a-zA-Z-]+) \s
([^:[]+)
(\[.*?\])? \s*
[:] \s* (?: ([^:]*)[:])? \s*
(.*)
) $
/x;
if (m/$regex/) {
my @m = ($1, $2, $3, $4, $5, $6, $7);
print "$m[3]\n$_\n" if $m[3] eq "$systemname";
}
}
OUTPUT:
in.identd
Jun 13 02:32:27 satellife in.identd[25994]: connect from mail.missouri
+.edu