LogReader.pl

N

Nikos

Here is the Perl source of a script a made to read irc logs.
============================================================

#!/usr/bin/perl

use MyConsole qw:)SUBS);

$|++;
$MC = new MyConsole;
&Clear;

@colors = qw(9 7 13 5 11 15);

print "Enter the log name you wish to view => ";
$filename = <STDIN>;
chomp ($filename);

open (LOGFILE, $filename) or die $!;

while (<LOGFILE>) {
($user) = /^<([^>]*)>/;

if (!$userhash{$user}) {
$userhash{$user} = $colors[0];
push(@colors, shift(@colors));
}

DoPrint("$userhash{$user}");

for $foo (split(//, $_)) {
for(1..300000) {};
DoPrint($foo);
}

DoPrint("0");
$line++;

if ($line == 22) {
print "\a--more--$/";
<STDIN>;
if ($^O eq 'MSWin32') { system("cls") } else { system("clear") };
$line = 0;
}
}

close (LOGFILE);
<STDIN>;
if ($^O eq 'MSWin32') { system("cls") } else { system("clear") };
print "\a\a!!! Made by Nikos !!!";




======================================================================
And here is the MyConsole.pm module needed by my LogReader.pl for
implementing colored text in console mode. This is not made by me but
from a guy called Ani from irc Undernet on #cgi
======================================================================



#!/usr/bin/perl
###############################################################################
# Displaying text in color.
#
#
#
# MODE 1: ANSI
#
# 2: Console
#
# 3: Autodetect: Windows: 2, Unix: 1
#
# default: ANSI
#
#
#
# 22/03/03
(e-mail address removed) #
###############################################################################
package MyConsole;

$VERSION = '1.00';

use Exporter;

use base qw(Exporter);
use vars qw(@EXPORT_OK %EXPORT_TAGS $FG_RED);

my @subs = qw(&Clear &DoPrint);
@EXPORT_OK = (@subs);
%EXPORT_TAGS = (SUBS => \@subs, );


our ($MODE) = 0;

my ($Command) = 'clear';
my ($C);

my (@Ansi) = (color("reset"), color("black"), color("blue"), color("blue
bold"), color("red"), color("red bold"), color("green"), color("green
bold"), color("magenta"), color("magenta bold"), color("cyan"),
color("cyan bold"), color("yellow"), color("yellow bold"),
color("white"), color("white bold"));
my (@console) = ("main::ATTR_NORMAL","main::FG_BLACK", "main::FG_BLUE",
"main::FG_LIGHTBLUE", "main::FG_RED", "main::FG_LIGHTRED",
"main::FG_GREEN", "main::FG_LIGHTGREEN", "main::FG_MAGENTA",
"main::FG_LIGHTMAGENTA", "main::FG_CYAN", "main::FG_LIGHTCYAN",
"main::FG_BROWN", "main::FG_YELLOW", "main::FG_GRAY", "main::FG_WHITE");

sub new {
my ($self) = shift;

if ($^O =~ /Win/i) {
$Command = 'cls';
if ($MODE ne 1) { $MODE = 2; }
}

if ($MODE eq 2) {
require Win32::Console;
$C = new Win32::Console;
}
else {
use Term::ANSIColor;
}
$self = {};
bless $self;
return $self;
}


sub Clear {
if ($MODE eq 2) {
$C->Cls;
$C->Display;
}
else { system($Command); }
}

sub DoPrint ($) {
my ($S) = $_[0];
$S =~ s/\\ /\0/g;

if ($MODE eq 2) {
my (@L) = split (/\/, $S);
my ($a);
foreach $a (@L) {
my ($Color) = "";
my ($b) = substr($a, 0, 2);
if ($b ge 10 and $b lt 16) {
$Color = $Console[$b];
$a = substr($a, 2, length($a) - 2);
}
else {
$b = substr($a, 0, 1);
if ($b ge 0 and $b le 9) {
$Color = $Console[$b];
$a = substr($a, 1, length($a) - 1);
}
}
if ($Color ne '') { $C->Attr($$Color); }
$C->Write($a);
$C->Display;
}
}
else {
$S =~ s/\([0-9][0-9])/$Ansi[$1]/g;
$S =~ s/\([0-9])/$Ansi[$1]/g;
print $S;
}
}

sub Demo {
for (my($i)=0;$i<16;$i++) {
&DoPrint("Demo $i:". (" " x (3 - length($i)))
.."".$i."aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0\n");
}
}

1;

__END__

Example:
use MyConsole qw:)SUBS);

$MC = new MyConsole;
&Clear;

$MC->Demo;
&DoPrint("Demo.");

Demo:
Use the sub Demo to get a list of all the colors (as shown in the
example)

Author:
Animator <[email protected]>


I hope you find it usefull! :)
 
J

John Bokma

Nikos said:
#!/usr/bin/perl

use strict;
use warnings;
&Clear; ^


open (LOGFILE, $filename) or die $!;

while (<LOGFILE>) {
($user) = /^<([^>]*)>/;

if (!$userhash{$user}) {

I would use something like:

unless ( exists $userhash{ $user } ) {

DoPrint("$userhash{$user}");
^
Don't use funny characters like that.

for $foo (split(//, $_)) {
for(1..300000) {};

^^^^^ silly, I can even
imagine Perl just drops that. Use sleep, or
something related.
DoPrint($foo);

Camel Case is weird for subs (IMNSHO)
 
B

Brian Wakem

John said:
Nikos said:
#!/usr/bin/perl

use strict;
use warnings;
&Clear; ^


open (LOGFILE, $filename) or die $!;

while (<LOGFILE>) {
($user) = /^<([^>]*)>/;

if (!$userhash{$user}) {

I would use something like:

unless ( exists $userhash{ $user } ) {

DoPrint("$userhash{$user}");
^
Don't use funny characters like that.

for $foo (split(//, $_)) {
for(1..300000) {};

^^^^^ silly, I can even
imagine Perl just drops that. Use sleep, or
something related.


I remember using code like that for pauses in BASIC on an old Amstrad CPC464
when I was about 8!
 
J

John Bokma

Brian said:
I remember using code like that for pauses in BASIC on an old Amstrad
CPC464 when I was about 8!

What !? You didn't use all those precious clock cycles to calculate another
Mandelbrot?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top