| 1 | package ApacheLog::Parser; | ||||||||||||
| 2 | 1 | $VERSION = v0.0.1; | |||||||||||
| 3 | |||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | =head1 NAME | ||||||||||||
| 9 | |||||||||||||
| 10 | ApacheLog::Parser - parse apache 'combined' log format | ||||||||||||
| 11 | |||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||
| 13 | |||||||||||||
| 14 | use ApacheLog::Parser qw(parse_line); | ||||||||||||
| 15 | # imports the field constants too | ||||||||||||
| 16 | |||||||||||||
| 17 | my %visitors; | ||||||||||||
| 18 | while(my $line = <$fh>) { | ||||||||||||
| 19 | chomp($line); | ||||||||||||
| 20 | my $ref = parse_line($line); | ||||||||||||
| 21 | ($visitors{$ref->[client]} ||= 0)++; | ||||||||||||
| 22 | } | ||||||||||||
| 23 | no ApacheLog::Parser; # cleans-out the constants | ||||||||||||
| 24 | |||||||||||||
| 25 | print join("\n ", 'visitors:', | ||||||||||||
| 26 | map({$_ . ': ' . $visitors{$_}} keys(%visitors))), "\n"; | ||||||||||||
| 27 | |||||||||||||
| 28 | |||||||||||||
| 29 | =cut | ||||||||||||
| 30 | |||||||||||||
| 31 | 1 | my @fields; | |||||||||||
| 32 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | 1 | @fields = qw( | |||||||||||
| 34 | client | ||||||||||||
| 35 | login | ||||||||||||
| 36 | dtime | ||||||||||||
| 37 | request | ||||||||||||
| 38 | file | ||||||||||||
| 39 | params | ||||||||||||
| 40 | proto | ||||||||||||
| 41 | code | ||||||||||||
| 42 | bytes | ||||||||||||
| 43 | refer | ||||||||||||
| 44 | agent | ||||||||||||
| 45 | ); | ||||||||||||
| 46 | } | ||||||||||||
| 47 | 1 | ▶ | use constant ({map({$fields[$_] => $_} 0..$#fields)}); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | |||||||||||||
| 49 | ######################################################################## | ||||||||||||
| 50 | # I-hate-exporter overhead | ||||||||||||
| 51 | my %exported; | ||||||||||||
| 52 | 12 | ▶ | my $do_export = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||
| 53 | 12 | my $package = shift; | |||||||||||
| 54 | 12 | my ($caller, $function) = @_; | |||||||||||
| 55 | |||||||||||||
| 56 | 12 | ▶ | my $track = $exported{$package} ||= {}; | ||||||||||
|
Condition100%2/2
$exported{$package} ||= {}
MC/DC100%1/1
$exported{$package} ||= {}
$exported{$package}
Truth table100%2/2
A
A= $exported{$package}
| |||||||||||||
| 57 | 12 | ▶ | $track = $track->{$caller} ||= {}; | ||||||||||
|
Condition100%2/2
$track->{$caller} ||= {}
MC/DC100%1/1
$track->{$caller} ||= {}
$track->{$caller}
Truth table100%2/2
A
A= $track->{$caller}
| |||||||||||||
| 58 | |||||||||||||
| 59 | 12 | $track->{$function} = 1; | |||||||||||
| 60 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 12 | ▶ | *{$caller . '::' . $function} = $package->can($function) or | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | croak("cannot $function"); | ||||||||||||
| 63 | 1 | }; | |||||||||||
| 64 | |||||||||||||
| 65 | 1 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=5 SCAR=16.2
| |||||||||||||
| 66 | 1 | my $package = shift; | |||||||||||
| 67 | 1 | my $caller = caller; | |||||||||||
| 68 | 1 | my %args = map({$_ => 1} @_); | |||||||||||
| 69 | |||||||||||||
| 70 | # DWIM bits | ||||||||||||
| 71 | 1 | ▶ | if($args{parse_line}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 1 | $args{':fields'} = 1; | |||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | # exports | ||||||||||||
| 76 | 1 | ▶ | if(delete($args{':fields'})) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 1 | $package->$do_export($caller, $_) for(@fields); | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 1 | foreach my $func (keys(%args)) { | |||||||||||
| 81 | 1 | $package->$do_export($caller, $func); | |||||||||||
| 82 | } | ||||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | =head2 unimport | ||||||||||||
| 86 | |||||||||||||
| 87 | Allows 'no ApacheLog::Parser' to cleanup your namespace. | ||||||||||||
| 88 | |||||||||||||
| 89 | =cut | ||||||||||||
| 90 | |||||||||||||
| 91 | 0 | ▶ | sub unimport { | ||||||||||
|
Subroutine
unimport: not called CC=4 SCAR=24.6
| |||||||||||||
| 92 | 0 | my $package = shift; | |||||||||||
| 93 | 0 | my $caller = caller; | |||||||||||
| 94 | |||||||||||||
| 95 | 0 | ▶ | my $track = $exported{$package} ||= {}; | ||||||||||
|
Condition0%0/2
$exported{$package} ||= {}
MC/DC0%0/1
$exported{$package} ||= {}
$exported{$package}
Truth table0%0/2
A
A= $exported{$package}
| |||||||||||||
| 96 | 0 | ▶ | $track = $track->{$caller} ||= {}; | ||||||||||
|
Condition0%0/2
$track->{$caller} ||= {}
MC/DC0%0/1
$track->{$caller} ||= {}
$track->{$caller}
Truth table0%0/2
A
A= $track->{$caller}
| |||||||||||||
| 97 | 0 | foreach my $func (keys(%$track)) { | |||||||||||
| 98 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 99 | 0 | delete(${$caller . '::'}{$func}); | |||||||||||
| 100 | } | ||||||||||||
| 101 | } | ||||||||||||
| 102 | ######################################################################## | ||||||||||||
| 103 | |||||||||||||
| 104 | # TODO document this as an interface? | ||||||||||||
| 105 | 1 | our $regexp = qr/^ | |||||||||||
| 106 | ([^ ]+)\ +([^ ]+)\ +([^\[]+)\ + # client, ruser, login | ||||||||||||
| 107 | \[([^\]]+)\]\ + # date | ||||||||||||
| 108 | "(.*)"\ +(\d+)\ +(\d+|-)\ + # req, code, bytes | ||||||||||||
| 109 | "(.*)"\ +"(.*)" # refer, agent | ||||||||||||
| 110 | $/x; | ||||||||||||
| 111 | |||||||||||||
| 112 | =head2 parse_line | ||||||||||||
| 113 | |||||||||||||
| 114 | Assumes an already chomp()'d $line. | ||||||||||||
| 115 | |||||||||||||
| 116 | my $array_ref = parse_line($line); | ||||||||||||
| 117 | |||||||||||||
| 118 | =cut | ||||||||||||
| 119 | |||||||||||||
| 120 | 0 | ▶ | sub parse_line { | ||||||||||
|
Subroutine
parse_line: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||
| 121 | 0 | my ($line) = @_; | |||||||||||
| 122 | |||||||||||||
| 123 | 0 | my @v; | |||||||||||
| 124 | my $req; | ||||||||||||
| 125 | |||||||||||||
| 126 | 0 | ▶ | $line =~ $regexp or die "failed to parse $line"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | 0 | (@v[client, login, dtime], $req, @v[code, bytes, refer, agent]) = | |||||||||||
| 128 | ($1, $3, $4, $5, $6, $7, $8, $9); | ||||||||||||
| 129 | |||||||||||||
| 130 | 0 | ▶ | $v[code] or die "no code in $line (@v)"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | |||||||||||||
| 132 | 0 | $req =~ s/^(?:([A-Z]+) +)?//; | |||||||||||
| 133 | 0 | ▶ | $v[request] = $1 || ''; # ouch, a non-request (telnet) hack | ||||||||||
|
Condition0%0/2
$1 || ''
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
| |||||||||||||
| 134 | # just tear this off the end | ||||||||||||
| 135 | 0 | ▶ | $v[proto] = ($req =~ s# +(HTTP/\d+\.\d+)$##) ? $1 : ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | |||||||||||||
| 137 | 0 | @v[file, params] = split(/\?/, $req, 2); | |||||||||||
| 138 | 0 | ▶ | defined($v[$_]) or $v[$_] = '' for(file, params); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | 0 | $v[params] =~ s/\\"/"/g; | |||||||||||
| 140 | |||||||||||||
| 141 | ($v[$_] eq '-') and $v[$_] = '' | ||||||||||||
| 142 | 0 | ▶ | for(login, request, code, refer, agent); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 0 | ▶ | $v[bytes] = 0 if($v[bytes] eq '-'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | |||||||||||||
| 145 | 0 | return(\@v); | |||||||||||
| 146 | } # end subroutine parse_line definition | ||||||||||||
| 147 | ######################################################################## | ||||||||||||
| 148 | |||||||||||||
| 149 | =head2 parse_line_to_hash | ||||||||||||
| 150 | |||||||||||||
| 151 | Is a little more elegant interface than the array-ref and constants used | ||||||||||||
| 152 | in parse_line(), but you pay dearly for passing-around those hash keys. | ||||||||||||
| 153 | Fun for one-off stuff, but not recommended for heavy lifting. | ||||||||||||
| 154 | |||||||||||||
| 155 | my %hash = parse_line_to_hash($line); | ||||||||||||
| 156 | |||||||||||||
| 157 | =cut | ||||||||||||
| 158 | |||||||||||||
| 159 | 0 | ▶ | sub parse_line_to_hash { | ||||||||||
|
Subroutine
parse_line_to_hash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 160 | 0 | my @keys = @fields; | |||||||||||
| 161 | 0 | return(map({shift(@keys) => $_} @{parse_line($_[0])})); | |||||||||||
| 162 | } # end subroutine parse_line_to_hash definition | ||||||||||||
| 163 | ######################################################################## | ||||||||||||
| 164 | |||||||||||||
| 165 | =head1 AUTHOR | ||||||||||||
| 166 | |||||||||||||
| 167 | Eric Wilhelm @ <ewilhelm at cpan dot org> | ||||||||||||
| 168 | |||||||||||||
| 169 | http://scratchcomputing.com/ | ||||||||||||
| 170 | |||||||||||||
| 171 | =head1 BUGS | ||||||||||||
| 172 | |||||||||||||
| 173 | If you found this module on CPAN, please report any bugs or feature | ||||||||||||
| 174 | requests through the web interface at L<http://rt.cpan.org>. I will be | ||||||||||||
| 175 | notified, and then you'll automatically be notified of progress on your | ||||||||||||
| 176 | bug as I make changes. | ||||||||||||
| 177 | |||||||||||||
| 178 | If you pulled this development version from my /svn/, please contact me | ||||||||||||
| 179 | directly. | ||||||||||||
| 180 | |||||||||||||
| 181 | =head1 COPYRIGHT | ||||||||||||
| 182 | |||||||||||||
| 183 | Copyright (C) 2007 Eric L. Wilhelm, All Rights Reserved. | ||||||||||||
| 184 | |||||||||||||
| 185 | =head1 NO WARRANTY | ||||||||||||
| 186 | |||||||||||||
| 187 | Absolutely, positively NO WARRANTY, neither express or implied, is | ||||||||||||
| 188 | offered with this software. You use this software at your own risk. In | ||||||||||||
| 189 | case of loss, no person or entity owes you anything whatsoever. You | ||||||||||||
| 190 | have been warned. | ||||||||||||
| 191 | |||||||||||||
| 192 | =head1 LICENSE | ||||||||||||
| 193 | |||||||||||||
| 194 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 195 | under the same terms as Perl itself. | ||||||||||||
| 196 | |||||||||||||
| 197 | =cut | ||||||||||||
| 198 | |||||||||||||
| 199 | # vi:ts=2:sw=2:et:sta | ||||||||||||
| 200 | 1 | 1; | |||||||||||