Search code examples
apacheperlparsingcpan

Using Time::Piece with Apache::Log::Parser


I am using Apache::Log::Parser to parse Apache log files.

I extracted the date from log file using the following code.

my $parser = Apache::Log::Parser->new(fast=>1);
my $log = $parser->parse($data);
$t = $log->{date};

Now,I tried to use Time::Piece to parse dates, but I'm unable to do it.

print "$t->day_of_month";

But, it's not working. How to use Time::Piece to parse date?


Solution

  • You cannot call methods on objects inside of string interpolation. It will probably output something like this:

    Sat Feb 18 12:44:47 2017->day_of_month
    

    Remove the double quotes "" to call the method.

    print $t->day_of_month;
    

    Now the output is:

    18
    

    Note that you need to create a Time::Piece object with localtime or gmtime if you have an epoch value in your log, or using strptime if the date is some kind of timestamp.