| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Base | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Base class module implementing common functionality for various | ||
| 7 | # other Badger modules. | ||
| 8 | # | ||
| 9 | # AUTHOR | ||
| 10 | # Andy Wardley <abw@wardley.org> | ||
| 11 | # | ||
| 12 | #======================================================================== | ||
| 13 | |||
| 14 | package Badger::Base; | ||
| 15 | |||
| 16 | use Badger::Class | ||
| 17 | version => 0.01, | ||
| 18 | debug => 0, | ||
| 19 | constants => 'CODE HASH ARRAY BLANK SPACE PKG REFS ONCE WARN NONE', | ||
| 20 | import => 'class classes', | ||
| 21 | utils => 'blessed reftype xprintf', | ||
| 22 | words => 'ID EXCEPTION THROWS ERROR DECLINED before after', | ||
| 23 | constant => { | ||
| 24 | base_id => 'Badger', # stripped from class name to make id | ||
| 25 | TRIAL => 'Badger::Base::Trial', | ||
| 26 | }; | ||
| 27 | |||
| 28 | use Badger::Exception; # TODO: autoload | ||
| 29 | use Badger::Debug 'debug debug_up dump_data_inline dump_data dump_list dump_hash'; | ||
| 30 | |||
| 31 | our $EXCEPTION = 'Badger::Exception' unless defined $EXCEPTION; | ||
| 32 | our $ON_WARN = WARN; | ||
| 33 | our $MESSAGES = { | ||
| 34 | not_found => '%s not found: %s', | ||
| 35 | not_found_in => '%s not found in %s', | ||
| 36 | not_implemented => '%s is not implemented %s', | ||
| 37 | no_component => 'No %s component defined', | ||
| 38 | bad_method => "Invalid method '%s' called on %s at %s line %s", | ||
| 39 | invalid => 'Invalid %s specified: %s', | ||
| 40 | unexpected => 'Invalid %s specified: %s (expected a %s)', | ||
| 41 | missing_to => 'No %s specified to %s', | ||
| 42 | missing => 'No %s specified', | ||
| 43 | todo => '%s is TODO %s', | ||
| 44 | at_line => '%s at line %s', | ||
| 45 | at_file_line => '%s in %s at line %s', | ||
| 46 | }; | ||
| 47 | |||
| 48 | |||
| 49 | sub new { | ||
| 50 | my $class = shift; | ||
| 51 | |||
| 52 | # install warning handling for odd number of parameters when DEBUG enabled | ||
| 53 | local $SIG{__WARN__} = sub { | ||
| 54 | Badger::Utils::odd_params(@_); | ||
| 55 | } if DEBUG; | ||
| 56 | |||
| 57 | my $args = @_ && ref $_[0] eq HASH ? shift : { @_ }; | ||
| 58 | my $self = bless { }, ref $class || $class; | ||
| 59 | $self = $self->init($args); | ||
| 60 | |||
| 61 | # be careful to account for object that overload the boolean comparison | ||
| 62 | # operator and may return false to a simple truth test. | ||
| 63 | return defined $self | ||
| 64 | ? $self | ||
| 65 | : $self->error("init() method failed\n"); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub init { | ||
| 69 | my $self = shift; | ||
| 70 | # default action is to store reference to entire configuration so | ||
| 71 | # that methods can examine it later if they need to | ||
| 72 | $self->{ config } = shift; | ||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub warn { | ||
| 77 | my $self = shift; | ||
| 78 | return unless @_; | ||
| 79 | |||
| 80 | my $message = join(BLANK, @_); | ||
| 81 | my $handlers = $self->on_warn; | ||
| 82 | |||
| 83 | $self->debug("dispatching handlers for warn: ", $self->dump_data_inline($handlers), "\n") if DEBUG; | ||
| 84 | $self->_dispatch_handlers( warn => $handlers => $message ) | ||
| 85 | if $handlers && @$handlers; | ||
| 86 | |||
| 87 | # Warning is usually raised by the last handler in the chain which | ||
| 88 | # defaults to 'warn', so it's OK to just drop out here. | ||
| 89 | } | ||
| 90 | |||
| 91 | sub error { | ||
| 92 | my $self = shift; | ||
| 93 | my $class = ref $self || $self; | ||
| 94 | my $type = reftype $self || BLANK; | ||
| 95 | no strict REFS; | ||
| 96 | no warnings ONCE; | ||
| 97 | |||
| 98 | if (@_) { | ||
| 99 | # don't stringify objects passed as argument | ||
| 100 | my $message = ref $_[0] ? shift : join(BLANK, map { defined($_) ? $_ : BLANK } @_); | ||
| 101 | my $handlers = $self->on_error; | ||
| 102 | |||
| 103 | # set package variable | ||
| 104 | ${ $class.PKG.ERROR } = $message; | ||
| 105 | ${ $class.PKG.DECLINED } = 0; | ||
| 106 | |||
| 107 | if ($type eq HASH) { | ||
| 108 | # set ERROR and DECLINED items in object | ||
| 109 | $self->{ ERROR } = $message; | ||
| 110 | $self->{ DECLINED } = 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | ($message) = $self->_dispatch_handlers( error => $handlers => $message ) | ||
| 114 | if $handlers && @$handlers; | ||
| 115 | |||
| 116 | $self->throw($message); | ||
| 117 | } | ||
| 118 | elsif ($type eq HASH) { | ||
| 119 | return $self->{ ERROR }; | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | return ${ $class.PKG.ERROR }; | ||
| 123 | } | ||
| 124 | # not reached | ||
| 125 | } | ||
| 126 | |||
| 127 | sub decline { | ||
| 128 | my $self = shift; | ||
| 129 | my $class = ref $self || $self; | ||
| 130 | my $type = reftype $self || BLANK; | ||
| 131 | my $reason = @_ == 1 ? shift : join(BLANK, @_); | ||
| 132 | no strict REFS; | ||
| 133 | no warnings ONCE; | ||
| 134 | |||
| 135 | ${ $class.PKG.ERROR } = $reason; | ||
| 136 | ${ $class.PKG.DECLINED } = 1; | ||
| 137 | |||
| 138 | if ($type eq HASH) { | ||
| 139 | $self->{ ERROR } = $reason; | ||
| 140 | $self->{ DECLINED } = 1; | ||
| 141 | } | ||
| 142 | |||
| 143 | return undef; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub declined { | ||
| 147 | my $self = shift; | ||
| 148 | my $class = ref $self || $self; | ||
| 149 | my $type = reftype $self || BLANK; | ||
| 150 | no strict REFS; | ||
| 151 | return ($type eq HASH) | ||
| 152 | ? $self->{ DECLINED } | ||
| 153 | : ${ $class.PKG.DECLINED }; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub reason { | ||
| 157 | my $self = shift; | ||
| 158 | my $class = ref $self || $self; | ||
| 159 | my $type = reftype $self || BLANK; | ||
| 160 | no strict REFS; | ||
| 161 | return $type eq HASH | ||
| 162 | ? $self->{ ERROR } | ||
| 163 | : ${ $class.PKG.ERROR }; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub throw { | ||
| 167 | my $self = shift; | ||
| 168 | my $type = shift; | ||
| 169 | my $emod = $self->exception; | ||
| 170 | my $e; | ||
| 171 | |||
| 172 | # TODO: grok file/line/sub from caller and add to exceptions | ||
| 173 | |||
| 174 | if (! @_) { | ||
| 175 | # single argument can be an exception object or an error message | ||
| 176 | # which is given whatever type is returned by throws() | ||
| 177 | # | ||
| 178 | # throw($exception) | ||
| 179 | # throw($info) | ||
| 180 | |||
| 181 | if (blessed $type && $type->isa($emod)) { | ||
| 182 | $self->debug("returning exception object: ", ref $type, " => [$type]\n") if DEBUG; | ||
| 183 | $e = $type; | ||
| 184 | } | ||
| 185 | else { | ||
| 186 | $self->debug("creating new exception object chain: info => $type\n") if DEBUG; | ||
| 187 | $e = $emod->new( type => $self->throws, info => $type ); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | else { | ||
| 191 | # Next argument can also be an exception object (e.g. when chaining | ||
| 192 | # exceptions) or a regular info message. In the first case, we don't | ||
| 193 | # re-throw the exception if it's already of the correct $type (but we | ||
| 194 | # do if any extra arguments are provided) | ||
| 195 | # | ||
| 196 | # throw($type, $exception) | ||
| 197 | # throw($type, $info) | ||
| 198 | |||
| 199 | my $info = shift; | ||
| 200 | |||
| 201 | if (! @_ && blessed $info && $info->isa($emod) && $info->type eq $type) { | ||
| 202 | # second argument is already an exception of type $type | ||
| 203 | $e = $info; | ||
| 204 | } | ||
| 205 | else { | ||
| 206 | my $config = @_ && ref $_[0] eq HASH ? shift : { @_ }; | ||
| 207 | # construct a new exception from $type and $info fields | ||
| 208 | $config->{ type } = $type; | ||
| 209 | $config->{ info } = $info; | ||
| 210 | $self->debug("creating new exception object: ", $self->dump_hash($config), "\n") if DEBUG; | ||
| 211 | $e = $emod->new($config); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | $e->throw; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub try { | ||
| 218 | my $self = shift; | ||
| 219 | if (@_) { | ||
| 220 | my $method = shift; | ||
| 221 | if (wantarray) { | ||
| 222 | my @result = eval { $self->$method(@_) }; | ||
| 223 | $self->decline($@) if $@; | ||
| 224 | return @result; | ||
| 225 | } | ||
| 226 | else { | ||
| 227 | my $result = eval { $self->$method(@_) }; | ||
| 228 | $self->decline($@) if $@; | ||
| 229 | return $result; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | else { | ||
| 233 | return TRIAL->_bind_($self); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | sub catch { | ||
| 238 | # this depends on some code in Badger::Exception which I haven't | ||
| 239 | # written yet... | ||
| 240 | shift->todo; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub throws { | ||
| 244 | my $self = shift; | ||
| 245 | my $type = reftype $self || BLANK; | ||
| 246 | my $class = class($self); | ||
| 247 | my $throws; | ||
| 248 | |||
| 249 | if (@_) { | ||
| 250 | # hash objects store exception type in $self->{ THROWS }, anything | ||
| 251 | # else (classes and non-hash objects) use the $THROWS package var | ||
| 252 | $throws = $type eq HASH | ||
| 253 | ? ($self->{ THROWS } = shift) | ||
| 254 | : $class->var(THROWS, shift); | ||
| 255 | } | ||
| 256 | elsif ($type eq HASH) { | ||
| 257 | # we also look in $self->{ config } to see if a 'throws' was | ||
| 258 | # specified as a constructor argument. | ||
| 259 | $throws = $self->{ THROWS } | ||
| 260 | ||= $self->{ config } | ||
| 261 | && $self->{ config }->{ throws }; | ||
| 262 | } | ||
| 263 | |||
| 264 | # fall back on looking for any package variable in class / base classes | ||
| 265 | return $throws | ||
| 266 | || $class->any_var(THROWS) | ||
| 267 | || $class->id; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub exception { | ||
| 271 | my $self = shift; | ||
| 272 | my $type = reftype $self || BLANK; | ||
| 273 | my $emod; | ||
| 274 | |||
| 275 | # TODO: Move this into Template::Class. It's so rare that you want to | ||
| 276 | # set an exception type this way. Then we can have throw() pass the $type | ||
| 277 | # to exception() and allow subclasses to make a decision about what kind | ||
| 278 | # of exception to return based on the $type. | ||
| 279 | if (@_) { | ||
| 280 | # as per throws() above, we have to be careful to only treat $self | ||
| 281 | # like a hash when it is a hash-based object | ||
| 282 | $emod = $type eq HASH | ||
| 283 | ? ($self->{ EXCEPTION } = shift) | ||
| 284 | : class($self)->var(EXCEPTION, shift); | ||
| 285 | } | ||
| 286 | elsif ($type eq HASH) { | ||
| 287 | $emod = $self->{ EXCEPTION } | ||
| 288 | ||= $self->{ config } | ||
| 289 | && $self->{ config }->{ exception }; | ||
| 290 | } | ||
| 291 | return $emod | ||
| 292 | || class($self)->any_var(EXCEPTION); | ||
| 293 | } | ||
| 294 | |||
| 295 | sub fatal { | ||
| 296 | my $self = shift; | ||
| 297 | my $class = ref $self || $self; | ||
| 298 | my $error = join(BLANK, @_); | ||
| 299 | no strict REFS; | ||
| 300 | |||
| 301 | # set package variable | ||
| 302 | ${ $class.PKG.ERROR } = $error; | ||
| 303 | |||
| 304 | if (ref $self && reftype $self eq HASH) { | ||
| 305 | $self->{ ERROR } = $error; | ||
| 306 | $self->{ DECLINED } = 0; | ||
| 307 | } | ||
| 308 | |||
| 309 | require Carp; | ||
| 310 | Carp::confess("Fatal badger error: ", @_); | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | #----------------------------------------------------------------------- | ||
| 315 | # messages | ||
| 316 | #----------------------------------------------------------------------- | ||
| 317 | |||
| 318 | sub message { | ||
| 319 | my $self = shift; | ||
| 320 | my $name = shift | ||
| 321 | || $self->fatal("message() called without format name"); | ||
| 322 | my $ref = $self && reftype $self; | ||
| 323 | my $format; | ||
| 324 | |||
| 325 | # allow $self object to have an internal messages hash | ||
| 326 | if ($self && $ref && $ref eq HASH && $self->{ messages }) { | ||
| 327 | $format = $self->{ messages }->{ $name } | ||
| 328 | if reftype $self->{ messages } eq HASH; | ||
| 329 | } | ||
| 330 | |||
| 331 | $format = class($self)->hash_value( MESSAGES => $name ) | ||
| 332 | || $self->fatal("message() called with invalid message type: $name") | ||
| 333 | unless defined $format; | ||
| 334 | |||
| 335 | xprintf($format, @_); | ||
| 336 | } | ||
| 337 | |||
| 338 | sub warn_msg { | ||
| 339 | # explicitly quantify local message() method in case a subclass decides | ||
| 340 | # to re-implement the message() method to do something else | ||
| 341 | $_[0]->warn( message(@_) ); | ||
| 342 | } | ||
| 343 | |||
| 344 | sub error_msg { | ||
| 345 | $_[0]->error( message(@_) ); | ||
| 346 | } | ||
| 347 | |||
| 348 | sub fatal_msg { | ||
| 349 | $_[0]->fatal( message(@_) ); | ||
| 350 | } | ||
| 351 | |||
| 352 | sub decline_msg { | ||
| 353 | $_[0]->decline( message(@_) ); | ||
| 354 | } | ||
| 355 | |||
| 356 | sub debug_msg { | ||
| 357 | $_[0]->debug( message(@_) ); | ||
| 358 | } | ||
| 359 | |||
| 360 | sub throw_msg { | ||
| 361 | my $self = shift; | ||
| 362 | $self->throw( shift, message($self, @_) ); | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | #----------------------------------------------------------------------- | ||
| 367 | # generate not_implemented() and todo() methods | ||
| 368 | #----------------------------------------------------------------------- | ||
| 369 | |||
| 370 | class->methods( | ||
| 371 | map { | ||
| 372 | my $name = $_; | ||
| 373 | $name => sub { | ||
| 374 | my $self = shift; | ||
| 375 | my $ref = ref $self || $self; | ||
| 376 | my ($pkg, $file, $line, $sub) = caller(0); | ||
| 377 | $sub = (caller(1))[3]; # subroutine the caller was called from | ||
| 378 | $sub =~ s/(.*):://; | ||
| 379 | my $msg = @_ ? join(BLANK, SPACE, @_) : BLANK; | ||
| 380 | return $self->error_msg( $name => "$sub()$msg", "for $ref in $file at line $line" ); | ||
| 381 | }; | ||
| 382 | } | ||
| 383 | qw( not_implemented todo ) | ||
| 384 | ); | ||
| 385 | |||
| 386 | |||
| 387 | #----------------------------------------------------------------------- | ||
| 388 | # generate on_warn() and on_error() methods | ||
| 389 | #----------------------------------------------------------------------- | ||
| 390 | |||
| 391 | class->methods( | ||
| 392 | map { | ||
| 393 | my $on_event = $_; | ||
| 394 | my $ON_EVENT = uc $on_event; | ||
| 395 | |||
| 396 | $on_event => sub { | ||
| 397 | my $self = shift; | ||
| 398 | my $class = class($self); | ||
| 399 | my $list; | ||
| 400 | |||
| 401 | if (ref $self && reftype $self eq HASH) { | ||
| 402 | # look in $self->{ config }->{ on_xxx } or in $ON_XXX pkg | ||
| 403 | # var for one or more event handlers | ||
| 404 | $list = $self->{ $ON_EVENT } | ||
| 405 | ||= $self->{ config }->{ $on_event } | ||
| 406 | || $class->list_vars($ON_EVENT); | ||
| 407 | # careful! the config value might be a single handler | ||
| 408 | $list = $self->{ $ON_EVENT } = [$list] | ||
| 409 | unless ref $list eq ARRAY; | ||
| 410 | $self->debug("got $on_event handlers: ", $self->dump_data_inline($list), "\n") if DEBUG; | ||
| 411 | } | ||
| 412 | else { | ||
| 413 | # class method or non-hash objects use pkg vars only | ||
| 414 | $list = $class->var_default($ON_EVENT, []); | ||
| 415 | $list = $class->var($ON_EVENT, [$list]) | ||
| 416 | unless ref $list eq ARRAY; | ||
| 417 | } | ||
| 418 | |||
| 419 | # Add to the list any extra handlers passed as args. First | ||
| 420 | # argument can be 'before' or 'after' to add remaining args | ||
| 421 | # to start or end of list, otherwise the entire list is replaced. | ||
| 422 | if (@_) { | ||
| 423 | if ($_[0] eq before) { | ||
| 424 | shift; | ||
| 425 | unshift(@$list, @_); | ||
| 426 | } | ||
| 427 | elsif ($_[0] eq after) { | ||
| 428 | shift; | ||
| 429 | push(@$list, @_); | ||
| 430 | } | ||
| 431 | else { | ||
| 432 | @$list = @_; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | # push(@$list, @_); | ||
| 436 | |||
| 437 | return $list; | ||
| 438 | } | ||
| 439 | } | ||
| 440 | qw( on_warn on_error ) | ||
| 441 | ); | ||
| 442 | |||
| 443 | |||
| 444 | #----------------------------------------------------------------------- | ||
| 445 | # internal method to dispatch on_error/on_warning handlers | ||
| 446 | #----------------------------------------------------------------------- | ||
| 447 | |||
| 448 | sub _dispatch_handlers { | ||
| 449 | my ($self, $type, $handlers, @args) = @_; | ||
| 450 | |||
| 451 | $self->debug("_dispatch handlers: ", $self->dump_data_inline($handlers), "\n") if DEBUG; | ||
| 452 | |||
| 453 | foreach (@$handlers) { | ||
| 454 | my $handler = $_; # don't alias list items | ||
| 455 | $self->debug("dispatch handler: $handler\n") if DEBUG; | ||
| 456 | if (! ref $handler) { | ||
| 457 | if ($handler eq WARN) { # 'warn' - we make sure that the | ||
| 458 | my $msg = join('', @args); # message is newline terminated | ||
| 459 | chomp($msg); # to stop Perl from adding a line | ||
| 460 | CORE::warn $msg, "\n"; # number that'll be wrong. | ||
| 461 | } | ||
| 462 | elsif ($handler eq NONE) { # NONE/0 - bail out | ||
| 463 | last; | ||
| 464 | } | ||
| 465 | else { | ||
| 466 | $handler = $self->can($handler) | ||
| 467 | || return $self->fatal("Invalid on_$type method: $handler"); | ||
| 468 | @args = $handler->($self, @args); | ||
| 469 | } | ||
| 470 | } | ||
| 471 | elsif (ref $handler eq CODE) { | ||
| 472 | @args = $handler->(@args); | ||
| 473 | } | ||
| 474 | else { | ||
| 475 | $self->fatal("Invalid on_$type handler: $handler"); | ||
| 476 | } | ||
| 477 | $self->debug("mid-dispatch args: [", join(', ', @args), "]\n") if DEBUG; | ||
| 478 | # bail out if we got an empty list of return values or a single | ||
| 479 | # false value | ||
| 480 | last if ! @args || @args == 1 && ! $args[0]; | ||
| 481 | } | ||
| 482 | $self->debug("returning ", join(', ', @args), "\n") if DEBUG; | ||
| 483 | return @args; | ||
| 484 | } | ||
| 485 | |||
| 486 | |||
| 487 | |||
| 488 | #----------------------------------------------------------------------- | ||
| 489 | # Badger::Base::Trial - nomadic object for $object->try operation | ||
| 490 | #----------------------------------------------------------------------- | ||
| 491 | |||
| 492 | package Badger::Base::Trial; | ||
| 493 | our $AUTOLOAD; | ||
| 494 | |||
| 495 | sub _bind_ { | ||
| 496 | my ($class, $object) = @_; | ||
| 497 | bless \$object, ref $class || $class; | ||
| 498 | } | ||
| 499 | |||
| 500 | sub AUTOLOAD { | ||
| 501 | my $self = shift; | ||
| 502 | my ($method) = ($AUTOLOAD =~ /([^:]+)$/ ); | ||
| 503 | return if $method eq 'DESTROY'; | ||
| 504 | |||
| 505 | # call method on target object in eval block, and downgrade | ||
| 506 | if (wantarray) { | ||
| 507 | my @result = eval { $$self->$method(@_) }; | ||
| 508 | $$self->decline($@) if $@; | ||
| 509 | return @result; | ||
| 510 | } | ||
| 511 | else { | ||
| 512 | my $result = eval { $$self->$method(@_) }; | ||
| 513 | $$self->decline($@) if $@; | ||
| 514 | return $result; | ||
| 515 | } | ||
| 516 | |||
| 517 | # TODO: catch missing error methods | ||
| 518 | } | ||
| 519 | |||
| 520 | |||
| 521 | 1; | ||
| 522 | __END__ |