| 1 | package Sub::Lib; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use v5.8.8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '0.03'; | |||||||||
| 8 | |||||||||||
| 9 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 10 | 5 | my ($class, @args) = @_; | |||||||||
| 11 | |||||||||||
| 12 | 5 | my $args; | |||||||||
| 13 | 5 | ▶ | if(1 == @args) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | 2 | ▶ | die "reference argument to new() must be a HASH\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | unless 'HASH' eq ref $args[0]; | ||||||||||
| 16 | 1 | $args = $args[0]; | |||||||||
| 17 | } | ||||||||||
| 18 | else { | ||||||||||
| 19 | 3 | ▶ | die "non-reference argument list to new() must have an even number of elements\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | unless 0 == @args % 2; | ||||||||||
| 21 | 2 | $args = {@args}; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | # \o/ Lobe den Abgrund | ||||||||||
| 25 | 3 | my $self = bless do { | |||||||||
| 26 | 3 | my $_lib = { }; | |||||||||
| 27 | 15 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||
| 28 | 15 | my ($name, $sub) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 15 | ▶ | return $_lib | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | unless defined $name; | ||||||||||
| 32 | |||||||||||
| 33 | 10 | ▶ | if(defined $sub) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 3 | ▶ | die "sub-routine ($name) is not a sub-routine?\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | unless 'CODE' eq ref $sub; | ||||||||||
| 36 | die "sub-routine ($name) already installed in library\n" | ||||||||||
| 37 | 2 | ▶ | if exists $_lib->{$name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | $_lib->{$name} = $sub; | |||||||||
| 40 | } | ||||||||||
| 41 | else { | ||||||||||
| 42 | die "sub-routine ($name) not installed in library\n" | ||||||||||
| 43 | 7 | ▶ | unless exists $_lib->{$name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 7 | return $_lib->{$name}; | |||||||||
| 47 | } | ||||||||||
| 48 | 3 | }, $class; | |||||||||
| 49 | |||||||||||
| 50 | $self->($_, $args->{$_}) | ||||||||||
| 51 | 3 | for keys %$args; | |||||||||
| 52 | 2 | return $self; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 2 | ▶ | sub has { | ||||||||
|
Subroutine
has: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 56 | 2 | my ($self, $name) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 2 | my $_lib = $self->(); | |||||||||
| 59 | 2 | return $_lib->{$name}; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 1 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 63 | 1 | my ($self, $name, @args) = @_; | |||||||||
| 64 | |||||||||||
| 65 | 1 | return $self->($name)->(@args); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 1 | ▶ | sub call { | ||||||||
|
Subroutine
call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 69 | 1 | my ($self, $name, $object, @args) = @_; | |||||||||
| 70 | |||||||||||
| 71 | 1 | my $sub = $self->($name); | |||||||||
| 72 | 1 | return $object->$sub(@args); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 2 | ▶ | sub void { | ||||||||
|
Subroutine
void: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 76 | 2 | my ($self, $name) = @_; | |||||||||
| 77 | |||||||||||
| 78 | 2 | my $_lib = $self->(); | |||||||||
| 79 | my $sub = exists $_lib->{$name} | ||||||||||
| 80 | ? $_lib->{$name} | ||||||||||
| 81 | 1 | ▶ | : sub { return } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 82 | 2 | ▶ | ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | |||||||||||
| 84 | 2 | return $sub; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 1 | ▶ | sub curry { | ||||||||
|
Subroutine
curry: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 88 | 1 | my ($self, $name, @args) = @_; | |||||||||
| 89 | |||||||||||
| 90 | 1 | my $sub = $self->($name); | |||||||||
| 91 | 1 | ▶ | return sub { $sub->(@args, @_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 1 | ▶ | sub o { | ||||||||
|
Subroutine
o: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 95 | 1 | my ($self, $name, $object, @args) = @_; | |||||||||
| 96 | |||||||||||
| 97 | 1 | my $sub = $self->($name); | |||||||||
| 98 | 1 | ▶ | return sub { $object->$sub(@args, @_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 2 | ▶ | sub y { | ||||||||
|
Subroutine
y: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 102 | 2 | my ($self, $name, $sub, @args) = @_; | |||||||||
| 103 | |||||||||||
| 104 | 2 | ▶ | die "code reference required for lambda\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | unless 'CODE' eq ref $sub; | ||||||||||
| 106 | |||||||||||
| 107 | 1 | ▶ | return sub { $sub->( $self->($name), @args, @_ ) } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 108 | 1 | } | |||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | 1 | 1 | |||||||||
| 112 | __END__ | ||||||||||