| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Class | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Module implementing metaclass functionality for composing classes | ||
| 7 | # (equivalent to C<use base>) and other class-related actions. | ||
| 8 | # | ||
| 9 | # AUTHOR | ||
| 10 | # Andy Wardley <abw@wardley.org> | ||
| 11 | # | ||
| 12 | #======================================================================== | ||
| 13 | |||
| 14 | package Badger::Class; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use Carp; | ||
| 19 | use base 'Badger::Exporter'; | ||
| 20 | use constant { | ||
| 21 | base_id => 'Badger', | ||
| 22 | BCLASS => 'Badger::Class', | ||
| 23 | FILESYSTEM => 'Badger::Filesystem', | ||
| 24 | CONSTANTS => 'Badger::Constants', | ||
| 25 | EXPORTER => 'Badger::Exporter', | ||
| 26 | MIXIN => 'Badger::Mixin', | ||
| 27 | CODECS => 'Badger::Codecs', | ||
| 28 | UTILS => 'Badger::Utils', | ||
| 29 | DEBUGGER => 'Badger::Debug', | ||
| 30 | CONFIG => 'Badger::Class::Config', | ||
| 31 | METHODS => 'Badger::Class::Methods', | ||
| 32 | VARS => 'Badger::Class::Vars', | ||
| 33 | MESSAGES => 'MESSAGES', | ||
| 34 | VERSION => 'VERSION', | ||
| 35 | MIXINS => 'MIXINS', | ||
| 36 | THROWS => 'THROWS', | ||
| 37 | ISA => 'ISA', | ||
| 38 | NO_VALUE => "You didn't specify a value for the '%s' option", | ||
| 39 | }; | ||
| 40 | use Badger::Constants | ||
| 41 | 'DELIMITER SCALAR ARRAY HASH CODE PKG REFS ONCE TRUE FALSE LOADED'; | ||
| 42 | use overload | ||
| 43 | '""' => 'name', | ||
| 44 | fallback => 1; | ||
| 45 | |||
| 46 | our $VERSION = 0.01; | ||
| 47 | our $DEBUG = 0 unless defined $DEBUG; | ||
| 48 | our $LOADED = { }; | ||
| 49 | |||
| 50 | BEGIN { | ||
| 51 | # generate a compile time constant from $DEBUG | ||
| 52 | *DEBUG = sub() { $DEBUG }; | ||
| 53 | } | ||
| 54 | |||
| 55 | #----------------------------------------------------------------------- | ||
| 56 | # Methods that we delegate to other modules. The module name is | ||
| 57 | # determined by calling the constant method (first argument on RHS | ||
| 58 | # which is auto-quoted by '=>', e.g. 'METHODS', 'ALIASES') against | ||
| 59 | # $self, allowing for sub-classes of Badger::Class to define different | ||
| 60 | # modules for this task. The second argument on the RHS is the method. | ||
| 61 | # The methods are generated a little further on in this module. | ||
| 62 | #----------------------------------------------------------------------- | ||
| 63 | |||
| 64 | our $DELEGATES = { | ||
| 65 | # note the first argument on RHS is quto-quoted by => | ||
| 66 | accessors => [ METHODS => 'accessors' ], | ||
| 67 | codec => [ CODECS => 'export_codec' ], | ||
| 68 | codecs => [ CODECS => 'export_codecs' ], | ||
| 69 | config => [ CONFIG => 'export' ], | ||
| 70 | constants => [ CONSTANTS => 'export' ], | ||
| 71 | filesystem => [ FILESYSTEM => 'export' ], | ||
| 72 | hash_methods => [ METHODS => 'hash' ], | ||
| 73 | mutators => [ METHODS => 'mutators' ], | ||
| 74 | slots => [ METHODS => 'slots' ], | ||
| 75 | init_method => [ METHODS => 'initialiser' ], | ||
| 76 | auto_can => [ METHODS => 'auto_can' ], | ||
| 77 | utils => [ UTILS => 'export' ], | ||
| 78 | vars => [ VARS => 'vars' ], | ||
| 79 | }; | ||
| 80 | |||
| 81 | *get_methods = \&accessors; | ||
| 82 | *set_methods = \&mutators; | ||
| 83 | |||
| 84 | |||
| 85 | #----------------------------------------------------------------------- | ||
| 86 | # Define exportable items and export hooks (see Badger::Exporter) | ||
| 87 | #----------------------------------------------------------------------- | ||
| 88 | |||
| 89 | our $EXPORT_ANY = ['BCLASS']; | ||
| 90 | our $EXPORT_FAIL = \&_export_fail; | ||
| 91 | our $EXPORT_HOOKS = { | ||
| 92 | debug => [\&_debug_hook, 1], | ||
| 93 | dumps => [\&_dumps_hook, 1], | ||
| 94 | map { $_ => \&_export_hook } | ||
| 95 | qw( | ||
| 96 | base uber mixin mixins version constant constants words vars | ||
| 97 | config exports throws messages utils codec codecs filesystem | ||
| 98 | hooks methods alias slots accessors mutators get_methods | ||
| 99 | set_methods hash_methods init_method auto_can overload as_text | ||
| 100 | is_true | ||
| 101 | ) | ||
| 102 | }; | ||
| 103 | |||
| 104 | |||
| 105 | sub export { | ||
| 106 | my ($class, $package, @args) = @_; | ||
| 107 | no strict REFS; | ||
| 108 | no warnings ONCE; | ||
| 109 | ${ $package.PKG.LOADED } ||= 1; # add $BADGER_LOADED to mark our scent | ||
| 110 | $class->SUPER::export($package, @args); | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | sub _export_hook { | ||
| 115 | my ($class, $target, $key, $symbols) = @_; | ||
| 116 | |||
| 117 | croak sprintf(NO_VALUE, $key) | ||
| 118 | unless @$symbols; | ||
| 119 | |||
| 120 | class($target, $class)->$key(shift @$symbols); | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | sub _export_fail { | ||
| 125 | my ($class, $target, $key, $symbols, $import) = @_; | ||
| 126 | |||
| 127 | # look for any additional export hooks defined in $HOOKS, e.g. | ||
| 128 | # by a subclass or poked in via the hooks() method | ||
| 129 | my $hook = class($class)->hash_value( HOOKS => $key ) || return; | ||
| 130 | |||
| 131 | croak sprintf(NO_VALUE, $key) | ||
| 132 | unless @$symbols; | ||
| 133 | |||
| 134 | # We use the two-argument call to class() which tells it that we want | ||
| 135 | # a $class metaclass object rather than the default of Badger::Class. | ||
| 136 | # This is because subclasses may be calling this method so $class isn't | ||
| 137 | # always going to be Badger::Class | ||
| 138 | class($target, $class)->$hook(shift @$symbols); | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | sub _debug_hook { | ||
| 143 | my ($class, $target, $key, $debug) = @_; | ||
| 144 | $debug = { default => $debug } | ||
| 145 | unless ref $debug eq HASH; | ||
| 146 | _autoload($class->DEBUGGER)->export($target, %$debug); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _dumps_hook { | ||
| 150 | my ($class, $target, $key, $dumps) = @_; | ||
| 151 | _autoload($class->DEBUGGER)->export($target, dumps => $dumps); | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | #----------------------------------------------------------------------- | ||
| 157 | # Define a lexical scope to enclose class lookup tables | ||
| 158 | #----------------------------------------------------------------------- | ||
| 159 | |||
| 160 | # Badger::Class and each of its subclasses have their own metaclass | ||
| 161 | # table mapping class names to objects. | ||
| 162 | my $METACLASSES = { }; | ||
| 163 | |||
| 164 | { | ||
| 165 | # class/package name - define this up-front so we can use it below | ||
| 166 | sub CLASS { | ||
| 167 | # first argument is object or class name, otherwise return caller | ||
| 168 | @_ ? (ref $_[0] || $_[0]) | ||
| 169 | : (caller())[0]; | ||
| 170 | } | ||
| 171 | |||
| 172 | # Sorry if this messes with your head. We want class() and classes() | ||
| 173 | # methods that create Badger::Class objects. However, we also want | ||
| 174 | # Badger::Class to be subclassable (e.g. Badger::Factory::Class), where | ||
| 175 | # class() and classes() return the subclass objects instead of the usual | ||
| 176 | # Badger::Class. So we have an UBER() class method whose job it is to | ||
| 177 | # create the class() and classes() methods for the relevant metaclass | ||
| 178 | |||
| 179 | sub UBER { | ||
| 180 | # $pkg is the metaclass name, e.g. Badger::Class, but can also be | ||
| 181 | # subclasses, e.g. Badger::Factory::Class | ||
| 182 | my $pkg = shift || __PACKAGE__; | ||
| 183 | |||
| 184 | # $CLASSES is a lookup table mapping package names to Badger::Class | ||
| 185 | # objects. We need a new lookup table for each subclass of | ||
| 186 | # Badger::Class, so we reuse/create such a table in $METACLASSES, | ||
| 187 | # indexed by the metaclass name, e.g. Badger::Class, etc. | ||
| 188 | my $CLASSES = $METACLASSES->{ $pkg } ||= { }; | ||
| 189 | |||
| 190 | # We want to keep the class() subroutine as fast as possible as it | ||
| 191 | # gets called often. It's a tiny bit faster to declare a variable | ||
| 192 | # outside the closure and reuse it, rather than defining a new | ||
| 193 | # variable each time the closure is called. Ho hum. | ||
| 194 | my $class; | ||
| 195 | |||
| 196 | # The class() subroutine is used to fetch/create a Badger::Class | ||
| 197 | # object for a package name. The first argument is the class name, | ||
| 198 | # or the caller's package if undefined and we look it up in $CLASSES. | ||
| 199 | # If we get a second argument then we're being asked to lookup an | ||
| 200 | # entry for a subclass of Badger::Class, e.g. Badger::Factory::Class, | ||
| 201 | # so we first lookup the correct $METACLASS table. | ||
| 202 | my $class_sub = sub { | ||
| 203 | $class = @_ ? shift : (caller())[0]; | ||
| 204 | $class = ref $class || $class; | ||
| 205 | return @_ | ||
| 206 | ? $METACLASSES->{ $_[0] }->{ $class } ||= $_[0]->new($class) | ||
| 207 | : $CLASSES->{ $class } ||= $pkg->new($class); | ||
| 208 | }; | ||
| 209 | |||
| 210 | # The classes() method returns a list of Badger::Class objects for | ||
| 211 | # each class in the inheritance chain, starting with the object | ||
| 212 | # itself, followed by each base class, their base classes, and so on. | ||
| 213 | # As with class(), we use a generator to create a closure for the | ||
| 214 | # subroutine to allow the the class object name to be parameterised. | ||
| 215 | my $classes_sub = sub { | ||
| 216 | $class = shift || (caller())[0]; | ||
| 217 | $class_sub->($class)->heritage; | ||
| 218 | }; | ||
| 219 | |||
| 220 | no strict REFS; | ||
| 221 | no warnings 'redefine'; | ||
| 222 | *{ $pkg.PKG.'CLASS' } = \&CLASS; | ||
| 223 | *{ $pkg.PKG.'class' } = $class_sub; | ||
| 224 | *{ $pkg.PKG.'bclass' } = $class_sub; # plan B | ||
| 225 | *{ $pkg.PKG.'classes' } = $classes_sub; | ||
| 226 | *{ $pkg.PKG.'_autoload' } = \&_autoload; | ||
| 227 | |||
| 228 | $pkg->export_any('CLASS', 'class', 'bclass', 'classes'); | ||
| 229 | } | ||
| 230 | |||
| 231 | # call the UBER method to generate class() and classes() for this module | ||
| 232 | __PACKAGE__->UBER; | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | #----------------------------------------------------------------------- | ||
| 238 | # generate additional delegate methods listed in $DELEGATES | ||
| 239 | #----------------------------------------------------------------------- | ||
| 240 | |||
| 241 | class(CLASS)->methods( | ||
| 242 | map { | ||
| 243 | my $info = $DELEGATES->{ $_ }; | ||
| 244 | my ($module, $method) = @$info; | ||
| 245 | $_ => sub { | ||
| 246 | my $self = shift; | ||
| 247 | _autoload($self->$module)->$method($self->{ name }, @_); | ||
| 248 | return $self; | ||
| 249 | }; | ||
| 250 | } | ||
| 251 | keys %$DELEGATES | ||
| 252 | ); | ||
| 253 | |||
| 254 | |||
| 255 | #----------------------------------------------------------------------- | ||
| 256 | # constructor method | ||
| 257 | #----------------------------------------------------------------------- | ||
| 258 | |||
| 259 | sub new { | ||
| 260 | my ($class, $package) = @_; | ||
| 261 | $package = ref $package || $package; | ||
| 262 | no strict 'refs'; | ||
| 263 | bless { | ||
| 264 | name => $package, | ||
| 265 | symbols => \%{"${package}::"}, | ||
| 266 | }, $class; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub id { | ||
| 270 | my $self = shift; | ||
| 271 | return @_ | ||
| 272 | ? $self->{ id } = shift | ||
| 273 | : $self->{ id } ||= do { | ||
| 274 | my $pkg = $self->{ name }; | ||
| 275 | my $base = $self->base_id; # base to remove, e.g. Badger | ||
| 276 | if ($base eq $pkg) { | ||
| 277 | $pkg = $1 if $pkg =~ /(\w+)$/; # Badger - Badger --> Badger | ||
| 278 | } else { | ||
| 279 | $pkg =~ s/^${base}:://; # Badger::X::Y - Badger --> X::Y | ||
| 280 | } | ||
| 281 | $pkg =~ s/::/./g; # X::Y --> X.Y | ||
| 282 | lc $pkg; # X.Y --> x.y | ||
| 283 | }; | ||
| 284 | } | ||
| 285 | |||
| 286 | |||
| 287 | #----------------------------------------------------------------------- | ||
| 288 | # methods to access symbol table | ||
| 289 | #----------------------------------------------------------------------- | ||
| 290 | |||
| 291 | *pkg = \&name; | ||
| 292 | sub name { $_[0]->{ name } } | ||
| 293 | sub symbols { $_[0]->{ symbols } } | ||
| 294 | sub symbol { $_[0]->{ symbols }->{ $_[1] } } | ||
| 295 | sub scalar_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ SCALAR } } | ||
| 296 | sub array_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ ARRAY } } | ||
| 297 | sub hash_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ HASH } } | ||
| 298 | sub code_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ CODE } } | ||
| 299 | sub glob_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ GLOB } } | ||
| 300 | sub scalar { ${ scalar_ref(@_) || return } } | ||
| 301 | sub array { @{ array_ref(@_) || return } } | ||
| 302 | sub hash { %{ hash_ref(@_) || return } } | ||
| 303 | |||
| 304 | sub import_symbol { | ||
| 305 | my ($self, $symbol, $ref) = @_; | ||
| 306 | no strict REFS; | ||
| 307 | no warnings ONCE; | ||
| 308 | *{ $self->{ name }.PKG.$symbol } = $ref; | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | #----------------------------------------------------------------------- | ||
| 313 | # methods for accessing class variables that DTRT in subclasses | ||
| 314 | #----------------------------------------------------------------------- | ||
| 315 | |||
| 316 | sub var { | ||
| 317 | my $self = shift; | ||
| 318 | my $name = shift; | ||
| 319 | no strict REFS; | ||
| 320 | no warnings ONCE; | ||
| 321 | |||
| 322 | # _debug("Looking for $self->{ name }", PKG, $name, " args: ", scalar(@_), " => ", join(', ', @_), "\n"); | ||
| 323 | return @_ | ||
| 324 | ? (${ $self->{name}.PKG.$name } = shift) | ||
| 325 | : ${ $self->{name}.PKG.$name }; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub var_default { | ||
| 329 | my ($self, $name, $default) = @_; | ||
| 330 | no strict REFS; | ||
| 331 | no warnings ONCE; | ||
| 332 | |||
| 333 | return ${ $self->{name}.PKG.$name } | ||
| 334 | ||= $default; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub any_var { | ||
| 338 | my $self = shift; | ||
| 339 | my $name = shift; | ||
| 340 | no strict REFS; | ||
| 341 | |||
| 342 | # remove any leading '$' | ||
| 343 | $name =~ s/^\$//; | ||
| 344 | |||
| 345 | foreach my $pkg ($self->heritage) { | ||
| 346 | _debug("looking for $name in $pkg\n") if DEBUG; | ||
| 347 | return ${ $pkg.PKG.$name } if defined ${ $pkg.PKG.$name }; | ||
| 348 | } | ||
| 349 | |||
| 350 | return undef; | ||
| 351 | } | ||
| 352 | |||
| 353 | sub any_var_in { | ||
| 354 | my $self = shift; | ||
| 355 | my $names = @_ == 1 ? shift : [@_]; | ||
| 356 | my ($pkg, $name); | ||
| 357 | no strict REFS; | ||
| 358 | |||
| 359 | $names = [ split DELIMITER, $names ] | ||
| 360 | unless ref $names eq ARRAY; | ||
| 361 | |||
| 362 | # remove any leading '$' | ||
| 363 | $names = [ map { s/^\$//; $_ } @$names ]; | ||
| 364 | |||
| 365 | foreach $pkg ($self->heritage) { | ||
| 366 | foreach $name (@$names) { | ||
| 367 | _debug("looking for $name in $pkg\n") if DEBUG; | ||
| 368 | return ${ $pkg.PKG.$name } if defined ${ $pkg.PKG.$name }; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | |||
| 372 | return undef; | ||
| 373 | } | ||
| 374 | |||
| 375 | sub all_vars { | ||
| 376 | my ($self, $name) = @_; | ||
| 377 | my $pkg = $self->{ name }; | ||
| 378 | my ($value, @values); | ||
| 379 | no strict REFS; | ||
| 380 | no warnings ONCE; | ||
| 381 | |||
| 382 | # remove any leading '$' | ||
| 383 | $name =~ s/^\$//; | ||
| 384 | |||
| 385 | # _debug("all_vars() caller: ", join(', ', caller()), "\n"); | ||
| 386 | |||
| 387 | foreach my $pkg ($self->heritage) { | ||
| 388 | _debug("looking for $name in ", $pkg || "UNDEF", "\n") if DEBUG; | ||
| 389 | push(@values, $value) | ||
| 390 | if defined ($value = ${ $pkg.PKG.$name }); | ||
| 391 | _debug("got: $value\n") if DEBUG && $value; | ||
| 392 | } | ||
| 393 | |||
| 394 | return wantarray ? @values : \@values; | ||
| 395 | |||
| 396 | } | ||
| 397 | |||
| 398 | sub list_vars { | ||
| 399 | my $self = shift; # must remove these from @_ here | ||
| 400 | my $name = shift; | ||
| 401 | my $vars = $self->all_vars($name); | ||
| 402 | my (@merged, $list); | ||
| 403 | |||
| 404 | # remove any leading '$' | ||
| 405 | $name =~ s/^\$//; | ||
| 406 | |||
| 407 | foreach $list (@_, @$vars) { # use whatever is left in @_ here | ||
| 408 | next unless defined $list; | ||
| 409 | if (ref $list eq ARRAY) { | ||
| 410 | next unless @$list; | ||
| 411 | push(@merged, @$list); | ||
| 412 | } | ||
| 413 | else { | ||
| 414 | push(@merged, $list); | ||
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 418 | # return \@merged; | ||
| 419 | |||
| 420 | # NOTE TO SELF: this causes problems when doing something like | ||
| 421 | # foo( something_that_calls_list_vars() ) because list_vars assumed | ||
| 422 | # list context when we actually want a scalar ref. Must find where | ||
| 423 | # this is and fix it. | ||
| 424 | return wantarray ? @merged : \@merged; | ||
| 425 | |||
| 426 | } | ||
| 427 | |||
| 428 | sub hash_vars { | ||
| 429 | my $self = shift; # must remove these from @_ here | ||
| 430 | my $name = shift; | ||
| 431 | my $vars = $self->all_vars($name); | ||
| 432 | my (%merged, $hash); | ||
| 433 | |||
| 434 | # remove any leading '$' | ||
| 435 | $name =~ s/^\$//; | ||
| 436 | |||
| 437 | # reverse the package vars so we get base classes first, followed by subclass, | ||
| 438 | # then we add any additional arguments on as well in the order specified | ||
| 439 | foreach $hash ( reverse(@$vars), @_ ) { | ||
| 440 | next unless defined $hash; | ||
| 441 | unless (ref $hash eq HASH) { | ||
| 442 | warn "Ignoring $name configuration option (not a hash ref): $hash\n"; | ||
| 443 | next; | ||
| 444 | } | ||
| 445 | @merged{ keys %$hash } = values %$hash; | ||
| 446 | } | ||
| 447 | |||
| 448 | return \%merged; | ||
| 449 | } | ||
| 450 | |||
| 451 | sub hash_value { | ||
| 452 | my ($self, $name, $item, $default) = @_; | ||
| 453 | |||
| 454 | # remove any leading '$' | ||
| 455 | $name =~ s/^\$//; | ||
| 456 | |||
| 457 | # _debug("hash_value() caller: ", join(', ', caller()), "\n"); | ||
| 458 | |||
| 459 | foreach my $hash ($self->all_vars($name)) { | ||
| 460 | next unless ref $hash eq HASH; | ||
| 461 | return $hash->{ $item } | ||
| 462 | if defined $hash->{ $item }; | ||
| 463 | } | ||
| 464 | |||
| 465 | return $default; | ||
| 466 | } | ||
| 467 | |||
| 468 | |||
| 469 | #----------------------------------------------------------------------- | ||
| 470 | # Methods to return immediate parent classes and all ancestor classes. | ||
| 471 | #----------------------------------------------------------------------- | ||
| 472 | |||
| 473 | sub parents { | ||
| 474 | my $self = shift; | ||
| 475 | my $class = ref $self || $self; | ||
| 476 | my $pkg = $self->{ name }; | ||
| 477 | my $parents = $self->{ parents } ||= do { | ||
| 478 | no strict REFS; | ||
| 479 | |||
| 480 | # make sure the module is loaded before we go looking at its @ISA | ||
| 481 | _autoload($pkg); | ||
| 482 | [ | ||
| 483 | map { class($_) } # parents are immediate | ||
| 484 | @{ $pkg.PKG.ISA } # superclasses defined in @ISA | ||
| 485 | ]; | ||
| 486 | }; | ||
| 487 | |||
| 488 | return wantarray | ||
| 489 | ? @$parents | ||
| 490 | : $parents; | ||
| 491 | } | ||
| 492 | |||
| 493 | sub heritage { | ||
| 494 | my $self = shift; | ||
| 495 | my $heritage = $self->{ heritage } ||= do { | ||
| 496 | my @pending = ($self); | ||
| 497 | my (%seen, $item, @order); | ||
| 498 | while (@pending) { | ||
| 499 | next unless defined ($item = pop @pending); | ||
| 500 | unshift(@order, $item); | ||
| 501 | push(@pending, reverse @{ $item->parents }); | ||
| 502 | } | ||
| 503 | [ reverse grep { ! $seen{$_}++ } @order ]; | ||
| 504 | }; | ||
| 505 | return wantarray | ||
| 506 | ? @$heritage | ||
| 507 | : $heritage; | ||
| 508 | } | ||
| 509 | |||
| 510 | |||
| 511 | #----------------------------------------------------------------------- | ||
| 512 | # class configuration methods - also available as import hooks | ||
| 513 | #----------------------------------------------------------------------- | ||
| 514 | |||
| 515 | sub base { | ||
| 516 | my $self = shift; | ||
| 517 | my $bases = @_ == 1 ? shift : [ @_ ]; | ||
| 518 | my $pkg = $self->{ name }; | ||
| 519 | |||
| 520 | $bases = [ split(DELIMITER, $bases) ] | ||
| 521 | unless ref $bases eq ARRAY; | ||
| 522 | |||
| 523 | # add each of $bases to @ISA and autoload it | ||
| 524 | foreach my $base (@$bases) { | ||
| 525 | no strict REFS; | ||
| 526 | next if $pkg->isa($base); | ||
| 527 | _debug("Adding $pkg base class $base\n") if DEBUG; | ||
| 528 | push @{ $pkg.PKG.ISA }, $base; | ||
| 529 | _autoload($base); | ||
| 530 | } | ||
| 531 | return $self; | ||
| 532 | } | ||
| 533 | |||
| 534 | sub mixin { | ||
| 535 | my $self = shift; | ||
| 536 | my $mixins = @_ == 1 ? shift : [ @_ ]; | ||
| 537 | |||
| 538 | $mixins = [ split(DELIMITER, $mixins) ] | ||
| 539 | unless ref $mixins eq ARRAY; | ||
| 540 | |||
| 541 | foreach my $name (@$mixins) { | ||
| 542 | # $name = $target . $name if $name =~ /^::/; | ||
| 543 | # $self->debug("mixing $name into $self\n") if $DEBUG; | ||
| 544 | _autoload($name)->mixin($self->{ name }); | ||
| 545 | } | ||
| 546 | |||
| 547 | return $self; | ||
| 548 | } | ||
| 549 | |||
| 550 | sub mixins { | ||
| 551 | my $self = shift; | ||
| 552 | $self->base(MIXIN); | ||
| 553 | $self->{ name }->mixins(@_); | ||
| 554 | return $self; | ||
| 555 | |||
| 556 | my $syms = @_ == 1 ? shift : [ @_ ]; | ||
| 557 | my $mixins = $self->var_default(MIXINS, [ ]); | ||
| 558 | |||
| 559 | $syms = [ split(DELIMITER, $syms) ] | ||
| 560 | unless ref $syms eq ARRAY; | ||
| 561 | |||
| 562 | # $mixins->{ $_ } | ||
| 563 | push(@$mixins, @$syms); | ||
| 564 | |||
| 565 | $self->debug("$self MIXINS are: ", $self->dump_data_inline($mixins), "\n") if DEBUG; | ||
| 566 | |||
| 567 | $self->exports( any => $syms ); | ||
| 568 | |||
| 569 | } | ||
| 570 | |||
| 571 | sub version { | ||
| 572 | my ($self, $version) = @_; | ||
| 573 | my $pkg = $self->{ name }; | ||
| 574 | no strict 'refs'; | ||
| 575 | _debug("Defining $pkg version $version\n") if DEBUG; | ||
| 576 | |||
| 577 | # define $VERSION and VERSION() | ||
| 578 | *{ $pkg.PKG.VERSION } = \$version | ||
| 579 | unless defined ${ $pkg.PKG.VERSION } | ||
| 580 | && ${ $pkg.PKG.VERSION }; | ||
| 581 | *{ $pkg.PKG.VERSION } = sub() { $version } | ||
| 582 | unless defined &{ $pkg.PKG.VERSION }; # CHECK THIS - was 'version' | ||
| 583 | |||
| 584 | return $self; | ||
| 585 | } | ||
| 586 | |||
| 587 | sub constant { | ||
| 588 | my $self = shift; | ||
| 589 | my $constants = @_ == 1 ? shift : { @_ }; | ||
| 590 | my $pkg = $self->{ name }; | ||
| 591 | |||
| 592 | # split string into pairs of assignments, e.g. "foo=bar, baz=bam" | ||
| 593 | $constants = { | ||
| 594 | map { split /\s*=>?\s*/ } | ||
| 595 | split(DELIMITER, $constants) | ||
| 596 | } unless ref $constants eq HASH; | ||
| 597 | |||
| 598 | |||
| 599 | while (my ($name, $value) = each %$constants) { | ||
| 600 | no strict REFS; | ||
| 601 | my $v = $value; # new lexical variable to bind in closure | ||
| 602 | _debug("Defining $pkg constant $name => $value\n") if DEBUG; | ||
| 603 | *{ $pkg.PKG.$name } = sub() { $value }; | ||
| 604 | } | ||
| 605 | return $self; | ||
| 606 | } | ||
| 607 | |||
| 608 | sub words { | ||
| 609 | my $self = shift; | ||
| 610 | my $words = @_ == 1 ? shift : [ @_ ]; | ||
| 611 | my $pkg = $self->{ name }; | ||
| 612 | |||
| 613 | $words = [ split(DELIMITER, $words) ] | ||
| 614 | unless ref $words eq ARRAY; | ||
| 615 | |||
| 616 | foreach (@$words) { | ||
| 617 | no strict REFS; | ||
| 618 | my $word = $_; # new lexical variable to bind in closure | ||
| 619 | _debug("Defining $pkg word $word\n") if DEBUG; | ||
| 620 | *{ $pkg.PKG.$word } = sub() { $word }; | ||
| 621 | } | ||
| 622 | return $self; | ||
| 623 | } | ||
| 624 | |||
| 625 | sub exports { | ||
| 626 | my $self = shift; | ||
| 627 | my $pkg = $self->{ name }; | ||
| 628 | $self->base(EXPORTER); | ||
| 629 | $pkg->exports(@_); | ||
| 630 | return $self; | ||
| 631 | } | ||
| 632 | |||
| 633 | sub throws { | ||
| 634 | my ($self, $throws) = @_; | ||
| 635 | $self->import_symbol(THROWS, \$throws); | ||
| 636 | return $self; | ||
| 637 | } | ||
| 638 | |||
| 639 | sub messages { | ||
| 640 | my $self = shift; | ||
| 641 | my $args = @_ && ref $_[0] eq HASH ? shift : { @_ }; | ||
| 642 | my $pkg = $self->{ name }; | ||
| 643 | no strict REFS; | ||
| 644 | no warnings ONCE; | ||
| 645 | |||
| 646 | # if there aren't any existing $MESSAGES then we can store | ||
| 647 | # $messages in it and be done, otherwise we have to merge. | ||
| 648 | my $messages = ${ $pkg.PKG.MESSAGES }; | ||
| 649 | |||
| 650 | if ($messages) { | ||
| 651 | _debug("merging $pkg messages: ", join(', ', keys %$args), "\n") if DEBUG; | ||
| 652 | @$messages{ keys %$args } = values %$args; | ||
| 653 | } | ||
| 654 | else { | ||
| 655 | _debug("adding $pkg messages: ", join(', ', keys %$args), "\n") if DEBUG; | ||
| 656 | ${ $pkg.PKG.MESSAGES } = $messages = $args; | ||
| 657 | } | ||
| 658 | |||
| 659 | return $self; | ||
| 660 | } | ||
| 661 | |||
| 662 | sub method { | ||
| 663 | my $self = shift; | ||
| 664 | my $name = shift; | ||
| 665 | no strict REFS; | ||
| 666 | no warnings 'redefine'; | ||
| 667 | |||
| 668 | # method($name) can be used to fetch a method/sub | ||
| 669 | return $self->{ name }->can($name) | ||
| 670 | unless @_; | ||
| 671 | |||
| 672 | # method($name => $code) or $method($name => $value) to define method | ||
| 673 | my $code = shift; | ||
| 674 | _debug("defining method: $self\::$name => $code\n") if DEBUG; | ||
| 675 | |||
| 676 | *{ $self->{name}.PKG.$name } = ref $code eq CODE | ||
| 677 | ? $code | ||
| 678 | : sub { $code }; # constant method returns value | ||
| 679 | |||
| 680 | return $self; | ||
| 681 | } | ||
| 682 | |||
| 683 | sub methods { | ||
| 684 | my $self = shift; | ||
| 685 | my $args = @_ && ref $_[0] eq HASH ? shift : { @_ }; | ||
| 686 | my $pkg = $self->{ name }; | ||
| 687 | no strict REFS; | ||
| 688 | no warnings 'redefine'; | ||
| 689 | |||
| 690 | while (my ($name, $code) = each %$args) { | ||
| 691 | _debug("defining method: $self\::$name => $code\n") if DEBUG; | ||
| 692 | *{ $pkg.PKG.$name } | ||
| 693 | = ref $code eq CODE ? $code : sub { $code }; | ||
| 694 | } | ||
| 695 | return $self; | ||
| 696 | } | ||
| 697 | |||
| 698 | sub alias { | ||
| 699 | my $self = shift; | ||
| 700 | my $args = @_ && ref $_[0] eq HASH ? shift : { @_ }; | ||
| 701 | my $pkg = $self->{ name }; | ||
| 702 | no strict REFS; | ||
| 703 | |||
| 704 | while (my ($names, $code) = each %$args) { | ||
| 705 | _debug("defining alias: $self\::$names => $code\n") if DEBUG; | ||
| 706 | $code = $self->method($code) | ||
| 707 | || croak "Invalid method specified for '$names' alias: $code" | ||
| 708 | unless ref $code eq CODE; | ||
| 709 | foreach my $name (split(DELIMITER, $names)) { | ||
| 710 | *{ $pkg.PKG.$name } = $code; | ||
| 711 | } | ||
| 712 | } | ||
| 713 | return $self; | ||
| 714 | } | ||
| 715 | |||
| 716 | sub overload { | ||
| 717 | my $self = shift; | ||
| 718 | my $args = @_ && ref $_[0] eq HASH ? shift : { @_ }; | ||
| 719 | _debug("overload on $self->{name} : { ", join(', ', %$args), " }\n") if DEBUG; | ||
| 720 | overload::OVERLOAD($self->{name}, %$args); | ||
| 721 | return $self; | ||
| 722 | } | ||
| 723 | |||
| 724 | sub as_text { | ||
| 725 | my ($self, $method) = @_; | ||
| 726 | $self->overload( '""' => $method, fallback => 1 ); | ||
| 727 | } | ||
| 728 | |||
| 729 | sub is_true { | ||
| 730 | my ($self, $arg) = @_; | ||
| 731 | my $method = | ||
| 732 | $arg eq FALSE ? \&FALSE : # allow 0/1 as shortcut | ||
| 733 | $arg eq TRUE ? \&TRUE : | ||
| 734 | $arg; | ||
| 735 | $self->overload( bool => $method, fallback => 1 ); | ||
| 736 | } | ||
| 737 | |||
| 738 | |||
| 739 | #----------------------------------------------------------------------- | ||
| 740 | # misc methods | ||
| 741 | #----------------------------------------------------------------------- | ||
| 742 | |||
| 743 | sub instance { | ||
| 744 | my $self = shift; | ||
| 745 | $self->{ name }->new(@_); | ||
| 746 | } | ||
| 747 | |||
| 748 | sub loaded { | ||
| 749 | # "loaded" is defined as "has an entry in the symbol table" | ||
| 750 | # NOTE: this is incorrect - see comment in _autoload() wrt | ||
| 751 | # case-insensitive filesystems | ||
| 752 | keys %{ $_[0]->{ symbols } } ? 1 : 0; | ||
| 753 | } | ||
| 754 | |||
| 755 | sub load { | ||
| 756 | my $self = shift; | ||
| 757 | _autoload($self->{ name }) || return; | ||
| 758 | return $self; | ||
| 759 | } | ||
| 760 | |||
| 761 | sub maybe_load { | ||
| 762 | my $self = shift; | ||
| 763 | return eval { $self->load } || do { | ||
| 764 | _debug("maybe_load($self) caught error: $@\n") if DEBUG; | ||
| 765 | # Don't confuse "Can't locate Missing/Module/Used/In/Your/Module.pm" | ||
| 766 | # messages with "Can't locate Your/Module.pm". The former is an | ||
| 767 | # error that should be reported, the latter isn't. We convert the | ||
| 768 | # class name to a regex that matches any non-word directory separators | ||
| 769 | # e.g. Your::Module => Your\W+Module | ||
| 770 | my $name = join( | ||
| 771 | "\\W+", | ||
| 772 | map { quotemeta $_ } | ||
| 773 | split('::', $self->{ name }) | ||
| 774 | ); | ||
| 775 | _debug("checking to see if we couldn't locate $name\n") if DEBUG; | ||
| 776 | croak $@ if $@ && $@ !~ /^Can't locate $name.*? in \@INC/; | ||
| 777 | 0; | ||
| 778 | } | ||
| 779 | } | ||
| 780 | |||
| 781 | |||
| 782 | #----------------------------------------------------------------------- | ||
| 783 | # methods for building Badger::Class subclasses | ||
| 784 | #----------------------------------------------------------------------- | ||
| 785 | |||
| 786 | sub uber { | ||
| 787 | my ($self, $base) = @_; | ||
| 788 | my $pkg = $self->{ name }; | ||
| 789 | $self->base($base); | ||
| 790 | $pkg->UBER; | ||
| 791 | return $self; | ||
| 792 | } | ||
| 793 | |||
| 794 | sub hooks { | ||
| 795 | my $self = shift; | ||
| 796 | my $args = @_ == 1 ? shift : { @_ }; | ||
| 797 | my $hooks = $self->var_default( HOOKS => { } ); | ||
| 798 | |||
| 799 | # split string into list ref | ||
| 800 | $args = [ split(DELIMITER, $args) ] | ||
| 801 | unless ref $args; | ||
| 802 | |||
| 803 | # map list ref to hash ref | ||
| 804 | $args = { | ||
| 805 | map { $_ => $_ } | ||
| 806 | @$args | ||
| 807 | } if ref $args eq ARRAY; | ||
| 808 | |||
| 809 | croak("Invalid hooks specified: $args") | ||
| 810 | unless ref $args eq HASH; | ||
| 811 | |||
| 812 | _debug("merging $self->{ name } hooks: ", join(', ', keys %$args), "\n") if DEBUG; | ||
| 813 | |||
| 814 | @$hooks{ keys %$args } = values %$args; | ||
| 815 | |||
| 816 | return $self; | ||
| 817 | } | ||
| 818 | |||
| 819 | |||
| 820 | #----------------------------------------------------------------------- | ||
| 821 | # autoload($module) | ||
| 822 | # | ||
| 823 | # Helper subroutine to autoload a module. | ||
| 824 | #----------------------------------------------------------------------- | ||
| 825 | |||
| 826 | sub _autoload { | ||
| 827 | my $class = shift; | ||
| 828 | no strict REFS; | ||
| 829 | no warnings ONCE; | ||
| 830 | my $symbols = \%{"${class}::"}; | ||
| 831 | |||
| 832 | unless ( | ||
| 833 | defined ${ $class.PKG.LOADED } | ||
| 834 | || scalar(grep { ! /::$/ } keys %$symbols) > 1 # any symbols defined other than import / sub-namespaces | ||
| 835 | ) { | ||
| 836 | _debug("autoloading $class\n") if DEBUG; | ||
| 837 | local $SIG{__DIE__}; | ||
| 838 | eval "use $class"; | ||
| 839 | croak $@ if $@; | ||
| 840 | |||
| 841 | # Problem here is that case-insensitive filesystems could load the | ||
| 842 | # wrong module. We could check the symbol table, but it will always | ||
| 843 | # have an 'import' entry because 'use' attempts to call import(). | ||
| 844 | # So we assume a successful module load requires there to be a symbol | ||
| 845 | # table with entries other than a single 'import' | ||
| 846 | # [later] Oh blimey, it's worse than that. There may be other | ||
| 847 | # sub-namespaces. | ||
| 848 | return 0 | ||
| 849 | if scalar(grep { ! /::$/ } keys %$symbols) == 1 | ||
| 850 | && exists $symbols->{ import }; | ||
| 851 | |||
| 852 | ${ $class.PKG.LOADED } ||= 1; | ||
| 853 | } | ||
| 854 | |||
| 855 | return $class; | ||
| 856 | } | ||
| 857 | |||
| 858 | sub _debug { | ||
| 859 | print STDERR @_; | ||
| 860 | } | ||
| 861 | |||
| 862 | |||
| 863 | 1; | ||
| 864 | |||
| 865 | __END__ |