| 1 | #======================================================================== | ||||||||||||||||||
| 2 | # | ||||||||||||||||||
| 3 | # Badger::Class::Vars | ||||||||||||||||||
| 4 | # | ||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||
| 6 | # Class mixin module for adding package variables to a class. | ||||||||||||||||||
| 7 | # | ||||||||||||||||||
| 8 | # AUTHOR | ||||||||||||||||||
| 9 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||
| 10 | # | ||||||||||||||||||
| 11 | #======================================================================== | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | package Badger::Class::Vars; | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 3 | ▶ | use Badger::Debug ':dump'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | use Badger::Class | ||||||||||||||||||
| 17 | 3 | version => 0.01, | |||||||||||||||||
| 18 | debug => 0, | ||||||||||||||||||
| 19 | base => 'Badger::Base Badger::Exporter', | ||||||||||||||||||
| 20 | import => 'BCLASS', | ||||||||||||||||||
| 21 | constants => 'DELIMITER SCALAR ARRAY HASH REFS PKG', | ||||||||||||||||||
| 22 | utils => 'is_object', | ||||||||||||||||||
| 23 | messages => { | ||||||||||||||||||
| 24 | no_target => 'No target class specified to generate variables for', | ||||||||||||||||||
| 25 | no_vars => 'No vars specified to define', | ||||||||||||||||||
| 26 | bad_vars => 'Invalid vars specified: %s', | ||||||||||||||||||
| 27 | bad_var => 'Invalid variable name in vars: %s', | ||||||||||||||||||
| 28 | bad_hash => 'Invalid hash variable for %s in vars: %s', | ||||||||||||||||||
| 29 | bad_sigil => 'Unrecognised sigil for symbol: %s', | ||||||||||||||||||
| 30 | 3 | ▶ | }; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | sub export { | ||||||||||||||||||
| 34 | 6 | ▶ | goto &vars if @_ > 2; | ||||||||||||||||
|
Subroutine
export: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||
| 35 | } | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | sub vars { | ||||||||||||||||||
| 39 | 9 | ▶ | my $class = shift; | ||||||||||||||||
|
Subroutine
vars: called CC=20 SCAR=30.5
Pod
documented
| |||||||||||||||||||
| 40 | 9 | ▶ | my $target = shift || return $class->error_msg('no_target'); | ||||||||||||||||
|
Condition50%1/2
shift() || (return $class->error_msg('no_target'))
MC/DC0%0/1
shift() || (return $class->error_msg('no_target'))
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||
| 41 | 9 | ▶ | my $vars = @_ == 1 ? shift : { @_ }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | 9 | my ($symbol, $sigil, $name, $dest, $ref); | |||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | # downgrade $target from a Badger::Class object to a package name | ||||||||||||||||||
| 45 | 9 | ▶ | $target = $target->name | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 46 | if is_object(BCLASS, $target); | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | # split text string into lisy ref of variable names | ||||||||||||||||||
| 49 | 9 | ▶ | $vars = [ split(DELIMITER, $vars) ] | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 50 | unless ref $vars; | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | # upgrade a list ref to a hash ref | ||||||||||||||||||
| 53 | 9 | ▶ | $vars = { map { $_ => undef } @$vars } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 54 | if ref $vars eq ARRAY; | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | 9 | ▶ | $class->error_msg( bad_vars => $vars ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 57 | unless ref $vars eq HASH; | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | 9 | ▶ | $class->error_msg('no_vars') | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 60 | unless %$vars; | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | 9 | $class->debug("Defining vars for $target: ", $class->dump_data($vars)) | |||||||||||||||||
| 63 | if DEBUG; | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | # This is a slightly simplified (stricter) version of the equivalent | ||||||||||||||||||
| 66 | # code in vars.pm with a little extra syntactic sugar supported. | ||||||||||||||||||
| 67 | # Unfortunately it's not possible to delegate to vars.pm because | ||||||||||||||||||
| 68 | # it will only export to its caller, and not to a third party package | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 9 | ▶ | while (($symbol, $ref) = each %$vars) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 71 | 3 | ▶ | no strict REFS; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | # only accept: $WORD @WORD %WORD WORD | ||||||||||||||||||
| 74 | 37 | ▶ | $symbol =~ /^([\$\@\%])?(\w+)$/ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 75 | || return $class->error_msg( bad_var => $symbol ); | ||||||||||||||||||
| 76 | 37 | ▶ | ($sigil, $name) = ($1 || '$', $2); | ||||||||||||||||
|
Condition100%2/2
$1 || '$'
MC/DC100%1/1
$1 || '$'
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | # expand destination to full package name ($Your::Module::WORD) | ||||||||||||||||||
| 79 | 37 | $dest = $target.PKG.$name; | |||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | 37 | $class->debug("var: $sigil$name => ", $ref || '\\'.$sigil.$dest, "\n") | |||||||||||||||||
| 82 | if DEBUG; | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | 37 | ▶ | if ($sigil eq '$') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 85 | *$dest = defined $ref | ||||||||||||||||||
| 86 | 22 | ▶ | ? (ref $ref eq SCALAR ? $ref : do { my $copy = $ref; \$copy }) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 87 | : \$$dest; | ||||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | elsif ($sigil eq '@') { | ||||||||||||||||||
| 90 | 8 | ▶ | *$dest = defined $ref | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 91 | ? (ref $ref eq ARRAY ? $ref : [$ref]) | ||||||||||||||||||
| 92 | : \@$dest; | ||||||||||||||||||
| 93 | } | ||||||||||||||||||
| 94 | elsif ($sigil eq '%') { | ||||||||||||||||||
| 95 | 7 | ▶ | *$dest = defined $ref | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 96 | ? (ref $ref eq HASH | ||||||||||||||||||
| 97 | ? $ref | ||||||||||||||||||
| 98 | : return $class->error_msg( bad_hash => $symbol, $ref ) | ||||||||||||||||||
| 99 | ) | ||||||||||||||||||
| 100 | : \%$dest; | ||||||||||||||||||
| 101 | } | ||||||||||||||||||
| 102 | else { | ||||||||||||||||||
| 103 | # should never happen | ||||||||||||||||||
| 104 | 0 | return $class->error_msg( bad_sigil => $symbol ); | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | 3 | 1; | |||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||