| 1 | package TAP::Base; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 44 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 44 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 44 | ▶ | use base 'TAP::Object'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | =head1 NAME | ||||||||||||||
| 9 | |||||||||||||||
| 10 | TAP::Base - Base class that provides common functionality to L<TAP::Parser> | ||||||||||||||
| 11 | and L<TAP::Harness> | ||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||
| 14 | |||||||||||||||
| 15 | Version 3.52 | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =cut | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 44 | our $VERSION = '3.52'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 44 | ▶ | use constant GOT_TIME_HIRES => do { | ||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||
| 22 | 44 | ▶ | eval 'use Time::HiRes qw(time);'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 44 | ▶ | $@ ? 0 : 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | 44 | }; | |||||||||||||
| 25 | |||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||
| 27 | |||||||||||||||
| 28 | package TAP::Whatever; | ||||||||||||||
| 29 | |||||||||||||||
| 30 | use base 'TAP::Base'; | ||||||||||||||
| 31 | |||||||||||||||
| 32 | # ... later ... | ||||||||||||||
| 33 | |||||||||||||||
| 34 | my $thing = TAP::Whatever->new(); | ||||||||||||||
| 35 | |||||||||||||||
| 36 | $thing->callback( event => sub { | ||||||||||||||
| 37 | # do something interesting | ||||||||||||||
| 38 | } ); | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||
| 41 | |||||||||||||||
| 42 | C<TAP::Base> provides callback management. | ||||||||||||||
| 43 | |||||||||||||||
| 44 | =head1 METHODS | ||||||||||||||
| 45 | |||||||||||||||
| 46 | =head2 Class Methods | ||||||||||||||
| 47 | |||||||||||||||
| 48 | =cut | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 594 | ▶ | sub _initialize { | ||||||||||||
|
Subroutine
_initialize: called CC=3 SCAR=14.2
| |||||||||||||||
| 51 | 0 | ▶ | my ( $self, $arg_for, $ok_callback ) = @_; | ||||||||||||
|
Subroutine
_initialize: not called CC=3 SCAR=14.2
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 0 | my %ok_map = map { $_ => 1 } @$ok_callback; | |||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | $self->{ok_callbacks} = \%ok_map; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | ▶ | if ( my $cb = delete $arg_for->{callbacks} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 58 | 0 | ▶ | while ( my ( $event, $callback ) = each %$cb ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 59 | 0 | $self->callback( $event, $callback ); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | return $self; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =head3 C<callback> | ||||||||||||||
| 67 | |||||||||||||||
| 68 | Install a callback for a named event. | ||||||||||||||
| 69 | |||||||||||||||
| 70 | =cut | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 148 | ▶ | sub callback { | ||||||||||||
|
Subroutine
callback: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||
| 73 | 0 | ▶ | my ( $self, $event, $callback ) = @_; | ||||||||||||
|
Subroutine
callback: not called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | my %ok_map = %{ $self->{ok_callbacks} }; | |||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | ▶ | $self->_croak('No callbacks may be installed') | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 78 | unless %ok_map; | ||||||||||||||
| 79 | |||||||||||||||
| 80 | $self->_croak( "Callback $event is not supported. Valid callbacks are " | ||||||||||||||
| 81 | . join( ', ', sort keys %ok_map ) ) | ||||||||||||||
| 82 | 0 | ▶ | unless exists $ok_map{$event}; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | push @{ $self->{code_for}{$event} }, $callback; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 0 | return; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 277 | ▶ | sub _has_callbacks { | ||||||||||||
|
Subroutine
_has_callbacks: called CC=1 SCAR=1.2
| |||||||||||||||
| 90 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_has_callbacks: not called CC=1 SCAR=1.2
| |||||||||||||||
| 91 | 0 | return keys %{ $self->{code_for} } != 0; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 1247 | ▶ | sub _callback_for { | ||||||||||||
|
Subroutine
_callback_for: called CC=1 SCAR=1.2
| |||||||||||||||
| 95 | 0 | ▶ | my ( $self, $event ) = @_; | ||||||||||||
|
Subroutine
_callback_for: not called CC=1 SCAR=1.2
| |||||||||||||||
| 96 | 0 | return $self->{code_for}{$event}; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 967 | ▶ | sub _make_callback { | ||||||||||||
|
Subroutine
_make_callback: called CC=2 SCAR=9.2
| |||||||||||||||
| 100 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_make_callback: not called CC=2 SCAR=9.2
| |||||||||||||||
| 101 | 0 | my $event = shift; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 0 | my $cb = $self->_callback_for($event); | |||||||||||||
| 104 | 0 | ▶ | return unless defined $cb; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 105 | 0 | return map { $_->(@_) } @$cb; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head3 C<get_time> | ||||||||||||||
| 109 | |||||||||||||||
| 110 | Return the current time using Time::HiRes if available. | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =cut | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 621 | ▶ | sub get_time { return time() } | ||||||||||||
|
Subroutine
get_time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 115 | |||||||||||||||
| 116 | =head3 C<time_is_hires> | ||||||||||||||
| 117 | |||||||||||||||
| 118 | Return true if the time returned by get_time is high resolution (i.e. if Time::HiRes is available). | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =cut | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 0 | ▶ | sub time_is_hires { return GOT_TIME_HIRES } | ||||||||||||
|
Subroutine
time_is_hires: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 123 | |||||||||||||||
| 124 | =head3 C<get_times> | ||||||||||||||
| 125 | |||||||||||||||
| 126 | Return array reference of the four-element list of CPU seconds, | ||||||||||||||
| 127 | as with L<perlfunc/times>. | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =cut | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 546 | ▶ | sub get_times { return [ times() ] } | ||||||||||||
|
Subroutine
get_times: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 132 | |||||||||||||||
| 133 | 44 | 1; | |||||||||||||