| 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 | use Badger::Debug ':dump'; | ||
| 16 | use Badger::Class | ||
| 17 | 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 | }; | ||
| 31 | |||
| 32 | |||
| 33 | sub export { | ||
| 34 | goto &vars if @_ > 2; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub vars { | ||
| 39 | my $class = shift; | ||
| 40 | my $target = shift || return $class->error_msg('no_target'); | ||
| 41 | my $vars = @_ == 1 ? shift : { @_ }; | ||
| 42 | my ($symbol, $sigil, $name, $dest, $ref); | ||
| 43 | |||
| 44 | # downgrade $target from a Badger::Class object to a package name | ||
| 45 | $target = $target->name | ||
| 46 | if is_object(BCLASS, $target); | ||
| 47 | |||
| 48 | # split text string into lisy ref of variable names | ||
| 49 | $vars = [ split(DELIMITER, $vars) ] | ||
| 50 | unless ref $vars; | ||
| 51 | |||
| 52 | # upgrade a list ref to a hash ref | ||
| 53 | $vars = { map { $_ => undef } @$vars } | ||
| 54 | if ref $vars eq ARRAY; | ||
| 55 | |||
| 56 | $class->error_msg( bad_vars => $vars ) | ||
| 57 | unless ref $vars eq HASH; | ||
| 58 | |||
| 59 | $class->error_msg('no_vars') | ||
| 60 | unless %$vars; | ||
| 61 | |||
| 62 | $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 | while (($symbol, $ref) = each %$vars) { | ||
| 71 | no strict REFS; | ||
| 72 | |||
| 73 | # only accept: $WORD @WORD %WORD WORD | ||
| 74 | $symbol =~ /^([\$\@\%])?(\w+)$/ | ||
| 75 | || return $class->error_msg( bad_var => $symbol ); | ||
| 76 | ($sigil, $name) = ($1 || '$', $2); | ||
| 77 | |||
| 78 | # expand destination to full package name ($Your::Module::WORD) | ||
| 79 | $dest = $target.PKG.$name; | ||
| 80 | |||
| 81 | $class->debug("var: $sigil$name => ", $ref || '\\'.$sigil.$dest, "\n") | ||
| 82 | if DEBUG; | ||
| 83 | |||
| 84 | if ($sigil eq '$') { | ||
| 85 | *$dest = defined $ref | ||
| 86 | ? (ref $ref eq SCALAR ? $ref : do { my $copy = $ref; \$copy }) | ||
| 87 | : \$$dest; | ||
| 88 | } | ||
| 89 | elsif ($sigil eq '@') { | ||
| 90 | *$dest = defined $ref | ||
| 91 | ? (ref $ref eq ARRAY ? $ref : [$ref]) | ||
| 92 | : \@$dest; | ||
| 93 | } | ||
| 94 | elsif ($sigil eq '%') { | ||
| 95 | *$dest = defined $ref | ||
| 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 | return $class->error_msg( bad_sigil => $symbol ); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ |