| 1 | # Copyright 2009, 2010, 2011, 2013, 2016, 2017 Kevin Ryde | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # This file is part of Time-Duration-Locale. | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # Time-Duration-Locale is free software; you can redistribute it and/or | ||||||||||||||||||||
| 6 | # modify it under the terms of the GNU General Public License as published | ||||||||||||||||||||
| 7 | # by the Free Software Foundation; either version 3, or (at your option) any | ||||||||||||||||||||
| 8 | # later version. | ||||||||||||||||||||
| 9 | # | ||||||||||||||||||||
| 10 | # Time-Duration-Locale is distributed in the hope that it will be useful, | ||||||||||||||||||||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||||||||||||||||||||
| 13 | # Public License for more details. | ||||||||||||||||||||
| 14 | # | ||||||||||||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||||||||||||
| 16 | # with Time-Duration-Locale. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | package Time::Duration::Locale; | ||||||||||||||||||||
| 19 | 3 | ▶ | use 5.004; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 3 | ▶ | use Time::Duration::LocaleObject; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 3 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 3 | $VERSION = 12; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 3 | ▶ | use Exporter; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 3 | @ISA = ('Exporter'); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # same exports as Time::Duration | ||||||||||||||||||||
| 31 | 3 | @EXPORT = qw(later later_exact earlier earlier_exact | |||||||||||||||||||
| 32 | ago ago_exact from_now from_now_exact | ||||||||||||||||||||
| 33 | duration duration_exact | ||||||||||||||||||||
| 34 | concise); | ||||||||||||||||||||
| 35 | 3 | @EXPORT_OK = ('interval', @EXPORT); | |||||||||||||||||||
| 36 | 3 | %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | # uncomment this to run the ### lines | ||||||||||||||||||||
| 39 | #use Smart::Comments; | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | # SUPER::can() here is UNIVERSAL::can(). When an autoloaded function like | ||||||||||||||||||||
| 42 | # duration() is exported, UNIVERSAL::can() returns the stub coderef which | ||||||||||||||||||||
| 43 | # calls to AUTOLOAD. This means AUTOLOAD() can't rely on can() to get the | ||||||||||||||||||||
| 44 | # dispatcher func, it has to make its own. Or unless can() oughtn't use | ||||||||||||||||||||
| 45 | # SUPER::can this way ... | ||||||||||||||||||||
| 46 | # | ||||||||||||||||||||
| 47 | # If $name exists as a method in Time::Duration::LocaleObject, meaning a | ||||||||||||||||||||
| 48 | # function in Time::Duration or langugage-specifc module, then create *$name | ||||||||||||||||||||
| 49 | # so as to have just one copy of which can() will return each time and so as | ||||||||||||||||||||
| 50 | # not to go through AUTOLOAD() every time. | ||||||||||||||||||||
| 51 | # | ||||||||||||||||||||
| 52 | # If $name is unknown then don't create a dispatcher, firstly of course so | ||||||||||||||||||||
| 53 | # can() is false, and secondly to avoid junking up the package if a caller | ||||||||||||||||||||
| 54 | # gets a name wrong. | ||||||||||||||||||||
| 55 | # | ||||||||||||||||||||
| 56 | 3 | ▶ | sub can { | ||||||||||||||||||
|
Subroutine
can: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 57 | 3 | my ($class, $name) = @_; | |||||||||||||||||||
| 58 | ### TDL can(): $name | ||||||||||||||||||||
| 59 | 3 | ▶ | return $class->SUPER::can($name) || _make_dispatcher($name); | ||||||||||||||||||
|
Condition100%3/3
$class->SUPER::can($name) || _make_dispatcher($name)
MC/DC100%2/2
$class->SUPER::can($name) || _make_dispatcher($name)
$class->SUPER::can($name)
_make_dispatcher($name)
Truth table100%3/3
A || B
A= $class->SUPER::can($name)B= _make_dispatcher($name)
| |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 24 | ▶ | sub AUTOLOAD { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 62 | 24 | my $name = $AUTOLOAD; | |||||||||||||||||||
| 63 | ### TDL AUTOLOAD(): $name | ||||||||||||||||||||
| 64 | 24 | $name =~ s/.*://; | |||||||||||||||||||
| 65 | 24 | ▶ | my $code = _make_dispatcher($name) || croak "No such function $name()"; | ||||||||||||||||||
|
Condition100%2/2
_make_dispatcher($name) || croak("No such function $name()")
MC/DC100%1/1
_make_dispatcher($name) || croak("No such function $name()")
_make_dispatcher($name)
Truth table100%2/2
A
A= _make_dispatcher($name)
| |||||||||||||||||||||
| 66 | 23 | goto $code; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | # The method call to Time::Duration::LocaleObject here is "by name". Could | ||||||||||||||||||||
| 70 | # instead go to the coderef returned by can(), like | ||||||||||||||||||||
| 71 | # | ||||||||||||||||||||
| 72 | # sub { unshift @_, 'Time::Duration::LocaleObject'; goto $can; }; | ||||||||||||||||||||
| 73 | # | ||||||||||||||||||||
| 74 | # Dunno if there's more merit in the name or the coderef. The name would | ||||||||||||||||||||
| 75 | # support redefinitions (though the base TDLObj->can() returns the same subr | ||||||||||||||||||||
| 76 | # every time). The coderef might save a couple of cycles. | ||||||||||||||||||||
| 77 | # | ||||||||||||||||||||
| 78 | 26 | ▶ | sub _make_dispatcher { | ||||||||||||||||||
|
Subroutine
_make_dispatcher: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 79 | 26 | my ($name) = @_; | |||||||||||||||||||
| 80 | 26 | ▶ | Time::Duration::LocaleObject->can($name) or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 38 | ▶ | my $subr = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 82 | #### TDL dispatch to TDLObj method: $name | ||||||||||||||||||||
| 83 | 38 | return Time::Duration::LocaleObject->$name (@_); | |||||||||||||||||||
| 84 | 24 | }; | |||||||||||||||||||
| 85 | 3 | ▶ | { no strict 'refs'; *$name = $subr } | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 86 | 24 | return $subr; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 3 | 1; | |||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||