LogReader.pl

Discussion in 'Perl Misc' started by Nikos, Apr 29, 2005.

  1. Nikos

    Nikos Guest

    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
    #
    ###############################################################################
    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 <>


    I hope you find it usefull! :)
    Nikos, Apr 29, 2005
    #1
    1. Advertising

  2. Nikos

    John Bokma Guest

    Nikos wrote:

    > #!/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)


    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Apr 29, 2005
    #2
    1. Advertising

  3. Nikos

    Brian Wakem Guest

    John Bokma wrote:

    > Nikos wrote:
    >
    >> #!/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!


    --
    Brian Wakem
    Brian Wakem, Apr 29, 2005
    #3
  4. Nikos

    John Bokma Guest

    Brian Wakem wrote:

    > John Bokma wrote:
    >
    >>> 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!


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

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Apr 29, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page