| 1 | #======================================================================== | ||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||
| 3 | # Badger::Prototype | ||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||||||
| 6 | # Base class module for a protoype class that has a default instance | ||||||||||||||||||||||||||||||||
| 7 | # that can be created on demand. | ||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||
| 9 | # AUTHOR | ||||||||||||||||||||||||||||||||
| 10 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||
| 12 | #======================================================================== | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | package Badger::Prototype; | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | use Badger::Class | ||||||||||||||||||||||||||||||||
| 17 | 70 | base => 'Badger::Base', | |||||||||||||||||||||||||||||||
| 18 | version => 0.01, | ||||||||||||||||||||||||||||||||
| 19 | debug => 0, | ||||||||||||||||||||||||||||||||
| 20 | constants => 'PKG REFS ONCE', | ||||||||||||||||||||||||||||||||
| 21 | 70 | ▶ | words => 'PROTOTYPE'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | sub prototype { | ||||||||||||||||||||||||||||||||
| 24 | 6022 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
prototype: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 25 | 6022 | ▶ | return $class if ref $class; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | 70 | ▶ | no strict REFS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | 70 | ▶ | no warnings ONCE; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 1927 | ▶ | if (@_ == 1 && ! defined $_[0]) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@_ == 1 and not defined $_[0]
MC/DC50%1/2
@_ == 1 and not defined $_[0]
@_ == 1
not defined $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not defined $_[0]
| |||||||||||||||||||||||||||||||||
| 30 | # if only a single undef argument is provided, then clear any | ||||||||||||||||||||||||||||||||
| 31 | # prototype from $PROTOTYPE and return a reference to it. | ||||||||||||||||||||||||||||||||
| 32 | 1 | my $proto = ${$class.PKG.PROTOTYPE}; | |||||||||||||||||||||||||||||||
| 33 | 1 | undef ${$class.PKG.PROTOTYPE}; | |||||||||||||||||||||||||||||||
| 34 | 1 | return $proto; | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | elsif (@_) { | ||||||||||||||||||||||||||||||||
| 37 | # if any other arguments are provided then it forces us to create | ||||||||||||||||||||||||||||||||
| 38 | # a new prototype with the fresh configuration options. | ||||||||||||||||||||||||||||||||
| 39 | 1 | undef ${$class.PKG.PROTOTYPE}; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | # return the cached value (assuming we didn't just clear it) or create | ||||||||||||||||||||||||||||||||
| 43 | # a new one (if we did, or if there wasn't a previous value) | ||||||||||||||||||||||||||||||||
| 44 | 1926 | ▶ | return ${$class.PKG.PROTOTYPE} ||= $class->new(@_); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
${$class . '::' . "PROTOTYPE";} ||= $class->new(@_)
MC/DC0%0/2
${$class . '::' . "PROTOTYPE";} ||= $class->new(@_)
${$class . '::' . "PROTOTYPE";}
$class->new(@_)
Truth table66%2/3
A ||= B
A= ${$class . '::' . "PROTOTYPE";}B= $class->new(@_)
| |||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | sub has_prototype { | ||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
has_prototype: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||
| 50 | 70 | ▶ | no strict REFS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 51 | 70 | ▶ | no warnings ONCE; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 52 | 2 | defined ${$class.PKG.PROTOTYPE}; | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 70 | 1; | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | __END__ | ||||||||||||||||||||||||||||||||