--- nfo/perl/libs/DesignPattern/Bridge.pm 2003/01/31 01:19:50 1.5 +++ nfo/perl/libs/DesignPattern/Bridge.pm 2003/02/21 08:38:21 1.11 @@ -1,7 +1,28 @@ ## -------------------------------------------------------------------------------- -## $Id: Bridge.pm,v 1.5 2003/01/31 01:19:50 root Exp $ +## $Id: Bridge.pm,v 1.11 2003/02/21 08:38:21 joko Exp $ ## -------------------------------------------------------------------------------- ## $Log: Bridge.pm,v $ +## Revision 1.11 2003/02/21 08:38:21 joko +## + additional checks +## + raising exceptions +## +## Revision 1.10 2003/02/20 20:50:32 joko +## + small exception handling: now inheriting from little Exception object +## +## Revision 1.9 2003/02/18 18:35:30 joko +## + encapsulated/abstracted some more functionality: sub load_single +## +## Revision 1.8 2003/02/14 14:20:05 joko +## + modified mixin behaviour +## +## Revision 1.7 2003/02/11 10:34:19 joko +## + loaded module may now lack 'mixin::with' declaration +## + this gets us the possibility to load modules from any perl namespace +## + enabled this mechanism +## +## Revision 1.6 2003/02/09 16:22:51 joko +## + pseudo constructor mechanism via options +## ## Revision 1.5 2003/01/31 01:19:50 root ## + fixed: doesn't need Log::Dispatch any more, but uses it if available ## @@ -26,13 +47,18 @@ use strict; use warnings; -use base qw( DesignPattern::Object ); +use base qw( + DesignPattern::Object + DesignPattern::Exception +); + use Data::Dumper; ## ======== object inheritance ======== -# TODO: +# TODO / REFACTORING PROPOSAL +# leading from Data::Storage to code abstracted out into this module - DesignPattern::Bridge # - this is no inheritance and it doesn't have to be # - implement this module as a bridge to its sub-modules # - use the BridgePattern (http://c2.com/cgi/wiki?BridgePattern) @@ -43,27 +69,31 @@ # - sub getChildNodes # - sub run +# 2003-02-11, joko: does this have anything in parallel with CPAN's Class::Inner? + + # get logger instance my $logger = eval { Log::Dispatch::Config->instance; }; my $meta; ## ======== object constructor ======== - sub new { - my $invocant = shift; - my $class = ref($invocant) || $invocant; - my @args = (); - @_ && (@args = @_); - $logger->debug( __PACKAGE__ . "->new(@args)" ) if $logger; - my $self = { @_ }; +sub new { + my $invocant = shift; + my $class = ref($invocant) || $invocant; + my @args = (); + @_ && (@args = @_); + $logger->debug( __PACKAGE__ . "->new(@args)" ) if $logger; + my $self = { @_ }; + + # trace #print "class: $class", "\n"; - bless $self, $class; - ##if (my $bizWorks = shift) { - ##$self->boot($bizWorks); - ##} - - return $self; - } + + # create instance + bless $self, $class; + + return $self; +} ## ======== method overrider ======== @@ -98,15 +128,41 @@ # build full package name my $self_classname = ref $self; - my $package = $self_classname . '::' . $modulename_load; + # name + my $package = $modulename_load; + + # if package is absolute, cut away prefix ('/' or '::') + if ($package !~ s/^:://) { + # else: prefix with base classname if above name is relative (lacks of '/' or '::') + $package = $self_classname . '::' . $package + } + return $package; } sub load { my $self = shift; + my $modulename = shift; + my $options = shift; + + if (ref $modulename eq 'ARRAY') { + foreach (@$modulename) { + $self->load_single($_, $options); + } + } else { + $self->load_single($modulename, $options); + } + + } + + sub load_single { + + my $self = shift; my $modulename_load = shift; + my $options = shift; + my $self_modulename = ref $self; my $package = $self->_getPluginPackage($modulename_load); @@ -120,6 +176,9 @@ # this is the module testing phase - use mixin doesn't seem to propagate errors by default eval("use $package;"); + $self->checkExceptions(); + +=pod if ($@) { $meta->{loaded}->{$package} = 0; # include caller information @@ -132,6 +191,7 @@ print $msg, "\n"; } } +=cut #print "ref-1: ", ref $self, "\n"; #print "ref-2: ", ref $self::SUPER, "\n"; @@ -140,27 +200,63 @@ #bless $self, $package; # V2: - # switch into foreign package and mixin plugin-module $self->mixinPackage($package); + + if (my $method = $options->{method}) { + $self->$method(); + } return 1; } + + sub mixinPackage { + my $self = shift; + my $package = shift; + + # switch into foreign package and prepare for mixin + $self->mixin_prepare($package); + + # switch into local package (scope which uses DesignPattern::Bridge) and mixin plugin-module + $self->mixin_do($package); + + } # TODO: maybe refactor to DesignPattern::Object? what about the '$logger'? - sub mixinPackage { + sub mixin_prepare { + my $self = shift; + my $package = shift; + my $self_classname = ref $self; + eval("package $package; use mixin::with '$self_classname';"); + + # FIXME: --- this is redundant --- + if ($@) { + $meta->{loaded}->{$package} = 0; + $logger->error( __PACKAGE__ . "->load: $@" ) if $logger; + } else { + $meta->{loaded}->{$package} = 1; + } + # FIXME: --- this is redundant --- + + } + + sub mixin_do { my $self = shift; my $package = shift; # switch into foreign package and mixin plugin-module my $self_classname = ref $self; eval("package $self_classname; use mixin '$package';"); #eval("use mixin_all '$package';"); + + # FIXME: --- this is redundant --- if ($@) { $meta->{loaded}->{$package} = 0; $logger->error( __PACKAGE__ . "->load: $@" ) if $logger; } else { $meta->{loaded}->{$package} = 1; } + # FIXME: --- this is redundant --- + } sub unload { @@ -188,9 +284,22 @@ my $self = shift; my $includefile = shift; my $package = shift; - # TODO: do better error-detection here / prevent dies under all circumstances! - require $includefile; + + # pre-flight checks + if (!$includefile) { + $self->raiseException('Filename for inclusion was empty.'); + return; + } + + # go for it ... + eval("require '$includefile';"); + # ... and handle errors afterwards catching every message from perl itself ... + return if $self->checkExceptions(); + + # ... otherwise continue assuming everything is fine $self->mixinPackage($package) if $package; + } 1; +__END__