| 1 | 5 | ▶ | use 5.006; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | package Data::Freq::Record; | ||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | Data::Freq::Record - Represents a record added to Data::Freq counting | ||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | 5 | ▶ | use base 'Exporter'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | 5 | ▶ | use Date::Parse qw(str2time); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 5 | our @EXPORT_OK = qw(logsplit); | |||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | =head1 EXPORT | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | =head2 logsplit | ||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | Splits a text that represents a line in a log file. | ||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | use Data::Freq::Record qw(logsplit); | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | logsplit("12.34.56.78 - user1 [01/Jan/2012:01:02:03 +0000] "GET / HTTP/1.1" 200 44"); | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | # Returns an array with: | ||||||||||||||||||||||
| 30 | # [0]: '12.34.56.78' | ||||||||||||||||||||||
| 31 | # [1]: '-' | ||||||||||||||||||||||
| 32 | # [2]: '[01/Jan/2012:01:02:03 +0000]' | ||||||||||||||||||||||
| 33 | # [3]: '"GET / HTTP/1.1"' | ||||||||||||||||||||||
| 34 | # [4]: '200' | ||||||||||||||||||||||
| 35 | # [5]: '44' | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | A log line is typically whitespace-separated, while anything inside | ||||||||||||||||||||||
| 38 | brackets C<[...]>, braces C<{...}>, parentheses C<(...)>, double quotes C<"...">, | ||||||||||||||||||||||
| 39 | or single quotes C<'...'> is considered as one chunk as a whole | ||||||||||||||||||||||
| 40 | even if whitespaces may be included inside. | ||||||||||||||||||||||
| 41 | |||||||||||||||||||||||
| 42 | The C<logsplit> function is intended to split such a log line into an array. | ||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | 34 | ▶ | sub logsplit { | ||||||||||||||||||||
|
Subroutine
logsplit: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 47 | 34 | my $log = shift; | |||||||||||||||||||||
| 48 | 34 | my @ret = (); | |||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | 34 | ▶ | push @ret, $1 while $log =~ m/ ( | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 51 | " (?: \\" | "" | [^"] )* " | | ||||||||||||||||||||||
| 52 | ' (?: \\' | '' | [^'] )* ' | | ||||||||||||||||||||||
| 53 | \[ (?: \\[\[\]] | \[\[ | \]\] | [^\]] )* \] | | ||||||||||||||||||||||
| 54 | \( (?: \\[\(\)] | \(\( | \)\) | [^\)] )* \) | | ||||||||||||||||||||||
| 55 | \{ (?: \\[\{\}] | \{\{ | \}\} | [^\}] )* \} | | ||||||||||||||||||||||
| 56 | \S+ | ||||||||||||||||||||||
| 57 | ) /gx; | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | 34 | return @ret; | |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | =head1 METHODS | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | =head2 new | ||||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | Usage: | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | # Text | ||||||||||||||||||||||
| 69 | Data::Freq::Record->new("text"); | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | Data::Freq::Record->new("an input line from a log file\n"); | ||||||||||||||||||||||
| 72 | # Line break at the end will be stripped off | ||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | # Array ref | ||||||||||||||||||||||
| 75 | Data::Freq::Record->new(['an', 'array', 'ref']); | ||||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | # Hash ref | ||||||||||||||||||||||
| 78 | Data::Freq::Record->new({key => 'hash ref'}); | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | Constructs a record object, which carries an input data | ||||||||||||||||||||||
| 81 | in the form of a text, an array ref, or a hash ref. | ||||||||||||||||||||||
| 82 | Each form of the input (or a converted value) can be retrieved | ||||||||||||||||||||||
| 83 | by the L<text()|/text>, L<array()|/array>, or L<hash()|/hash> function. | ||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | When an array ref is required via the L</array>() method | ||||||||||||||||||||||
| 86 | while a text is given as the input, the array ref is created internally | ||||||||||||||||||||||
| 87 | by the L<logsplit()|/logsplit> function. | ||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | When a text is required via the L<text()|/text> method | ||||||||||||||||||||||
| 90 | while an array ref is given as the input, the text is taken | ||||||||||||||||||||||
| 91 | from the first element of the array. | ||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | The hash form is incompatible with the other forms, and whenever an incompatible | ||||||||||||||||||||||
| 94 | form is required, the return value is C<undef>. | ||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | If the text input has a line break at the end, it is stripped off. | ||||||||||||||||||||||
| 97 | If the line break should not be stripped off, use an array ref with the first element | ||||||||||||||||||||||
| 98 | set to the text. | ||||||||||||||||||||||
| 99 | |||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||
| 101 | |||||||||||||||||||||||
| 102 | 186 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||
| 103 | 186 | my ($class, $input) = @_; | |||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | 186 | my $self = bless { | |||||||||||||||||||||
| 106 | init => undef, | ||||||||||||||||||||||
| 107 | text => undef, | ||||||||||||||||||||||
| 108 | array => undef, | ||||||||||||||||||||||
| 109 | hash => undef, | ||||||||||||||||||||||
| 110 | date => undef, | ||||||||||||||||||||||
| 111 | date_tried => 0, | ||||||||||||||||||||||
| 112 | }, $class; | ||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | 186 | ▶ | if (!defined $input) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 115 | 0 | $self->{text} = ''; | |||||||||||||||||||||
| 116 | 0 | $self->{init} = 'text'; | |||||||||||||||||||||
| 117 | } elsif (!ref $input) { | ||||||||||||||||||||||
| 118 | 121 | $input =~ s/\r?\n$//; | |||||||||||||||||||||
| 119 | 121 | $self->{text} = $input; | |||||||||||||||||||||
| 120 | 121 | $self->{init} = 'text'; | |||||||||||||||||||||
| 121 | } elsif (ref $input eq 'ARRAY') { | ||||||||||||||||||||||
| 122 | 60 | $self->{array} = $input; | |||||||||||||||||||||
| 123 | 60 | $self->{init} = 'array'; | |||||||||||||||||||||
| 124 | } elsif (ref $input eq 'HASH') { | ||||||||||||||||||||||
| 125 | 5 | $self->{hash} = $input; | |||||||||||||||||||||
| 126 | 5 | $self->{init} = 'hash'; | |||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||
| 128 | 0 | croak "invalid argument type: ".ref($input); | |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 186 | return $self; | |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | =head2 text | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | Retrieves the text form of the input. | ||||||||||||||||||||||
| 137 | |||||||||||||||||||||||
| 138 | If the input was an array ref, the first element of the array is returned. | ||||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | 98 | ▶ | sub text { | ||||||||||||||||||||
|
Subroutine
text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 143 | 98 | my $self = shift; | |||||||||||||||||||||
| 144 | 98 | ▶ | return $self->{text} if defined $self->{text}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 145 | |||||||||||||||||||||||
| 146 | 4 | ▶ | if (defined $self->{array}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 147 | 2 | $self->{text} = $self->{array}[0]; | |||||||||||||||||||||
| 148 | 2 | return $self->{text}; | |||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | 2 | return undef; | |||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | =head2 array | ||||||||||||||||||||||
| 155 | |||||||||||||||||||||||
| 156 | Retrieves the array ref form of the input. | ||||||||||||||||||||||
| 157 | |||||||||||||||||||||||
| 158 | If the input was a text, it is split by the L<logsplit()|/logsplit> function.. | ||||||||||||||||||||||
| 159 | |||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | 158 | ▶ | sub array { | ||||||||||||||||||||
|
Subroutine
array: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 163 | 158 | my $self = shift; | |||||||||||||||||||||
| 164 | 158 | ▶ | return $self->{array} if defined $self->{array}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | 30 | ▶ | if (defined $self->{text}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 167 | 28 | $self->{array} = [logsplit $self->{text}]; | |||||||||||||||||||||
| 168 | 28 | return $self->{array}; | |||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | 2 | return undef; | |||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||
| 173 | |||||||||||||||||||||||
| 174 | =head2 hash | ||||||||||||||||||||||
| 175 | |||||||||||||||||||||||
| 176 | Retrieves the hash ref form of the input. | ||||||||||||||||||||||
| 177 | |||||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||||
| 179 | |||||||||||||||||||||||
| 180 | 5 | ▶ | sub hash { | ||||||||||||||||||||
|
Subroutine
hash: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 181 | 5 | my $self = shift; | |||||||||||||||||||||
| 182 | 5 | ▶ | return $self->{hash} if defined $self->{hash}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 183 | 2 | return undef; | |||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||
| 185 | |||||||||||||||||||||||
| 186 | =head2 date | ||||||||||||||||||||||
| 187 | |||||||||||||||||||||||
| 188 | Extracts a date/time from the input and returns the timestamp value. | ||||||||||||||||||||||
| 189 | |||||||||||||||||||||||
| 190 | The date/time is retrieved from the array ref form (or from a split text), | ||||||||||||||||||||||
| 191 | where the first element enclosed by a pair of brackets C<[...]> is | ||||||||||||||||||||||
| 192 | parsed by the L<Date::Parse::str2time()|Date::Parse/str2time> function. | ||||||||||||||||||||||
| 193 | |||||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||||
| 195 | |||||||||||||||||||||||
| 196 | 18 | ▶ | sub date { | ||||||||||||||||||||
|
Subroutine
date: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||
| 197 | 18 | my $self = shift; | |||||||||||||||||||||
| 198 | 18 | ▶ | return $self->{date} if $self->{date_tried}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 199 | |||||||||||||||||||||||
| 200 | 18 | $self->{date_tried} = 1; | |||||||||||||||||||||
| 201 | |||||||||||||||||||||||
| 202 | 18 | ▶ | my $array = $self->array or return undef; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 203 | |||||||||||||||||||||||
| 204 | 18 | ▶ | if (my $pos = shift) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 205 | 1 | my $str = "@$array[@$pos]"; | |||||||||||||||||||||
| 206 | 1 | $str =~ s/^ \[ (.*) \] $/$1/x; | |||||||||||||||||||||
| 207 | 1 | ▶ | return $self->{date} = $str if $str !~ /\D/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 208 | 1 | return $self->{date} = _str2time($str); | |||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||
| 210 | |||||||||||||||||||||||
| 211 | 17 | for my $item (@$array) { | |||||||||||||||||||||
| 212 | 44 | ▶ | if ($item =~ /^ \[ (.*) \] $/x) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 213 | 17 | my $t = _str2time($1); | |||||||||||||||||||||
| 214 | 17 | ▶ | return $self->{date} = $t if defined $t; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||
| 217 | |||||||||||||||||||||||
| 218 | 0 | return undef; | |||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||
| 220 | |||||||||||||||||||||||
| 221 | 18 | ▶ | sub _str2time { | ||||||||||||||||||||
|
Subroutine
_str2time: called CC=4 SCAR=13.9
| |||||||||||||||||||||||
| 222 | 18 | my $str = shift; | |||||||||||||||||||||
| 223 | |||||||||||||||||||||||
| 224 | 18 | ▶ | my $msec = $1 if $str =~ s/[,\.](\d+)$//; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 225 | 18 | my $t = str2time($str); | |||||||||||||||||||||
| 226 | 18 | ▶ | return undef unless defined $t; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 227 | |||||||||||||||||||||||
| 228 | 18 | ▶ | $t += "0.$msec" if $msec; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 229 | 18 | return $t; | |||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||
| 231 | |||||||||||||||||||||||
| 232 | =head1 AUTHOR | ||||||||||||||||||||||
| 233 | |||||||||||||||||||||||
| 234 | Mahiro Ando, C<< <mahiro at cpan.org> >> | ||||||||||||||||||||||
| 235 | |||||||||||||||||||||||
| 236 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||
| 237 | |||||||||||||||||||||||
| 238 | Copyright 2012 Mahiro Ando. | ||||||||||||||||||||||
| 239 | |||||||||||||||||||||||
| 240 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||
| 241 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||
| 242 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||
| 243 | |||||||||||||||||||||||
| 244 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||
| 245 | |||||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||||
| 247 | |||||||||||||||||||||||
| 248 | 5 | 1; | |||||||||||||||||||||