D
Dave Saville
I have a general purpose CGI script to interogate various log files on
my server. Because sometimes I want to limit what I see I have an
optional field where I can type a regular expression.
If the field is blank then obviously nothing special needs to be done,
but if there is something then I need to build a regex. If the field
is just for example abc then I need m/abc/ generated - or something
that will do that. OTH if the field has /abc/i then I need m/abc/i and
so on.
Things are further complicated in that if there is a regular
expression then I want that matching text highlighted in the output.
What I have follows - but it does not always work. For example if I
have /o/g it does not highlight all the o's.
sub getlog
{
my $logfile = shift;
open LOG, "<$logfile" or die "Can't open $logfile $!";
local $regex = $regex;
if ( $regex )
{
if ( $regex =~ m/^(.)(.*)\1([giomsx]+)$/ )
{
my $body = $2;
my $mod = $3;
if ( $mod )
{
$regex = qr/(?$mod)$body/;
}
else
{
$regex = qr/$body/;
}
}
}
while ( <LOG> )
{
s/\</</gm;
s/\>/>/gm;
s/\xf8/°/gm;
if ( $regex )
{
push @log, $_ if s/($regex)/\<span
class="searchlight"\>$1\<\/span\>/;
}
else
{
push @log, $_;
}
}
close LOG;
}
TIA
my server. Because sometimes I want to limit what I see I have an
optional field where I can type a regular expression.
If the field is blank then obviously nothing special needs to be done,
but if there is something then I need to build a regex. If the field
is just for example abc then I need m/abc/ generated - or something
that will do that. OTH if the field has /abc/i then I need m/abc/i and
so on.
Things are further complicated in that if there is a regular
expression then I want that matching text highlighted in the output.
What I have follows - but it does not always work. For example if I
have /o/g it does not highlight all the o's.
sub getlog
{
my $logfile = shift;
open LOG, "<$logfile" or die "Can't open $logfile $!";
local $regex = $regex;
if ( $regex )
{
if ( $regex =~ m/^(.)(.*)\1([giomsx]+)$/ )
{
my $body = $2;
my $mod = $3;
if ( $mod )
{
$regex = qr/(?$mod)$body/;
}
else
{
$regex = qr/$body/;
}
}
}
while ( <LOG> )
{
s/\</</gm;
s/\>/>/gm;
s/\xf8/°/gm;
if ( $regex )
{
push @log, $_ if s/($regex)/\<span
class="searchlight"\>$1\<\/span\>/;
}
else
{
push @log, $_;
}
}
close LOG;
}
TIA