vasuperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a set of test cases. In each test case i need to parse a log file. So for parsing a file and searching for some patterns i would like to write a pl file or a module so that it can be reused to all other test cases. The patterns in each test case is different. I would like to generalise the parsing of different files with different patterns in each testcase. Can any one help me in this? Thanks in advance.

  • Comment on How to parse the logs in a generic manner

Replies are listed 'Best First'.
Re: How to parse the logs in a generic manner
by GrandFather (Saint) on Jan 02, 2015 at 08:21 UTC

    Yes, we can help you with that.

    Perl is the programming world's equivalent of English
      Then can u suggest me how to parse those logs in a generic manner so that the code should not be repititive in every .pl file. Thanks in advance

        Ask yourself what steps are required to achieve your goal. Write them down. Work through them in order using example data to ensure your method works. If you find problems alter your steps to cater for them. Once it works on paper implement your solution in perl.

Re: How to parse the logs in a generic manner
by karlgoethebier (Abbot) on Jan 02, 2015 at 16:05 UTC

    Write a module:

    package Vasuperl; use strict; use warnings; use Exporter; use vars qw($VERSION @ISA @EXPORT_OK ); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT_OK = qw(parse_log); sub parse_log { my ( $log_file, $pattern ) = @_; # parse your log # and return something useful } 1;

    Use it:

    #!/usr/bin/env perl use strict; use warnings; use lib q(.); use Vasuperl qw (parse_log); my $log_file = shift || die $!; my $pattern = qr(.); my $result = parse_log( $log_file, $pattern ); # do something with $result __END__

    Update: Changed legacy code on special request by choroba:

    What is so difficult with this that you can't figure it out your self?

    Karl

    «The Crux of the Biscuit is the Apostrophe»

      Manually changing @ISA is not needed. Just use
      use Exporter qw{ import };

      Also, use vars can be replaced by prefixing the variable assignments with our:

      our $VERSION = 1.01; our @EXPORT_OK = qw{ parse_log };
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        "...changing @ISA is not needed...prefixing the variable assignments with our..."

        Sure, you are right. Thank you for clarifying this.

        It seems that i'm a bit "outdated", right?

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: How to parse the logs in a generic manner
by thargas (Deacon) on Jan 02, 2015 at 19:09 UTC

    The problem with this question/request is that it is not sufficiently defined. You say you want to "parse a log file". What might that file look like? What do you want the results of parsing it to be? Your request is so vague that no-one here knows what you want. Give some sample log lines and what you would want to produce from them and you might get more useful responses than "yes we can help you". Until then, I'd have to say "No we can't help you."

Re: How to parse the logs in a generic manner
by karlgoethebier (Abbot) on Jan 02, 2015 at 19:03 UTC

    You've got a lot a helpful response. Now it would be nice to see your solution (AKA as your own effort).

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»