/[cvs]/nfo/perl/libs/DesignPattern/Object.pm
ViewVC logotype

Contents of /nfo/perl/libs/DesignPattern/Object.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Wed Jan 22 17:56:49 2003 UTC (21 years, 4 months ago) by root
Branch: MAIN
Changes since 1.3: +6 -3 lines
+ fix: just use the logger if it's available

1 ## ---------------------------------------------------------------------------
2 ## $Id: Object.pm,v 1.3 2003/01/20 16:54:22 joko Exp $
3 ## ---------------------------------------------------------------------------
4 ## $Log: Object.pm,v $
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
9 ## + played with Devel::CallerItem and Devel::StackTrace
10 ##
11 ## Revision 1.1 2002/12/13 21:46:29 joko
12 ## + initial check-in
13 ##
14 ## ---------------------------------------------------------------------------
15
16
17 package DesignPattern::Object;
18
19 use strict;
20 use warnings;
21
22
23 use Data::Dumper;
24 #use Devel::CallerItem;
25 #use Devel::StackTrace;
26
27
28 # get logger instance
29 my $logger = eval { Log::Dispatch::Config->instance; };
30
31 sub new {
32
33 # the classname in most cases
34 my $classname = shift;
35
36 # use already blessed reference, if passed in - else use the very classname
37 my $class = ref ($classname) || $classname;
38
39 $logger->debug( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" ) if $logger;
40
41 # the base for our object - a plain perl hash, which ....
42 my $self = {};
43 # note:
44 # this makes an instance of an arbitrary perl variable,
45 # the most often used for this purpose is - guess it - the hash,
46 # since it resembles object-properties in a convenient way:
47 # $object->{property} = 'Hello World!';
48 # if you _do_ care about privacy you might take a look
49 # at CPAN's Tie::SecureHash or Class::Contract ... have fun!
50
51 # TODO: what about logging in here? inherit from
52 # DesignPattern::Object::Logger for this purpose....
53 # ... or would this give us (harmful) circular module dependencies???
54 #$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"!
55
56
57 # remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize!
58 #my $trace = Devel::StackTrace->new();
59 #print Dumper($trace);
60 #print Dumper($trace->frame(1)->args());
61
62 #print "args: ", $self->{'__caller'}->hasargs(), "\n";
63
64 #print Dumper($self);
65 #exit;
66
67
68 # argument-handling ...
69
70 # ... get them ...
71 my @args = ();
72 @_ && (@args = @_);
73
74 # ... check if we can coerce them into an array (this needs an even number of arguments)
75 my $argcount = $#args + 1;
76 my $fract = $argcount / 2;
77
78 my $seperate = pop @args if $fract != int($fract);
79
80 # mixin arguments
81 $self = { @args };
82
83 # mixin seperate
84 $self->{'__arg'} = $seperate if $seperate;
85
86 # ... bless hash into object using classname
87 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');
97
98 return $self;
99 }
100
101 sub _abstract_function {
102 my $self = shift;
103 my $self_classname = ref $self;
104 my $function_name = shift;
105 # was:
106 $logger->warning( __PACKAGE__ . ": function '$function_name' is an abstract method, please implement it in '$self_classname'.");
107 # is:
108 #die( __PACKAGE__ . ": Function '$function_name' is an abstract method, please implement it in '$self_classname'.");
109 #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;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed