| 1 | # -*- perl -*- -------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # Resource::Loader - load different resources depending... | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Joshua Keroes | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # This number is *not* the $VERSION (see below): | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # $Id: Loader.pm,v 1.10 2003/04/28 23:28:19 jkeroes Exp $ | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | package Resource::Loader; | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use vars qw/$VERSION/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # In: hash-style args. See docs. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # Out: object | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 9 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 9 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 9 | $self->_init( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # In: hash-style args. See docs for new() | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # Out: object | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 9 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 9 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 9 | ▶ | while ( my ( $method, $args ) = each %args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 17 | $self->$method( $args ); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 8 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # In: array or arrayref. See docs. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # Out: array or arrayref of resources. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | sub resources { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resources: called CC=14 SCAR=30.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | if ( @_ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 8 | undef $self->{resources}; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 8 | ▶ | for ( ref $_[0] eq 'ARRAY' ? @{ $_[0] } : $_[0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | croak "Malformed resource. Needs 'name', 'when', and 'what' args" | ||||||||||||||||||||||||||||||||||||||||||
| 55 | unless defined $_->{name} | ||||||||||||||||||||||||||||||||||||||||||
| 56 | && defined $_->{when} | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 15 | ▶ | && defined $_->{what}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $_->{"name"} and defined $_->{"when"} and defined $_->{"what"}
defined $_->{"name"} and defined $_->{"when"}
MC/DC0%0/3
defined $_->{"name"} and defined $_->{"when"} and defined $_->{"what"}
defined $_->{"name"}
defined $_->{"when"}
defined $_->{"what"}
Truth table25%1/4
A and B and C
A= defined $_->{"name"}B= defined $_->{"when"}C= defined $_->{"what"}
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | croak "Malformed resource. 'when' and 'what' need to be coderefs." | ||||||||||||||||||||||||||||||||||||||||||
| 60 | unless ref $_->{what} eq "CODE" | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 15 | ▶ | && ref $_->{when} eq "CODE"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_->{"what"} eq "CODE" and ref $_->{"when"} eq "CODE"
MC/DC0%0/2
ref $_->{"what"} eq "CODE" and ref $_->{"when"} eq "CODE"
ref $_->{"what"} eq "CODE"
ref $_->{"when"} eq "CODE"
Truth table33%1/3
A and B
A= ref $_->{"what"} eq "CODE"B= ref $_->{"when"} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | croak "Malformed resource. 'whatargs' needs to be an arrayref." | ||||||||||||||||||||||||||||||||||||||||||
| 64 | if $_->{whatargs} | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 15 | ▶ | && ref $_->{whatargs} ne "ARRAY"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_->{"whatargs"} and ref $_->{"whatargs"} ne "ARRAY"
MC/DC0%0/2
$_->{"whatargs"} and ref $_->{"whatargs"} ne "ARRAY"
$_->{"whatargs"}
ref $_->{"whatargs"} ne "ARRAY"
Truth table66%2/3
A and B
A= $_->{"whatargs"}B= ref $_->{"whatargs"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | croak "Malformed resource. 'whenargs' needs to be an arrayref." | ||||||||||||||||||||||||||||||||||||||||||
| 68 | if $_->{whenargs} | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 15 | ▶ | && ref $_->{whenargs} ne "ARRAY"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_->{"whenargs"} and ref $_->{"whenargs"} ne "ARRAY"
MC/DC100%2/2
$_->{"whenargs"} and ref $_->{"whenargs"} ne "ARRAY"
$_->{"whenargs"}
ref $_->{"whenargs"} ne "ARRAY"
Truth table100%3/3
A and B
A= $_->{"whenargs"}B= ref $_->{"whenargs"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 14 | push @{ $self->{resources} }, $_; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return wantarray ? @{ $self->{resources} } : $self->{resources}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # In: optional new value | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # Out: current value | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 15 | ▶ | sub testing { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
testing: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 15 | ▶ | $self->{testing} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 15 | ▶ | return defined $ENV{RMTESTING} ? $ENV{RMTESTING} : $self->{testing}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # In: optional new value | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # Out: current value | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 19 | ▶ | sub verbose { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verbose: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 19 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 19 | ▶ | $self->{verbose} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 19 | ▶ | return defined $ENV{RMVERBOSE} ? $ENV{RMVERBOSE} : $self->{verbose}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | # In: optional new value | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # Out: current value | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 12 | ▶ | sub cont { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cont: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 12 | ▶ | $self->{cont} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 12 | ▶ | return defined $ENV{RMCONT} ? $ENV{RMCONT} : $self->{cont}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | # In: n/a | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # Out: hashref of our environment variables | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub env { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
env: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 106 | return { RMTESTING => $ENV{RMTESTING}, | ||||||||||||||||||||||||||||||||||||||||||
| 107 | RMVERBOSE => $ENV{RMVERBOSE}, | ||||||||||||||||||||||||||||||||||||||||||
| 108 | RMSTATES => $ENV{RMSTATES}, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | RMCONT => $ENV{RMCONT}, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # In: n/a | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # Out: hashref of loaded states and their returns values. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 8 | ▶ | sub loaded { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
loaded: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 8 | return $self->{loaded}; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | # In: n/a | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # Out: status report | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # Runs the appropriate resources() | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 8 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | # clear out loaded() and status() tables. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 8 | undef $self->{loaded}; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 8 | undef $self->{status}; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 8 | $self->{status} = { map { $_->{name} => 'inactive' } @{ $self->{resources} } }; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 8 | for( @{ $self->{resources} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 133 | 18 | my $name = $_->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 18 | ▶ | if ( defined $ENV{RMSTATES} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 12 | ▶ | if ( grep { $_ eq $name } split /:/, $ENV{RMSTATES} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | print __PACKAGE__ . " state '$name' present in RMSTATES environment var\n"; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 8 | print __PACKAGE__ . " state '$name' skipped due to RMSTATES environment var\n"; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 8 | $self->{status}{$name} = 'skipped'; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 8 | next; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 10 | ▶ | if ( $_->{when}->( ref $_->{whenargs} eq "ARRAY" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 2 | ? @{ $_->{whenargs} } | |||||||||||||||||||||||||||||||||||||||||
| 148 | : () ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 8 | ▶ | print __PACKAGE__ . " state '$name' active\n" if $self->verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 8 | ▶ | if ( $self->testing ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 4 | ▶ | print __PACKAGE__ . " in testing: won't run code for state '$name'\n" if $self->verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 4 | $self->{status}{$name} = 'notrun'; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | $self->{loaded}{$name} = $_->{what}->( ref $_->{whatargs} eq "ARRAY" | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | ▶ | ? @{ $_->{whatargs} } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | : () ); | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 4 | $self->{status}{$name} = 'loaded'; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 8 | ▶ | last unless $self->cont; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | ▶ | print __PACKAGE__ . " state '$name' inactive\n" if $self->verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 2 | $self->{status}{$name} = 'inactive'; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 8 | return $self->loaded; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | # In: n/a | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # Out: status report, e.g. { name => status, ... } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 6 | ▶ | sub status { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | return unless $self->{status} | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 6 | ▶ | && ref $self->{status} eq "HASH"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"status"} and ref $self->{"status"} eq "HASH"
MC/DC0%0/2
$self->{"status"} and ref $self->{"status"} eq "HASH"
$self->{"status"}
ref $self->{"status"} eq "HASH"
Truth table33%1/3
A and B
A= $self->{"status"}B= ref $self->{"status"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 6 | return $self->{status}; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||||||||||||||||