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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.