time diffrence between two lines in a log file

Hi Rado,

Thank You.
When i ran the script like this it gave me error--

C:\Perl Script>perl timediff.pl logfile
Use of uninitialized value in print at timediff.pl line 35, <LF> line 17.
start:
end: 2009-04-26T04:06:05
Can't call method "time_zone" on an undefined value at C:/Perl/lib/DateTime.pm l
ine 1231, <LF> line 17.

I put the DateTime module files in Perl Librray.
logfile is also in the directory where i am running the program.

Thanks
NT

When i ran the program i am getting error like

[root@tsrh4ent shell_perl_script]# ./timediff1.sh
./timediff1.sh: line 25: [: -: integer expression expected
./timediff1.sh: line 31: [: -: integer expression expected
./timediff1.sh: line 38: [: -: integer expression expected
00\c
./timediff1.sh: line 50: [: -: integer expression expected
-\c
./timediff1.sh: line 50: [: -: integer expression expected
-\c
./timediff1.sh: line 50: [: -: integer expression expected
-\c

Thanks
NT

Try this:

#!/usr/bin/env perl

use strict;
use warnings;
use DateTime;

my $logfile = shift || die "usage: $0 <filename>\n";

my %mon2num = qw(
  Jan 1  Feb 2  Mar 3  Apr 4  May 5  Jun 6
  Jul 7  Aug 8  Sep 9  Oct 10 Nov 11 Dec 12
);

my ( $start_dt, $end_dt );

open LF, $logfile or die "$logfile: $!\n";

while (<LF>) {    
    if (/(?:Entering|Exiting) ARRF_LIB_Evaluate/) {
        $end_dt = '';
        my @dtA = split;
        my @time = split /[:.]/, $dtA[3];
        ( /Exiting/ ? $end_dt : $start_dt ) = DateTime->new(
            year       => $dtA[4],
            month      => $mon2num{ $dtA[1] },
            day        => $dtA[2],
            hour       => $time[0],
            minute     => $time[1],
            second     => $time[2],
            nanosecond => $time[3],
        );

        if ($end_dt && $start_dt) {
            print "start: ", $start_dt, "\n";
            print "end: ",   $end_dt,   "\n";
            my $e = $end_dt->subtract_datetime($start_dt);
            printf
"elapsed: %s year(s), %s month(s), %s week(s), %s day(s), %s hour(s), %s min, %s sec, %s ms\n",
              $e->years, $e->months, $e->weeks, $e->days, $e->hours,
              $e->minutes, $e->seconds, $e->nanoseconds;

        }
    }
}

Hi Rado,

Thank You very much for looking into my problem and providing me the script. I tested it and it worked fine means it executed well without any error. I need to verify the time diffrences here and let you know.
I want to know some explanation here---

my $logfile = shift || die "usage: $0 <filename>\n";

What is the use of shift here, later part of the (OR ||) i know.

my ( $start_dt, $end_dt );

Is this fuction parameters. It does not have any name.

my @dtA = split;

what this line is going to do?

( /Exiting/ ? $end_dt : $start_dt ) = DateTime->new(

what this line explains?

my $e = $end_dt->subtract_datetime($start_dt);

Is subtract_datetime some standard function in DateTime.

Thanks in advance.

Thanks
NT

If the first argument passed to the script (i.e. the value returned by the shift function) is true in boolean context,
assign that value to the variable logfile, otherwise exit with error. If you name your input file 0, the code above wont work :slight_smile:

This is just a declaration, it's the same as:

my $start_dt;
my $end_dt;

Assign the list returned by the splitting of the $_ variable to the array dtA (date Array). split by default splits on whitespace (after skipping any leading whitespace).

It's the ternary operator: expression ? if true evaluate this:if false evaluate that.

If the current input record ($_) matches the pattern in // assign the returned object to the reference $end_dt, otherwise assign it to the reference $start_dt.

Yes, subtract_datetime is one of the available methods in the DateTime class.