1 |
## -------------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
2 |
## $Id$ |
## $Id$ |
3 |
## -------------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.3 2003/01/20 16:54:22 joko |
6 |
|
## + sub fromPackage: refactored from libp's 'getNewPerlObjFromPkgName' or s.th.l.th. |
7 |
|
## |
8 |
## Revision 1.2 2002/12/27 16:05:42 joko |
## Revision 1.2 2002/12/27 16:05:42 joko |
9 |
## + played with Devel::CallerItem and Devel::StackTrace |
## + played with Devel::CallerItem and Devel::StackTrace |
10 |
## |
## |
11 |
## Revision 1.1 2002/12/13 21:46:29 joko |
## Revision 1.1 2002/12/13 21:46:29 joko |
12 |
## + initial check-in |
## + initial check-in |
13 |
## |
## |
14 |
## -------------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
15 |
|
|
16 |
|
|
17 |
package DesignPattern::Object; |
package DesignPattern::Object; |
24 |
#use Devel::CallerItem; |
#use Devel::CallerItem; |
25 |
#use Devel::StackTrace; |
#use Devel::StackTrace; |
26 |
|
|
27 |
|
|
28 |
|
# get logger instance |
29 |
|
my $logger = Log::Dispatch::Config->instance; |
30 |
|
|
31 |
sub new { |
sub new { |
32 |
|
|
33 |
# the classname in most cases |
# the classname in most cases |
36 |
# use already blessed reference, if passed in - else use the very classname |
# use already blessed reference, if passed in - else use the very classname |
37 |
my $class = ref ($classname) || $classname; |
my $class = ref ($classname) || $classname; |
38 |
|
|
39 |
|
$logger->debug( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" ); |
40 |
|
|
41 |
# the base for our object - a plain perl hash, which .... |
# the base for our object - a plain perl hash, which .... |
42 |
my $self = {}; |
my $self = {}; |
43 |
# note: |
# note: |
53 |
# ... or would this give us (harmful) circular module dependencies??? |
# ... or would this give us (harmful) circular module dependencies??? |
54 |
#$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"! |
#$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"! |
55 |
|
|
|
# remember the caller |
|
|
$self->{'__caller'} = caller; |
|
|
#print Dumper(caller(2)); |
|
|
#exit; |
|
56 |
|
|
57 |
# remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize! |
# remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize! |
58 |
#my $trace = Devel::StackTrace->new(); |
#my $trace = Devel::StackTrace->new(); |
64 |
#print Dumper($self); |
#print Dumper($self); |
65 |
#exit; |
#exit; |
66 |
|
|
67 |
|
|
68 |
# argument-handling ... |
# argument-handling ... |
69 |
|
|
70 |
# ... get them ... |
# ... get them ... |
85 |
|
|
86 |
# ... bless hash into object using classname |
# ... bless hash into object using classname |
87 |
bless $self, $class; |
bless $self, $class; |
88 |
|
|
89 |
|
# remember the caller |
90 |
|
$self->{'__caller'} = caller; |
91 |
|
#print Dumper(caller(2)); |
92 |
|
#exit; |
93 |
|
|
94 |
|
$self->{__classname} = $classname; |
95 |
|
|
96 |
$self->_init() if $self->can('_init'); |
$self->_init() if $self->can('_init'); |
97 |
|
|
98 |
return $self; |
return $self; |
99 |
} |
} |
100 |
|
|
103 |
my $self_classname = ref $self; |
my $self_classname = ref $self; |
104 |
my $function_name = shift; |
my $function_name = shift; |
105 |
# was: |
# was: |
106 |
# $logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\""); |
$logger->warning( __PACKAGE__ . ": function '$function_name' is an abstract method, please implement it in '$self_classname'."); |
107 |
# is: |
# is: |
108 |
die( __PACKAGE__ . ": Function '$function_name' is an abstract method, please implement it in '$self_classname'."); |
#die( __PACKAGE__ . ": Function '$function_name' is an abstract method, please implement it in '$self_classname'."); |
109 |
#exit; |
#exit; |
110 |
} |
} |
111 |
|
|
112 |
|
sub fromPackage { |
113 |
|
my $self = shift; |
114 |
|
#my $self_classname = ref $self; |
115 |
|
my $pkgname = shift; |
116 |
|
my @args = @_; |
117 |
|
# my $args = shift; |
118 |
|
|
119 |
|
# my $caller = $self->{'__caller'}; |
120 |
|
|
121 |
|
#print Dumper($args); |
122 |
|
|
123 |
|
$logger->debug( __PACKAGE__ . "->fromPackage( pkgname $pkgname args @args )" ); |
124 |
|
|
125 |
|
# perl-load |
126 |
|
my $evstring = "use $pkgname;"; |
127 |
|
eval($evstring); |
128 |
|
|
129 |
|
# report errors |
130 |
|
if ($@) { |
131 |
|
# build error-messages |
132 |
|
my $errmsg_native = __PACKAGE__ . ':' . __LINE__ . " Error in eval \"$evstring\": " . $@; |
133 |
|
#my $classname = $self->{__classname}; |
134 |
|
my $errmsg_critical = ''; |
135 |
|
if ($errmsg_native =~ m/Can't locate (.+?) in \@INC/) { |
136 |
|
$errmsg_critical = "Could not instantiate object from package '$pkgname' - location of '$1' failed."; |
137 |
|
} else { |
138 |
|
$errmsg_critical = $errmsg_native; |
139 |
|
} |
140 |
|
# write error to logging-output (console|file|database) |
141 |
|
$logger->debug( $errmsg_native ); |
142 |
|
$logger->critical( $errmsg_critical ); |
143 |
|
return; |
144 |
|
} |
145 |
|
|
146 |
|
# object-creation |
147 |
|
my $object = $pkgname->new(@args); |
148 |
|
|
149 |
|
# run boot-methods on object |
150 |
|
$object->_init() if $object->can('_init'); |
151 |
|
|
152 |
|
return $object; |
153 |
|
} |
154 |
|
|
155 |
1; |
1; |