Filename | /usr/share/perl5/DateTime/Format/Builder.pm |
Statements | Executed 88 statements in 1.90ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 55µs | 1.48ms | create_class | DateTime::Format::Builder::
3 | 1 | 1 | 20µs | 1.37ms | create_parser | DateTime::Format::Builder::
1 | 1 | 1 | 16µs | 66µs | BEGIN@12 | DateTime::Format::Builder::
1 | 1 | 1 | 15µs | 15µs | BEGIN@9 | DateTime::Format::Builder::
3 | 1 | 1 | 15µs | 1.39ms | create_end_parser | DateTime::Format::Builder::
1 | 1 | 1 | 11µs | 22µs | BEGIN@6 | DateTime::Format::Builder::
1 | 1 | 1 | 11µs | 22µs | BEGIN@11 | DateTime::Format::Builder::
1 | 1 | 1 | 11µs | 11µs | create_constructor | DateTime::Format::Builder::
1 | 1 | 1 | 10µs | 18µs | BEGIN@7 | DateTime::Format::Builder::
3 | 1 | 1 | 7µs | 7µs | create_method | DateTime::Format::Builder::
1 | 1 | 1 | 7µs | 37µs | BEGIN@10 | DateTime::Format::Builder::
1 | 1 | 1 | 6µs | 24µs | BEGIN@15 | DateTime::Format::Builder::
1 | 1 | 1 | 6µs | 1.48ms | import | DateTime::Format::Builder::
1 | 1 | 1 | 6µs | 15µs | BEGIN@50 | DateTime::Format::Builder::
1 | 1 | 1 | 6µs | 14µs | BEGIN@96 | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | __ANON__[:109] | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | __ANON__[:140] | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | __ANON__[:161] | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | clone | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | format_datetime | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | get_parser | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | new | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | on_fail | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | parse_datetime | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | parser | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | set_parser | DateTime::Format::Builder::
0 | 0 | 0 | 0s | 0s | verbose | DateTime::Format::Builder::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package DateTime::Format::Builder; | ||||
2 | { | ||||
3 | 2 | 1µs | $DateTime::Format::Builder::VERSION = '0.81'; | ||
4 | } | ||||
5 | |||||
6 | 2 | 24µs | 2 | 33µs | # spent 22µs (11+11) within DateTime::Format::Builder::BEGIN@6 which was called:
# once (11µs+11µs) by DateTime::Format::MySQL::BEGIN@11 at line 6 # spent 22µs making 1 call to DateTime::Format::Builder::BEGIN@6
# spent 11µs making 1 call to strict::import |
7 | 2 | 22µs | 2 | 26µs | # spent 18µs (10+8) within DateTime::Format::Builder::BEGIN@7 which was called:
# once (10µs+8µs) by DateTime::Format::MySQL::BEGIN@11 at line 7 # spent 18µs making 1 call to DateTime::Format::Builder::BEGIN@7
# spent 8µs making 1 call to warnings::import |
8 | |||||
9 | 2 | 42µs | 1 | 15µs | # spent 15µs within DateTime::Format::Builder::BEGIN@9 which was called:
# once (15µs+0s) by DateTime::Format::MySQL::BEGIN@11 at line 9 # spent 15µs making 1 call to DateTime::Format::Builder::BEGIN@9 |
10 | 2 | 24µs | 2 | 66µs | # spent 37µs (7+29) within DateTime::Format::Builder::BEGIN@10 which was called:
# once (7µs+29µs) by DateTime::Format::MySQL::BEGIN@11 at line 10 # spent 37µs making 1 call to DateTime::Format::Builder::BEGIN@10
# spent 29µs making 1 call to Exporter::import |
11 | 3 | 51µs | 2 | 34µs | # spent 22µs (11+12) within DateTime::Format::Builder::BEGIN@11 which was called:
# once (11µs+12µs) by DateTime::Format::MySQL::BEGIN@11 at line 11 # spent 22µs making 1 call to DateTime::Format::Builder::BEGIN@11
# spent 12µs making 1 call to version::_VERSION |
12 | 1 | 4µs | 1 | 44µs | # spent 66µs (16+51) within DateTime::Format::Builder::BEGIN@12 which was called:
# once (16µs+51µs) by DateTime::Format::MySQL::BEGIN@11 at line 14 # spent 44µs making 1 call to Exporter::import |
13 | validate SCALAR ARRAYREF HASHREF SCALARREF CODEREF GLOB GLOBREF UNDEF | ||||
14 | 2 | 37µs | 2 | 73µs | ); # spent 66µs making 1 call to DateTime::Format::Builder::BEGIN@12
# spent 6µs making 1 call to version::_VERSION |
15 | 2 | 158µs | 2 | 41µs | # spent 24µs (6+17) within DateTime::Format::Builder::BEGIN@15 which was called:
# once (6µs+17µs) by DateTime::Format::MySQL::BEGIN@11 at line 15 # spent 24µs making 1 call to DateTime::Format::Builder::BEGIN@15
# spent 17µs making 1 call to vars::import |
16 | |||||
17 | 1 | 200ns | my $parser = 'DateTime::Format::Builder::Parser'; | ||
18 | |||||
19 | sub verbose { | ||||
20 | warn "Use of verbose() deprecated for the interim."; | ||||
21 | 1; | ||||
22 | } | ||||
23 | |||||
24 | # spent 1.48ms (6µs+1.48) within DateTime::Format::Builder::import which was called:
# once (6µs+1.48ms) by DateTime::Format::MySQL::BEGIN@11 at line 11 of DateTime/Format/MySQL.pm | ||||
25 | 1 | 400ns | my $class = shift; | ||
26 | 1 | 6µs | 1 | 1.48ms | $class->create_class( @_, class => (caller)[0] ) if @_; # spent 1.48ms making 1 call to DateTime::Format::Builder::create_class |
27 | } | ||||
28 | |||||
29 | # spent 1.48ms (55µs+1.42) within DateTime::Format::Builder::create_class which was called:
# once (55µs+1.42ms) by DateTime::Format::Builder::import at line 26 | ||||
30 | 1 | 300ns | my $class = shift; | ||
31 | 1 | 33µs | 1 | 18µs | my %args = validate( # spent 18µs making 1 call to Params::Validate::XS::validate |
32 | @_, | ||||
33 | { | ||||
34 | class => { type => SCALAR, default => (caller)[0] }, | ||||
35 | version => { type => SCALAR, optional => 1 }, | ||||
36 | verbose => { type => SCALAR | GLOBREF | GLOB, optional => 1 }, | ||||
37 | parsers => { type => HASHREF }, | ||||
38 | groups => { type => HASHREF, optional => 1 }, | ||||
39 | constructor => | ||||
40 | { type => UNDEF | SCALAR | CODEREF, optional => 1 }, | ||||
41 | } | ||||
42 | ); | ||||
43 | |||||
44 | 1 | 400ns | verbose( $args{verbose} ) if exists $args{verbose}; | ||
45 | |||||
46 | 1 | 500ns | my $target = $args{class}; # where we're writing our methods and such. | ||
47 | |||||
48 | # Create own lovely new package | ||||
49 | { | ||||
50 | 3 | 212µs | 2 | 23µs | # spent 15µs (6+8) within DateTime::Format::Builder::BEGIN@50 which was called:
# once (6µs+8µs) by DateTime::Format::MySQL::BEGIN@11 at line 50 # spent 15µs making 1 call to DateTime::Format::Builder::BEGIN@50
# spent 8µs making 1 call to strict::unimport |
51 | |||||
52 | 1 | 300ns | ${"${target}::VERSION"} = $args{version} if exists $args{version}; | ||
53 | |||||
54 | 1 | 3µs | 1 | 11µs | $class->create_constructor( # spent 11µs making 1 call to DateTime::Format::Builder::create_constructor |
55 | $target, exists $args{constructor}, | ||||
56 | $args{constructor} | ||||
57 | ); | ||||
58 | |||||
59 | # Turn groups of parser specs in to groups of parsers | ||||
60 | { | ||||
61 | 2 | 600ns | my $specs = $args{groups}; | ||
62 | 1 | 100ns | my %groups; | ||
63 | |||||
64 | 1 | 2µs | for my $label ( keys %$specs ) { | ||
65 | my $parsers = $specs->{$label}; | ||||
66 | my $code = $class->create_parser($parsers); | ||||
67 | $groups{$label} = $code; | ||||
68 | } | ||||
69 | |||||
70 | 1 | 1µs | $dispatch_data{$target} = \%groups; | ||
71 | } | ||||
72 | |||||
73 | # Write all our parser methods, creating parsers as we go. | ||||
74 | 1 | 8µs | while ( my ( $method, $parsers ) = each %{ $args{parsers} } ) { | ||
75 | 3 | 2µs | my $globname = $target . "::$method"; | ||
76 | croak "Will not override a preexisting method $method()" | ||||
77 | 3 | 5µs | if defined &{$globname}; | ||
78 | 3 | 10µs | 3 | 1.39ms | *$globname = $class->create_end_parser($parsers); # spent 1.39ms making 3 calls to DateTime::Format::Builder::create_end_parser, avg 464µs/call |
79 | } | ||||
80 | } | ||||
81 | |||||
82 | } | ||||
83 | |||||
84 | # spent 11µs within DateTime::Format::Builder::create_constructor which was called:
# once (11µs+0s) by DateTime::Format::Builder::create_class at line 54 | ||||
85 | 1 | 300ns | my $class = shift; | ||
86 | 1 | 1µs | my ( $target, $intended, $value ) = @_; | ||
87 | |||||
88 | 1 | 800ns | my $new = $target . "::new"; | ||
89 | 1 | 400ns | $value = 1 unless $intended; | ||
90 | |||||
91 | 1 | 200ns | return unless $value; | ||
92 | 1 | 2µs | return if not $intended and defined &$new; | ||
93 | 1 | 800ns | croak "Will not override a preexisting constructor new()" | ||
94 | if defined &$new; | ||||
95 | |||||
96 | 2 | 531µs | 2 | 21µs | # spent 14µs (6+8) within DateTime::Format::Builder::BEGIN@96 which was called:
# once (6µs+8µs) by DateTime::Format::MySQL::BEGIN@11 at line 96 # spent 14µs making 1 call to DateTime::Format::Builder::BEGIN@96
# spent 8µs making 1 call to strict::unimport |
97 | |||||
98 | 1 | 200ns | return *$new = $value if ref $value eq 'CODE'; | ||
99 | return *$new = sub { | ||||
100 | my $class = shift; | ||||
101 | croak "${class}->new takes no parameters." if @_; | ||||
102 | |||||
103 | my $self = bless {}, ref($class) || $class; | ||||
104 | |||||
105 | # If called on an object, clone, but we've nothing to | ||||
106 | # clone | ||||
107 | |||||
108 | $self; | ||||
109 | 1 | 7µs | }; | ||
110 | } | ||||
111 | |||||
112 | # spent 1.37ms (20µs+1.35) within DateTime::Format::Builder::create_parser which was called 3 times, avg 456µs/call:
# 3 times (20µs+1.35ms) by DateTime::Format::Builder::create_end_parser at line 132, avg 456µs/call | ||||
113 | 3 | 1µs | my $class = shift; | ||
114 | 3 | 2µs | my @common = ( maker => $class ); | ||
115 | 3 | 6µs | if ( @_ == 1 ) { | ||
116 | 3 | 500ns | my $parsers = shift; | ||
117 | 3 | 5µs | my @parsers = ( | ||
118 | ( ref $parsers eq 'HASH' ) | ||||
119 | ? %$parsers | ||||
120 | : ( ( ref $parsers eq 'ARRAY' ) ? @$parsers : $parsers ) | ||||
121 | ); | ||||
122 | 3 | 6µs | 3 | 1.35ms | $parser->create_parser( \@common, @parsers ); # spent 1.35ms making 3 calls to DateTime::Format::Builder::Parser::create_parser, avg 450µs/call |
123 | } | ||||
124 | else { | ||||
125 | $parser->create_parser( \@common, @_ ); | ||||
126 | } | ||||
127 | } | ||||
128 | |||||
129 | |||||
130 | # spent 1.39ms (15µs+1.38) within DateTime::Format::Builder::create_end_parser which was called 3 times, avg 464µs/call:
# 3 times (15µs+1.38ms) by DateTime::Format::Builder::create_class at line 78, avg 464µs/call | ||||
131 | 3 | 800ns | my ( $class, $parsers ) = @_; | ||
132 | 3 | 11µs | 6 | 1.38ms | $class->create_method( $class->create_parser($parsers) ); # spent 1.37ms making 3 calls to DateTime::Format::Builder::create_parser, avg 456µs/call
# spent 7µs making 3 calls to DateTime::Format::Builder::create_method, avg 2µs/call |
133 | } | ||||
134 | |||||
135 | # spent 7µs within DateTime::Format::Builder::create_method which was called 3 times, avg 2µs/call:
# 3 times (7µs+0s) by DateTime::Format::Builder::create_end_parser at line 132, avg 2µs/call | ||||
136 | 3 | 1µs | my ( $class, $parser ) = @_; | ||
137 | return sub { | ||||
138 | my $self = shift; | ||||
139 | $parser->parse( $self, @_ ); | ||||
140 | } | ||||
141 | 3 | 9µs | } | ||
142 | |||||
143 | sub on_fail { | ||||
144 | my ( $class, $input ) = @_; | ||||
145 | |||||
146 | my $pkg; | ||||
147 | my $i = 0; | ||||
148 | while ( ($pkg) = caller( $i++ ) ) { | ||||
149 | last | ||||
150 | if ( !UNIVERSAL::isa( $pkg, 'DateTime::Format::Builder' ) | ||||
151 | && !UNIVERSAL::isa( $pkg, 'DateTime::Format::Builder::Parser' ) ); | ||||
152 | } | ||||
153 | local $Carp::CarpLevel = $i; | ||||
154 | croak "Invalid date format: $input"; | ||||
155 | } | ||||
156 | |||||
157 | sub new { | ||||
158 | my $class = shift; | ||||
159 | croak "Constructor 'new' takes no parameters" if @_; | ||||
160 | my $self = bless { | ||||
161 | parser => sub { croak "No parser set." } | ||||
162 | }, | ||||
163 | ref($class) || $class; | ||||
164 | if ( ref $class ) { | ||||
165 | |||||
166 | # If called on an object, clone | ||||
167 | $self->set_parser( $class->get_parser ); | ||||
168 | |||||
169 | # and that's it. we don't store that much info per object | ||||
170 | } | ||||
171 | return $self; | ||||
172 | } | ||||
173 | |||||
174 | sub parser { | ||||
175 | my $class = shift; | ||||
176 | my $parser = $class->create_end_parser( \@_ ); | ||||
177 | |||||
178 | # Do we need to instantiate a new object for return, | ||||
179 | # or are we modifying an existing object? | ||||
180 | my $self; | ||||
181 | $self = ref $class ? $class : $class->new(); | ||||
182 | |||||
183 | $self->set_parser($parser); | ||||
184 | |||||
185 | $self; | ||||
186 | } | ||||
187 | |||||
188 | sub clone { | ||||
189 | my $self = shift; | ||||
190 | croak "Calling object method as class method!" unless ref $self; | ||||
191 | return $self->new(); | ||||
192 | } | ||||
193 | |||||
194 | sub set_parser { | ||||
195 | my ( $self, $parser ) = @_; | ||||
196 | croak "set_parser given something other than a coderef" | ||||
197 | unless $parser | ||||
198 | and ref $parser eq 'CODE'; | ||||
199 | $self->{parser} = $parser; | ||||
200 | $self; | ||||
201 | } | ||||
202 | |||||
203 | sub get_parser { | ||||
204 | my ($self) = @_; | ||||
205 | return $self->{parser}; | ||||
206 | } | ||||
207 | |||||
208 | sub parse_datetime { | ||||
209 | my $self = shift; | ||||
210 | croak "parse_datetime is an object method, not a class method." | ||||
211 | unless ref $self and $self->isa(__PACKAGE__); | ||||
212 | croak "No date specified." unless @_; | ||||
213 | return $self->{parser}->( $self, @_ ); | ||||
214 | } | ||||
215 | |||||
216 | sub format_datetime { | ||||
217 | croak __PACKAGE__ . "::format_datetime not implemented."; | ||||
218 | } | ||||
219 | |||||
220 | 1 | 668µs | require DateTime::Format::Builder::Parser; | ||
221 | |||||
222 | 1 | 2µs | 1; | ||
223 | |||||
224 | # ABSTRACT: Create DateTime parser classes and objects. | ||||
225 | |||||
226 | __END__ |